Ninja grew 19 years older and started his own detective company. One day a kidnapper challenges our Ninja to solve a case. Kidnapper makes many duplicates of a child and each child (or we can say duplicate and the original one) is assigned a unique number. A bomb is tied to each child and it will blast within 5 minutes.
Now he gives a number βNβ to Ninja and tells him that the child mentioned with βNthβ Fibonacci number is the original child.
So help our Ninja in finding the βNthβ Fibonacci number so he can save the lives of children as within 5 minutes he can't defuse all the bombs alone.
Nth term of Fibonacci series F(N) is calculated using the following formula -
F(N) = F(N-1) + F(N-2),
Where, F(1) = 1 F(2) = 1
The first line of input contains the βTβ number of test cases.
The first line of each test case contains a real number βNβ.
Output Format :
For each test case, return its equivalent Fibonacci number.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
1 <= T <= 10000
1 <= N <= 40
Time Limit: 1 second
2
4
6
3
8
Test Case 1:
In the first line, there is the number of test cases i.e., 2, and in the next line β4βis the number for which we have to find its equivalent Fibonacci number or we can say return the β4thβ Fibonacci number.
So by using the property of the Fibonacci series i.e
[ 1, 1, 2, 3]
So the β4thβ element is β3β hence we get the output.
Test Case 2:
Now the number is β6β so we have to find the β6thβ Fibonacci number
So by using the property of the Fibonacci series i.e
[ 1, 1, 2, 3, 5, 8]
So the β6thβ element is β8β hence we get the output.
2
5
3
5
2
Can we use recursion to solve the problem?
O(2^N), where βNβ is the number for which we are finding its equivalent Fibonacci.
As our function will be called exponentially.
O(N), where βNβ is the given number.
As recursion uses a stack of size βNβ