# Find F(n) when F(i) and F(j) of a sequence are given

Given five integers **i, F _{i}, j, F_{j} and N**. Where

**F**and

_{i}**F**are the

_{j}**i**and

^{th}**j**term of a sequence which follows the Fibonacci recurrence i.e.

^{th}**F**. The task is to find the

_{N}= F_{N – 1}+ F_{N – 2}**N**term of the original sequence.

^{th}**Examples:**

Input:i = 3, F_{3}= 5, j = -1, F_{-1}= 4, N = 5

Output:12

Fibonacci sequence can be reconstructed using known values:

…, F_{-1}= 4, F_{0}= -1, F_{1}= 3, F_{2}= 2, F_{3}= 5, F_{4}= 7, F_{5}= 12, …

Input:i = 0, F_{0}= 1, j = 1, F_{1}= 4, N = -2

Output:-2

**Approach:** Note that, if the two consecutive terms of the Fibonacci sequence are known then the **N ^{th}** term can easily be determined. Assuming

**i < j**, as per Fibonacci condition:

F

_{i+1}= 1*F_{i+1}+ 0*F_{i}

F_{i+2}= 1*F_{i+1}+ 1*F_{i}

F_{i+3}= F_{i+2}+ F_{i+1}= 2*F_{i+1}+ 1*F_{i}

F_{i+4}= F_{i+3}+ F_{i+2}= 3*F_{i+1}+ 2*F_{i}

F_{i+5}= F_{i+4}+ F_{i+3}= 5*F_{i+1}+ 3*F_{i}

.. .. ..

and so on

Note that, the coefficients of **F _{i+1}** and

**F**in the above set of equations are nothing but the terms of Standard Fibonacci Sequence.

_{i}So, considering the Standard Fibonacci sequence i.e. *f _{0} = 0, f_{1} = 1, f_{2} = 1, f_{3} = 2, f_{4} = 3, …* ; we can generalize, the above set of equations (for k > 0) as:

F

_{i+k}= f_{k}*F_{i+1}+ f_{k-1}*F_{i}…(1)

Now consider,

k = j-i

…(2)

Now, substituting **eq.(2)** in **eq.(1)**, we get:

F

_{j}= f_{j-i}*F_{i+1}+ f_{j-i-1}*F_{i}

Hence, we can calculate **F _{i+1}** from known values of

**F**and

_{i}**F**. Now that we know two consecutive terms of sequence

_{j}**F**, we can easily reconstruct

**F**and determine the value of

**F**.

_{N}Below is the implementation of the above approach:

`# Python3 implementation of the approach ` ` ` `# Function to calculate kth Fibonacci number ` `# in the standard Fibonacci sequence ` `def` `fibonacci(k): ` ` ` `a ` `=` `0` ` ` `b ` `=` `1` ` ` `if` `k ` `=` `=` `0` `: ` ` ` `return` `a ` ` ` `if` `k ` `=` `=` `1` `: ` ` ` `return` `b ` ` ` `for` `i ` `in` `range` `(` `2` `, k ` `+` `1` `): ` ` ` `c ` `=` `a ` `+` `b ` ` ` `a ` `=` `b ` ` ` `b ` `=` `c ` ` ` `return` `c ` ` ` `# Function to determine ` `# the value of F(n) ` `def` `determineFn(i, Fi, j, Fj, n): ` ` ` `if` `j<i: ` ` ` `i, j ` `=` `j, i ` ` ` `Fi, Fj ` `=` `Fj, Fi ` ` ` ` ` `# Find the value of F(i + 1) ` ` ` `# from F(i) and F(j) ` ` ` `Fi1 ` `=` `(Fj ` `-` `fibonacci(j` `-` `i` `-` `1` `)` `*` `Fi)\ ` ` ` `/` `/` `fibonacci(j` `-` `i) ` ` ` ` ` `# When n is equal to i ` ` ` `if` `n ` `=` `=` `i: ` ` ` `Fn ` `=` `Fi ` ` ` ` ` `# When n is greater than i ` ` ` `elif` `n>i: ` ` ` `b ` `=` `Fi ` ` ` `Fn ` `=` `Fi1 ` ` ` `n ` `=` `n ` `-` `1` ` ` `while` `n !` `=` `i: ` ` ` `n ` `=` `n ` `-` `1` ` ` `a ` `=` `b ` ` ` `b ` `=` `Fn ` ` ` `Fn ` `=` `a ` `+` `b ` ` ` ` ` `# When n is smaller than i ` ` ` `else` `: ` ` ` `a ` `=` `Fi ` ` ` `b ` `=` `Fi1 ` ` ` `while` `n !` `=` `i: ` ` ` `n ` `=` `n ` `+` `1` ` ` `Fn ` `=` `b ` `-` `a ` ` ` `b ` `=` `a ` ` ` `a ` `=` `Fn ` ` ` `return` `Fn ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `i ` `=` `3` ` ` `Fi ` `=` `5` ` ` `j ` `=` `-` `1` ` ` `Fj ` `=` `4` ` ` `n ` `=` `5` ` ` `print` `(determineFn(i, Fi, j, Fj, n)) ` |

*chevron_right*

*filter_none*

**Output:**

12

## Recommended Posts:

- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Program to find sum of the given sequence
- Find Recurring Sequence in a Fraction
- Find if the given number is present in the infinite sequence or not
- Find element position in given monotonic sequence
- 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, .........
- Juggler Sequence
- Padovan Sequence
- Aliquot Sequence
- Increasing sequence with given GCD

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.