Generalized Fibonacci Numbers

We all know that Fibonacci numbers (Fn) is defined by the recurrence relation 
 

Fibonacci Numbers (Fn) = F(n-1) + F(n-2) 
with seed values 
F0 = 0 and F1 = 1 
 

Similarly, we can generalise these numbers. Such number sequence is known as Generalized Fibonacci number (G)
Generalized Fibonacci number (G) is defined by the recurrence relation 
 

Generalised Fibonacci Numbers (Gn) = (c * G(n-1)) + (d * G(n-2)) 
with seed values 
G0 = a and G1 = b 
 

 



Finding Nth term

Given the four constant values of Generalised Fibonacci Numbers as a, b, c and d and an integer N, the task is to find the Nth term of the Generalised Fibonacci Numbers, i.e. Gn.
Examples: 
 

Input: N = 2, a = 0, b = 1, c = 2, d = 3 
Output:
Explanation: 
As a = 0 -> G(0) = 0 
b = 1 -> G(1) = 1 
So, G(2) = 2 * G(1) + 3 * G(0) = 2
Input: N = 3, a = 0, b = 1, c = 2, d = 3 
Output:
 

 

Naive Approach: Using the given values, find each term of the series till Nth term and then print the Nth term. 
Time Complexity: O(2N)
Another Approach: The idea is to use DP tabulation to find all the terms till Nth terms and then print the Nth term. 
Time Complexity: O(N)
 

Efficient Approach: Using matrix multiplication we can solve the given problem in log(N) time.

 

\small {Given\, G(0)=a\, and\, G(1)=b, \, therefore \, G(2)=c*a+d*b } \\ \small {} \\  \begin{bmatrix} G(2) & G(1)\\ G(1) & G(0) \end{bmatrix} = \begin{bmatrix} d*b+c*a  & b\\ b & a \end{bmatrix} \newline  \text {Multiplying matrices on RHS will eventually give us our LHS }\\  \begin{bmatrix} G(3) & G(2)\\ G(2) & G(1) \end{bmatrix} = \begin{bmatrix} G(2) & G(1)\\ G(1) & G(0) \end{bmatrix} * \begin{bmatrix} d & 1\\ c & 0 \end{bmatrix} \newline  \text {One more step } \\  \begin{bmatrix} G(4) & G(3)\\ G(3) & G(2) \end{bmatrix} = \begin{bmatrix} G(3) & G(2)\\ G(2) & G(1) \end{bmatrix} * \begin{bmatrix} d & 1\\ c & 0 \end{bmatrix} \newline  \text {So by looking at the sequence above we can generalize the Nth term as... }\\  \begin{bmatrix} G(N) & G(N-1)\\ G(N-1) & G(N-2) \end{bmatrix} = \begin{bmatrix} G(2) & G(1)\\ G(1) & G(0) \end{bmatrix} * \begin{bmatrix} d & 1\\ c & 0 \end{bmatrix} ^{\!N-2}\newline  \text{Now }\begin{bmatrix} d & 1\\ c & 0 \end{bmatrix} ^{\!N-2}} \text{ can be solved in log(N) time complexity }

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement the 
// Generalised Fibonacci numbers 
  
#include <bits/stdc++.h> 
using namespace std; 
  
// Helper function that multiplies 
// 2 matrices F and M of size 2*2, 
// and puts the multiplication 
// result back to F[][] 
void multiply(int F[2][2], int M[2][2]); 
  
// Helper function that calculates F[][] 
// raised to the power N 
// and puts the result in F[][] 
void power(int F[2][2], int N, int m, int n); 
  
// Function to find the Nth term 
int F(int N, int a, int b, int m, int n) 
    // m 1 
    // n 0 
    int F[2][2] = { { m, 1 }, { n, 0 } }; 
  
    if (N == 0) 
        return a; 
  
    if (N == 1) 
        return b; 
  
    if (N == 2) 
        return m * b + n * a; 
  
    int initial[2][2] 
        = { { m * b + n * a, b }, 
            { b, a } }; 
  
    power(F, N - 2, m, n); 
  
    // Discussed above
    multiply(initial, F); 
  
    return F[0][0]; 
  
// Function that multiplies 
// 2 matrices F and M of size 2*2, 
// and puts the multiplication 
// result back to F[][] 
void multiply(int F[2][2], int M[2][2]) 
    int x = F[0][0] * M[0][0] + F[0][1] * M[1][0]; 
    int y = F[0][0] * M[0][1] + F[0][1] * M[1][1]; 
    int z = F[1][0] * M[0][0] + F[1][1] * M[1][0]; 
    int w = F[1][0] * M[0][1] + F[1][1] * M[1][1]; 
  
    F[0][0] = x; 
    F[0][1] = y; 
    F[1][0] = z; 
    F[1][1] = w; 
  
