Given a number N, the task is to evaluate below expression. Expected time complexity is O(1).
f(n-1)*f(n+1) - f(n)*f(n)
Where f(n) is the n-th Fibonacci number with n >= 1. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, ………..i.e. (considering 0 as 0th Fibonacci number)
Input : n = 5 Output : -1 f(5-1=4) = 3 f(5+1=6) = 8 f(5)*f(5)= 5*5 = 25 f(4)*f(6)- f(5)*f(5)= 24-25= -1
Although the task is simple i.e. find n-1th, nth and (n+1)-th Fibonacci numbers. Evaluate the expression and display the result. But this can be done in O(1) time using Cassini’s Identity which states that:
f(n-1)*f(n+1) - f(n*n) = (-1)^n
So, we don’t need to calculate any Fibonacci term,the only thing is to check whether n is even or odd.
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Proizvolov's Identity
- Euler's Four Square Identity
- Significance of Pascal’s Identity
- Program for Identity Matrix
- Brahmagupta Fibonacci Identity
- Count of subarrays with sum at least K
- Minimize the number by changing at most K digits
- Find the number of ways to reach Kth step in stair case
- Find the count of unvisited indices in an infinite array
- Check whether the exchange is possible or not
- Find the coordinates of a triangle whose Area = (S / 2)
- Find permutation array from the cumulative sum array
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal