86. Generate Fibonacci Number

JavaScriptAlgorithm
Is this helpful ?

medium  - accepted / - tried

0
1
1 = 0 + 1
2 = 1 + 1
3 = 1 + 2
5 = 2 + 3
8 = 3 + 5
13 = 5 + 8
....

[0,1,1,2,3,5,8,13 ...]

Given 2 initial numbers, we can generate a sequence by adding the sum of last two numbers as a new lement.

This is Fibonacci number.

You are asked to create a fib(n) function, which generate the n-th Fibonacci number.

What is the time & space cost of your solution?

Let's try to solve this problem within 10 mintues.

Discuss(27)