Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 9b60042593
Fetching contributors…

Cannot retrieve contributors at this time

25 lines (19 sloc) 0.733 kB
// a tail recursive fib implementation
// http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29
def fib(n: Int): Long = fib_tr(n, 1, 0)
def fib_tr(n: Int, b: Long, a: Long): Long = n match {
case 0 => a
case _ => fib_tr(n - 1, a + b, b)
}
// experimentations in summations (and tail recursion)
def acc_tr(n: Int): Int = {
acc_tr_helper(n, 0)
}
def acc_tr_helper(n: Int, sum: Int): Int = n match {
case 0 => sum
case _ => acc_tr_helper(n - 1, n + sum)
}
val maxAllowedValue = 4000000
val fibLessThanMax = Stream.from(1).map(fib(_)).takeWhile(_ < maxAllowedValue)
println(fibLessThanMax.filter(_ % 2 == 0).foldLeft(0L)(_ + _))
// http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/
Jump to Line
Something went wrong with that request. Please try again.