Fibonnaci Sequence fast implementation
Posted
by
user2947615
on Stack Overflow
See other posts from Stack Overflow
or by user2947615
Published on 2013-11-05T21:24:27Z
Indexed on
2013/11/05
21:53 UTC
Read the original article
Hit count: 175
I have written this function in Scala to calculate the fibonacci number given a particular index n:
def fibonacci(n: Long): Long = {
if(n <= 1) n
else
fibonacci(n - 1) + fibonacci(n - 2)
}
However it is not efficient when calculating with large indexes. Therefore I need to implement a function using a tuple and this function should return two consecutive values as the result.
Can somebody give me any hints about this? I have never used Scala before. Thanks!
© Stack Overflow or respective owner