You are viewing a single comment's thread. Return to all comments →
Tail recursive in Scala
def fibonacci(x: Int): Int = { val initialValue = 0 // f(1) == 0 val initNext = 1 // f(2) == 1 val initialIndex = 1 // 1 < N <= 40 @tailrec def iter(cur: Int, next: Int, acc: Int): Int = { if (acc == x) cur else iter(next, cur + next, acc + 1) } iter(initialValue, initialNext, initialIndex) }
Seems like cookies are disabled on this browser, please enable them to open this website
Fibonacci Numbers
You are viewing a single comment's thread. Return to all comments →
Tail recursive in Scala