-2

I'm thinking of problem to find the nth Fibonacci number and I see various solutions such as recursive, dynamic programming, golden ratio and etc to name a few. I am wondering isn't calculating nth Fibonacci number sequentially a better solution than these? Am I missing something?

Either recursively or dynamic programming, we have to traverse the path from the 1st element in Fibonacci series to the nth element. If we use the golden ratio, it has an exponentially growing time complexity. Instead, if we iteratively calculate the nth Fibonacci number, we can achieve this in O(n) operations which seem to be better than other methods.

user3837868
  • 917
  • 1
  • 12
  • 24
  • I think the link in @Muhammad Orvais answer below gives you what you are looking for. – Peter Smith Apr 07 '19 at 05:51
  • 4
    Possible duplicate of [Finding out nth fibonacci number for very large 'n'](https://stackoverflow.com/questions/14661633/finding-out-nth-fibonacci-number-for-very-large-n) – greybeard Apr 07 '19 at 05:53

1 Answers1

0

Calculate nth fibonacci number with recursive solution:

function fibonacci(num) {
  if (num <= 1) return 1;

  return fibonacci(num - 1) + fibonacci(num - 2);
}

Also check reference site click here

I hope you got your answer

  • Interesting link +1 – Peter Smith Apr 07 '19 at 05:52
  • However, this program takes exponential time. Moreover, I'm sure OP is already aware of it. The question is not, "how to compute Fibonacci (n)?" but rather how to compute it efficiently. – rici Apr 07 '19 at 16:13