Find the Nth element of the modified Fibonacci series

• Difficulty Level : Basic
• Last Updated : 28 Apr, 2021

Given two integers A and B which are the first two terms of the series and another integer N. The task is to find the Nth number using Fibonacci rule i.e. fib(i) = fib(i – 1) + fib(i – 2)
Example:

Input: A = 2, B = 3, N = 4
Output:
The series will be 2, 3, 5, 8, 13, 21, …
And the 4th element is 8.
Input: A = 5, B = 7, N = 10
Output: 343

Approach: Intilalize variable sum = 0 that stores sum of the previous two values. Now, run a loop from i = 2 to N and for each index update value of sum = A + B and A = B, B = sum. Then finally, return the sum which is the required Nth element.
Below is the implementation of the above approach:

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 termsint 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 codeint main(){    int A = 5, B = 7, N = 10;     cout << findNthNumber(A, B, N);     return 0;}

Java

 // Java implementation of the approachimport 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 termsdef 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 codeif __name__ == '__main__':    A = 5    B = 7    N = 10     print(findNthNumber(A, B, N)) # This code is contributed by Ashutosh450

C#

 // C# implementation of the approachusing 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

Javascript


Output:
343

My Personal Notes arrow_drop_up