We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A better way that take advantage of lazy evaluation in haskell is to use infinite recursion to calculate the serie of all fibonacci numbers
-- this has all the fibonacci numbers and never ends-- [0, 1, 1, 2, 3, 5, 8, 13, 21 ... ]-- but haskell does not calculate them unless you actually-- ask for them.fib=serie01whereserieij=i:seriej(i+j)
Then if you want the nth fib, all you have to do is:
fib!!n
which is very efficient O(n)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fibonacci
You are viewing a single comment's thread. Return to all comments →
A better way that take advantage of lazy evaluation in haskell is to use infinite recursion to calculate the serie of all fibonacci numbers
Then if you want the nth fib, all you have to do is:
which is very efficient O(n)