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.
I used Long in Scala. For those having problems with overflow you may note that we are looking for
fib(n) % (10**8 + 7)
A nice property I used is that:
(a + b) % n = (a % n + b % n) % n
So, you can calculate and make a memo on fib(n)%(10**8+7) instead of using slower representations of big numbers. I passed all tests, but I wonder if the extra mods calculations are more efficient than the other solutions you mentioned.
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 comment for Scala.
[SPOILER]
I used Long in Scala. For those having problems with overflow you may note that we are looking for
A nice property I used is that:
So, you can calculate and make a memo on fib(n)%(10**8+7) instead of using slower representations of big numbers. I passed all tests, but I wonder if the extra mods calculations are more efficient than the other solutions you mentioned.