📌  相关文章
📜  给定序列的F(i)和F(j)时找到F(n)

📅  最后修改于: 2021-05-04 11:59:56             🧑  作者: Mango

给定五个整数i,F i ,j,F j和N。其中, F iF j是斐波那契递归之后的序列的ij项,即F N = F N – 1 + F N – 2 。任务是找到原始序列的N项。

例子:

方法:请注意,如果已知斐波纳契数列的两个连续项,则可以轻松确定N项。根据斐波那契条件,假设i

注意,上述方程组中的F i + 1F i的系数不过是标准斐波纳契数列的项。

因此,考虑标准斐波那契数列,即f 0 = 0,f 1 = 1,f 2 = 1,f 3 = 2,f 4 = 3,… ;我们可以将上述方程组(对于k> 0)推广为:

现在考虑,

现在,将eq。(2)替换为eq。(1) ,我们得到:

因此,我们可以从已知的F iF j值计算出F i + 1 。现在我们知道序列F的两个连续项,我们可以轻松地重建F并确定F N的值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
  
using namespace std;
  
// Function to calculate kth Fibonacci number
// in the standard Fibonacci sequence
int fibonacci(int k)
{
    int a = 0, b = 1, c = 0;
    if( k == 0)
        return a;
    if (k == 1)
        return b;
    for(int i = 2; i < k + 1; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}
  
// Function to determine the value of F(n)
int determineFn(int i, int Fi, int j, int Fj, int n)
{
    if (j < i)
    {
        swap(i, j);
        swap(Fi, Fj);
    }
  
    // Find the value of F(i + 1)
    // from F(i) and F(j)
    int Fi1 = (Fj - fibonacci(j - i - 1) * Fi) / 
                    fibonacci(j - i);
  
    // When n is equal to i
    int Fn = 0;
    if (n == i)
        Fn = Fi;
  
    // When n is greater than i
    else if (n > i) 
    {
        int b = Fi;
        Fn = Fi1;
        n = n - 1;
        while (n != i)
        {
            n = n - 1;
            int a = b;
            b = Fn;
            Fn = a + b;
        }
    }
      
    // When n is smaller than i
    else 
    {
        int a = Fi;
        int b = Fi1;
        while (n != i)
        {
            n = n + 1;
            Fn = b - a;
            b = a;
            a = Fn;
        }
    }
    return Fn;
}
  
// Driver code
int main()
{
    int i = 3;
    int Fi = 5;
    int j = -1;
    int Fj = 4;
    int n = 5;
    cout << (determineFn(i, Fi, j, Fj, n));
}
  
// This code is contributed by Mohit Kumar


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
    // Function to calculate kth Fibonacci number
    // in the standard Fibonacci sequence
    static int fibonacci(int k)
    {
        int a = 0, b = 1, c = 0;
        if (k == 0)
            return a;
        if (k == 1)
            return b;
        for (int i = 2; i < k + 1; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
  
    // Function to determine the value of F(n)
    static int determineFn(int i, int Fi,       
                           int j, int Fj, int n) 
    {
        if (j < i) 
        {
  
            // Swap i, j
            i = i + j;
            j = i - j;
            i = i - j;
  
            // swap Fi, Fj
            Fi = Fi + Fj;
            Fj = Fi - Fj;
            Fi = Fi - Fj;
        }
  
        // Find the value of F(i + 1)
        // from F(i) and F(j)
        int Fi1 = (Fj - fibonacci(j - i - 1) * Fi) / 
                        fibonacci(j - i);
  
        // When n is equal to i
        int Fn = 0;
        if (n == i)
            Fn = Fi;
  
        // When n is greater than i
        else if (n > i)
        {
            int b = Fi;
            Fn = Fi1;
            n = n - 1;
            while (n != i)
            {
                n = n - 1;
                int a = b;
                b = Fn;
                Fn = a + b;
            }
        }
  
        // When n is smaller than i
        else
        {
            int a = Fi;
            int b = Fi1;
            while (n != i) 
            {
                n = n + 1;
                Fn = b - a;
                b = a;
                a = Fn;
            }
        }
        return Fn;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int i = 3;
        int Fi = 5;
        int j = -1;
        int Fj = 4;
        int n = 5;
        System.out.println(determineFn(i, Fi, j, Fj, n));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# 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 ji:
        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))


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function to calculate kth Fibonacci number
    // in the standard Fibonacci sequence
    static int fibonacci(int k)
    {
        int a = 0, b = 1, c = 0;
        if (k == 0)
            return a;
        if (k == 1)
            return b;
        for (int i = 2; i < k + 1; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
  
    // Function to determine the value of F(n)
    static int determineFn(int i, int Fi,     
                           int j, int Fj, int n) 
    {
        if (j < i) 
        {
  
            // Swap i, j
            i = i + j;
            j = i - j;
            i = i - j;
  
            // swap Fi, Fj
            Fi = Fi + Fj;
            Fj = Fi - Fj;
            Fi = Fi - Fj;
        }
  
        // Find the value of F(i + 1)
        // from F(i) and F(j)
        int Fi1 = (Fj - fibonacci(j - i - 1) * Fi) / 
                        fibonacci(j - i);
  
        // When n is equal to i
        int Fn = 0;
        if (n == i)
            Fn = Fi;
  
        // When n is greater than i
        else if (n > i)
        {
            int b = Fi;
            Fn = Fi1;
            n = n - 1;
            while (n != i)
            {
                n = n - 1;
                int a = b;
                b = Fn;
                Fn = a + b;
            }
        }
  
        // When n is smaller than i
        else
        {
            int a = Fi;
            int b = Fi1;
            while (n != i) 
            {
                n = n + 1;
                Fn = b - a;
                b = a;
                a = Fn;
            }
        }
        return Fn;
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        int i = 3;
        int Fi = 5;
        int j = -1;
        int Fj = 4;
        int n = 5;
        Console.WriteLine(determineFn(i, Fi, j, Fj, n));
    }
}
  
// This code is contributed by Rajput-Ji


输出:
12