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.
As Jacob said, the problem is unusual in the sense that it starts with 1 instead of 0. Your solution is correct if you print fibonacci n-1
Although correct, this implementation is inneficient. If you calculate fibonnaci(5) you will end up calculating fibonnaci(3) twice. For larger numbers the tree will grow in O(2^n) performance.
A better way is to calculate fibonnaci from 0 upwards and stopping at the number you want. This way you only calculate each fibonnaci once and you solve your problem in O(n). For example:
-- never ending series of fibonacci numbersfibSerieab=a:fibSerieb(a+b)-- take the n - 1 number in the seriesfibn=(fibSerie01)!!(n-1)
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 →
As Jacob said, the problem is unusual in the sense that it starts with 1 instead of 0. Your solution is correct if you print fibonacci n-1
Although correct, this implementation is inneficient. If you calculate fibonnaci(5) you will end up calculating fibonnaci(3) twice. For larger numbers the tree will grow in O(2^n) performance.
A better way is to calculate fibonnaci from 0 upwards and stopping at the number you want. This way you only calculate each fibonnaci once and you solve your problem in O(n). For example: