Given five integers i, Fi, j, Fj and N. Where Fi and Fj are the ith and jth term of a sequence which follows the Fibonacci recurrence i.e. FN = FN – 1 + FN – 2. The task is to find the Nth term of the original sequence.
Input: i = 3, F3 = 5, j = -1, F-1 = 4, N = 5
Fibonacci sequence can be reconstructed using known values:
…, F-1 = 4, F0 = -1, F1 = 3, F2 = 2, F3 = 5, F4 = 7, F5 = 12, …
Input: i = 0, F0 = 1, j = 1, F1 = 4, N = -2
Approach: Note that, if the two consecutive terms of the Fibonacci sequence are known then the Nth term can easily be determined. Assuming i < j, as per Fibonacci condition:
Fi+1 = 1*Fi+1 + 0*Fi
Fi+2 = 1*Fi+1 + 1*Fi
Fi+3 = Fi+2 + Fi+1 = 2*Fi+1 + 1*Fi
Fi+4 = Fi+3 + Fi+2 = 3*Fi+1 + 2*Fi
Fi+5 = Fi+4 + Fi+3 = 5*Fi+1 + 3*Fi
.. .. ..
and so on
Note that, the coefficients of Fi+1 and Fi in the above set of equations are nothing but the terms of Standard Fibonacci Sequence.
So, considering the Standard Fibonacci sequence i.e. f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, … ; we can generalize, the above set of equations (for k > 0) as:
Fi+k = fk*Fi+1 + fk-1*Fi …(1)
k = j-i …(2)
Now, substituting eq.(2) in eq.(1), we get:
Fj = fj-i*Fi+1 + fj-i-1*Fi
Hence, we can calculate Fi+1 from known values of Fi and Fj. Now that we know two consecutive terms of sequence F, we can easily reconstruct F and determine the value of FN.
Below is the implementation of the above approach:
- Program to find sum of the given sequence
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Find Recurring Sequence in a Fraction
- Program to find Nth term of the Van Eck's Sequence
- Find the Kth position element of the given sequence
- Find element position in given monotonic sequence
- Find if the given number is present in the infinite sequence or not
- Find nth term of the Dragon Curve Sequence
- Find the final sequence of the array after performing given operations
- Find a sequence of N prime numbers whose sum is a composite number
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Sum of the sequence 2, 22, 222, .........
- Recaman's sequence
- Farey Sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.