📜  找到修改后的斐波那契数列的第N个元素

📅  最后修改于: 2021-04-29 09:50:25             🧑  作者: Mango

给定两个整数AB ,它们是该序列的前两个项,另一个整数N。任务是使用斐波那契定律找到第N数字,即fib(i)= fib(i – 1)+ fib(i – 2)

例子:

方法:引入变量sum = 0 ,该变量存储前两个值的和。现在,运行一个从i = 2到N的循环,对于sum = A + BA = B,B = sum的每个索引更新值。然后,最后返回总和,它是所需的第N个元素

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the Nth number of
// the modified Fibonacci series where
// A and B are the first two terms
int findNthNumber(int A, int B, int N)
{
  
    // To store the current element which
    // is the sum of previous two
    // elements of the series
    int sum = 0;
  
    // This loop will terminate when
    // the Nth element is found
    for (int i = 2; i < N; i++) {
        sum = A + B;
  
        A = B;
  
        B = sum;
    }
  
    // Return the Nth element
    return sum;
}
  
// Driver code
int main()
{
    int A = 5, B = 7, N = 10;
  
    cout << findNthNumber(A, B, N);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    // Function to return the Nth number of
    // the modified Fibonacci series where
    // A and B are the first two terms
    static int findNthNumber(int A, int B, int N) 
    {
  
        // To store the current element which
        // is the sum of previous two
        // elements of the series
        int sum = 0;
  
        // This loop will terminate when
        // the Nth element is found
        for (int i = 2; i < N; i++)
        {
            sum = A + B;
  
            A = B;
  
            B = sum;
        }
  
        // Return the Nth element
        return sum;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int A = 5, B = 7, N = 10;
  
        System.out.println(findNthNumber(A, B, N));
    }
} 
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
  
# Function to return the Nth number of
# the modified Fibonacci series where
# A and B are the first two terms
def findNthNumber(A, B, N):
  
    # To store the current element which
    # is the sum of previous two
    # elements of the series
    sum = 0
  
    # This loop will terminate when
    # the Nth element is found
    for i in range(2, N):
        sum = A + B
  
        A = B
  
        B = sum
      
    # Return the Nth element
    return sum
  
# Driver code
if __name__ == '__main__':
    A = 5
    B = 7
    N = 10
  
    print(findNthNumber(A, B, N))
  
# This code is contributed by Ashutosh450


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the Nth number of
    // the modified Fibonacci series where
    // A and B are the first two terms
    static int findNthNumber(int A, int B, int N) 
    {
  
        // To store the current element which
        // is the sum of previous two
        // elements of the series
        int sum = 0;
  
        // This loop will terminate when
        // the Nth element is found
        for (int i = 2; i < N; i++)
        {
            sum = A + B;
  
            A = B;
  
            B = sum;
        }
  
        // Return the Nth element
        return sum;
    }
  
    // Driver code
    public static void Main()
    {
        int A = 5, B = 7, N = 10;
  
        Console.WriteLine(findNthNumber(A, B, N));
    }
}
  
// This code is contributed by AnkitRai01


输出:
343