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.
Since this problem is such a basic introduction to recursion, I like to go against the grain and like solving it without recursion. This problem can be trivialized with a little mathematical insight. Binet's formula is a formula for calculating the nth fibonacci number.
In a mathematically perfect universe the function would always output an integer, but due to floating point error it will usually be a little off. Rounding it to the nearest integer will solve this issue for all practical purposes (I have a hunch that if the input is allowed to be arbitrarily large then the error might make it round to the wrong int, but the input would probably be far outside any range one would reasonably be working with at that point).
Cookie support is required to access HackerRank
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 →
Since this problem is such a basic introduction to recursion, I like to go against the grain and like solving it without recursion. This problem can be trivialized with a little mathematical insight. Binet's formula is a formula for calculating the nth fibonacci number.
In a mathematically perfect universe the function would always output an integer, but due to floating point error it will usually be a little off. Rounding it to the nearest integer will solve this issue for all practical purposes (I have a hunch that if the input is allowed to be arbitrarily large then the error might make it round to the wrong int, but the input would probably be far outside any range one would reasonably be working with at that point).