// Function that calculates F[][] 
// raised to the power N 
// and puts the result in F[][] 
void power(int F[2][2], int N, int m, int n) 
    int i; 
    int M[2][2] = { { m, 1 }, { n, 0 } }; 
  
    for (i = 1; i <= N; i++) 
        multiply(F, M); 
  
// Driver code 
int main() 
    int N = 2, a = 0, b = 1, m = 2, n = 3; 
    printf("%d\n", F(N, a, b, m, n)); 
  
    N = 3; 
    printf("%d\n", F(N, a, b, m, n)); 
  
    N = 4; 
    printf("%d\n", F(N, a, b, m, n)); 
  
    N = 5; 
    printf("%d\n", F(N, a, b, m, n)); 
  
    return 0; 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement the 
// Generalised Fibonacci numbers 
import java.util.*;
  
class GFG{
  
// Function to find the Nth term 
static int F(int N, int a, int b, 
             int m, int n) 
    // m 1 
    // n 0 
    int[][] F = { { m, 1 }, { n, 0 } }; 
  
    if (N == 0
        return a; 
    if (N == 1
        return b; 
    if (N == 2
        return m * b + n * a; 
  
    int[][] initial = { { m * b + n * a, b }, 
                        { b, a } }; 
                          
    power(F, N - 2, m, n); 
  
    // Discussed below 
    multiply(initial, F); 
  
    return F[0][0]; 
  
// Function that multiplies 
// 2 matrices F and M of size 2*2, 
// and puts the multiplication 
// result back to F[][] 
static void multiply(int[][] F, int[][] M) 
    int x = F[0][0] * M[0][0] + 
            F[0][1] * M[1][0]; 
    int y = F[0][0] * M[0][1] + 
            F[0][1] * M[1][1]; 
    int z = F[1][0] * M[0][0] + 
            F[1][1] * M[1][0]; 
    int w = F[1][0] * M[0][1] + 
            F[1][1] * M[1][1]; 
  
    F[0][0] = x; 
    F[0][1] = y; 
    F[1][0] = z; 
    F[1][1] = w; 
  
// Function that calculates F[][] 
// raised to the power N 
// and puts the result in F[][] 
static void power(int[][] F, int N, 
                  int m, int n) 
    int i; 
    int[][] M = { { m, 1 }, { n, 0 } }; 
  
    for(i = 1; i <= N; i++) 
        multiply(F, M); 
  
// Driver code
public static void main(String[] args)
{
    int N = 2, a = 0, b = 1, m = 2, n = 3
    System.out.println(F(N, a, b, m, n));
      
    N = 3
    System.out.println(F(N, a, b, m, n)); 
      
    N = 4
    System.out.println(F(N, a, b, m, n)); 
      
    N = 5
    System.out.println(F(N, a, b, m, n)); 
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement the
# Generalised Fibonacci numbers
  
# Function to find the Nth term
def F(N, a, b, m, n):
      
    # m 1 
    # n 0 
    F = [[ m, 1 ], [ n, 0 ]]
  
    if(N == 0):
        return a
    if(N == 1):
        return b
    if(N == 2):
        return m * b + n * a
  
    initial = [[ m * b + n * b, b ],
                           [ b, a ]]
  
    power(F, N - 2, m, n)
  
    multiply(initial, F)
  
    return F[0][0]
  
# Function that multiplies
# 2 matrices F and M of size 2*2,
# and puts the multiplication
# result back to F[][]
def multiply(F, M):
      
    x = (F[0][0] * M[0][0] + 
         F[0][1] * M[1][0])
  
    y = (F[0][0] * M[0][1] +
         F[0][1] * M[1][1])
  
    z = (F[1][0] * M[0][0] + 
         F[1][1] * M[1][0])
  
    w = (F[1][0] * M[0][1] + 
         F[1][1] * M[1][1])
  
    F[0][0] = x
    F[0][1] = y
    F[1][0] = z
    F[1][1] = w
  
# Function that calculates F[][]
# raised to the power N
# and puts the result in F[][]
def power(F, N, m, n):
  
    M = [[ m, 1 ], [ n, 0 ]]
    for i in range(1, N + 1):
        multiply(F, M)
  
# Driver code
if __name__ == '__main__':
  
    N, a, b, m, n = 2, 0, 1, 2, 3
    print(F(N, a, b, m, n))
  
    N = 3
    print(F(N, a, b, m, n))
  
    N = 4
    print(F(N, a, b, m, n))
  
    N = 5
    print(F(N, a, b, m, n))
  
# This code is contributed by Shivam Singh

chevron_right


Output:

2
7
20
61

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : SHIVAMSINGH67, offbeat