Edited at

Scalaの並列コレクションでフィボナッチ数列

More than 3 years have passed since last update.


フィボナッチ数列を計算


Fib.scala

object Fib {

def main(args: Array[String]): Unit = {
println ((1 to 30).par.map(x => fib(x)))
}
def fib(n: Int): Int = if(n < 2) 1 else fib(n - 1) + fib(n - 2)
}


出力:

ParVector(1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269)