Nth Fibonacci Number
Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
F(n) = F(n-1) + F(n-2),
Where, F(1) = F(2) = 1
Provided N you have to find out the Nth Fibonacci Number.
Input Format :
The first line of each test case contains a real number ‘N’.
Output Format :
For each test case, return its equivalent Fibonacci number.
Constraints:
1 <= N <= 10000
Where ‘N’ represents the number for which we have to find its equivalent Fibonacci number.
Time Limit: 1 second
CodingNinjas
author
2y
step 1 : initilize a = 0 and b = 1
step 2 : for i = 1 to i = n:
print(a)
temp = a+b
a = b
b = temp
CodingNinjas
author
2y
Recursive Approach
- In this approach, we use recursion and uses a basic condition that :
- If ‘N’ is smaller than ‘1’(N<=1) we return ‘N’
- Else we call the function again as ninjaJasoos(N-1) + ninjaJasoos(N-...read more
Anonymous
5mo
def nth_fibonacci_iterative(n): if n <= 0: return 0 elif n == 1 or n == 2: return 1 a, b = 1, 1 for ...read more
CodingNinjas
author
2y
Dynamic Programming
Ans of ‘i-th’ number is = ‘ans[i-11+ans[i-2]’, so
- With the help of dynamic programming, we try to store the previous value as the previous value leads to a solution.
- So we use an arra...read more
CodingNinjas
author
2y
Optimized Dynamic programming
- We take three integers a, b, c and we initialized a=0, b=1 as now we want to optimize the space by only storing “2” last numbers as we need only them.
- Now we run a loop up ...read more
xaxa
1y
freelancer
megs
excellentlypanettone
1y
freelancer
mesaaaa
Add answer anonymously...
Top Cognizant Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Cognizant Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app