Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,5 @@
def fib(i:Int, a:Int=1, b:Int=0):Int = i match{
case 1 => b
case _ => fib(i-1, b, a+b)
}
def fib(x:Int, prev: BigInt = 0, next: BigInt = 1):BigInt = x match {
case 0 => prev
case 1 => next
case _ => fib(x-1, next, (next + prev))
}

View file

@ -1,13 +1,3 @@
// Fibonacci using BigInt with Stream.foldLeft optimized for GC (Scala v2.9 and above)
// Does not run out of memory for very large Fibonacci numbers
def fib(n:Int) = {
def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j, i+j)
series(1,0).take(n).foldLeft(BigInt("0"))(_+_)
}
// Small test
(0 to 13) foreach {n => print(fib(n).toString + " ")}
// result: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
val it = Iterator.iterate((0,1)){case (a,b) => (b,a+b)}.map(_._1)
//example:
println(it.take(13).mkString(",")) //prints: 0,1,1,2,3,5,8,13,21,34,55,89,144