Generalized Fibonacci Numbers extend the standard Fibonacci sequence by introducing four constants a, b, c, and d. The nth term is defined by the recurrence relation Gn = a*G(n-1) + b*G(n-2), with seed values G(0) = c and G(1) = d.
Examples:
Input: n = 2, a = 0, b = 1, c = 2, d = 3
Output: 2
Explanation: As a = 0 -> G(0) = 0
b = 1 -> G(1) = 1
So, G(2) = 2 * G(1) + 3 * G(0) = 2Input: n = 3, a = 0, b = 1, c = 2, d = 3
Output: 7
Table of Content
[Naive Approach] Recursion - O(2^n) Time and O(n) Space
- Directly apply the recurrence relation recursively
- For every n, recursively compute the previous two terms and multiply them with a and b respectively
- Base cases are the first term equals c and second term equals d
#include <iostream>
using namespace std;
int Fibonacci(int a, int b, int c, int d, int n) {
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Recurrence relation: G(n) = a*G(n-1) + b*G(n-2)
return a * Fibonacci(a, b, c, d, n - 1) +
b * Fibonacci(a, b, c, d, n - 2);
}
int main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
cout << Fibonacci(a, b, c, d, n) << endl;
return 0;
}
public class GfG {
static int Fibonacci(int a, int b, int c, int d, int n) {
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Recurrence relation: G(n) = a*G(n-1) + b*G(n-2)
return a * Fibonacci(a, b, c, d, n - 1) +
b * Fibonacci(a, b, c, d, n - 2);
}
public static void main(String[] args) {
int a = 1, b = 1, c = 0, d = 1, n = 6;
System.out.println(Fibonacci(a, b, c, d, n));
}
}
def Fibonacci(a, b, c, d, n):
# Base cases
if n == 1:
return c
if n == 2:
return d
# Recurrence relation: G(n) = a*G(n-1) + b*G(n-2)
return a * Fibonacci(a, b, c, d, n - 1) + \
b * Fibonacci(a, b, c, d, n - 2)
a, b, c, d, n = 1, 1, 0, 1, 6
print(Fibonacci(a, b, c, d, n))
using System;
class GFG {
static int Fibonacci(int a, int b, int c, int d, int n) {
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Recurrence relation: G(n) = a*G(n-1) + b*G(n-2)
return a * Fibonacci(a, b, c, d, n - 1) +
b * Fibonacci(a, b, c, d, n - 2);
}
static void Main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
Console.WriteLine(Fibonacci(a, b, c, d, n));
}
}
function Fibonacci(a, b, c, d, n) {
// Base cases
if (n === 1)
return c;
if (n === 2)
return d;
// Recurrence relation: G(n) = a*G(n-1) + b*G(n-2)
return a * Fibonacci(a, b, c, d, n - 1) +
b * Fibonacci(a, b, c, d, n - 2);
}
//driver code
let a = 1, b = 1, c = 0, d = 1, n = 6;
console.log(Fibonacci(a, b, c, d, n));
Output
5
[Efficient Approach] DP Tabulation - O(n) Time and O(n) Space
- Create a DP array of size n+1 to store all terms
- Initialize base cases G(1) = c and G(2) = d
- Fill the DP array bottom up using recurrence relation G(n) = aG(n-1) + bG(n-2)
- Return the nth term from the DP array
#include <iostream>
#include <vector>
using namespace std;
int Fibonacci(int a, int b, int c, int d, int n) {
// DP array to store all terms till n
vector<int> dp(n + 1);
// Base cases
dp[1] = c;
dp[2] = d;
// Fill DP array bottom up
for (int i = 3; i <= n; i++)
dp[i] = a * dp[i - 1] + b * dp[i - 2];
return dp[n];
}
int main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
cout << Fibonacci(a, b, c, d, n) << endl;
return 0;
}
import java.util.ArrayList;
public class GfG {
static int Fibonacci(int a, int b, int c, int d, int n) {
// DP array to store all terms till n
int[] dp = new int[n + 1];
// Base cases
dp[1] = c;
dp[2] = d;
// Fill DP array bottom up
for (int i = 3; i <= n; i++)
dp[i] = a * dp[i - 1] + b * dp[i - 2];
return dp[n];
}
public static void main(String[] args) {
int a = 1, b = 1, c = 0, d = 1, n = 6;
System.out.println(Fibonacci(a, b, c, d, n));
}
}
def Fibonacci(a, b, c, d, n):
# DP array to store all terms till n
dp = [0] * (n + 1)
# Base cases
dp[1] = c
dp[2] = d
# Fill DP array bottom up
for i in range(3, n + 1):
dp[i] = a * dp[i - 1] + b * dp[i - 2]
return dp[n]
a, b, c, d, n = 1, 1, 0, 1, 6
print(Fibonacci(a, b, c, d, n))
using System;
class GFG {
static int Fibonacci(int a, int b, int c, int d, int n) {
// DP array to store all terms till n
int[] dp = new int[n + 1];
// Base cases
dp[1] = c;
dp[2] = d;
// Fill DP array bottom up
for (int i = 3; i <= n; i++)
dp[i] = a * dp[i - 1] + b * dp[i - 2];
return dp[n];
}
static void Main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
Console.WriteLine(Fibonacci(a, b, c, d, n));
}
}
class GFG {
Fibonacci(a, b, c, d, n) {
// DP array to store all terms till N
let dp = new Array(n + 1).fill(0);
// Base cases
dp[1] = c;
dp[2] = d;
// Fill DP array bottom up
for (let i = 3; i <= n; i++)
dp[i] = a * dp[i - 1] + b * dp[i - 2];
return dp[n];
}
}
//driver code
let a = 1, b = 1, c = 0, d = 1, n = 6;
let obj = new GFG();
console.log(obj.Fibonacci(a, b, c, d, n));
Output
5
[Efficient Approach] Matrix Exponentiation - O(n) Time and O(1) Space
- Represent the recurrence relation Gn = m*G(n-1) + n*G(n-2) as a 2×2 transformation matrix {{m, 1}, {n, 0}} to compute the nth term efficiently.
- The initial matrix is set up using base cases G(0) = a and G(1) = b, and the transformation matrix is raised to power (n-2) using a loop.
- A helper multiply function performs standard 2×2 matrix multiplication using dot product of rows and columns.
- After computing the required matrix power, it is multiplied with the initial matrix to get the final result.
- The top-left element of the resultant matrix gives the nth Generalized Fibonacci Number.
#include <iostream>
using namespace std;
void multiply(int F[2][2], int M[2][2]) {
// Multiply two 2x2 matrices and store result in F
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;
}
void power(int F[2][2], int n, int a, int b) {
// Raise transformation matrix F to the power n
int M[2][2] = { { a, 1 }, { b, 0 } };
for (int i = 1; i <= n; i++)
multiply(F, M);
}
int Fibonacci(int a, int b, int c, int d, int n) {
// Transformation matrix
int F[2][2] = { { a, 1 }, { b, 0 } };
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Initial matrix using base case values
int initial[2][2] = { { a * d + b * c, d }, { d, c } };
// Raise transformation matrix to power n-2
power(F, n - 2, a, b);
// Multiply initial matrix with result
multiply(initial, F);
// Top left element is the nth term
return initial[0][0];
}
int main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
cout << Fibonacci(a, b, c, d, n) << endl;
return 0;
}
public class Main {
static void multiply(int[][] F, int[][] M) {
// Multiply two 2x2 matrices and store result in F
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;
}
static void power(int[][] F, int n, int a, int b) {
// Raise transformation matrix F to the power n
int[][] M = { { a, 1 }, { b, 0 } };
for (int i = 1; i <= n; i++)
multiply(F, M);
}
static int Fibonacci(int a, int b, int c, int d, int n) {
// Transformation matrix
int[][] F = { { a, 1 }, { b, 0 } };
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Initial matrix using base case values
int[][] initial = { { a * d + b * c, d }, { d, c } };
// Raise transformation matrix to power n-2
power(F, n - 2, a, b);
// Multiply initial matrix with result
multiply(initial, F);
// Top left element is the Nth term
return initial[0][0];
}
public static void main(String[] args) {
int a = 1, b = 1, c = 0, d = 1, n = 6;
System.out.println(Fibonacci(a, b, c, d, n));
}
}
def multiply(F, M):
# Multiply two 2x2 matrices and store result in F
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
def power(F, n, a, b):
# Raise transformation matrix F to the power N
M = [[a, 1], [b, 0]]
for i in range(1, n + 1):
multiply(F, M)
def Fibonacci(a, b, c, d, n):
# Transformation matrix
F = [[a, 1], [b, 0]]
# Base cases
if n == 1:
return c
if n == 2:
return d
# Initial matrix using base case values
initial = [[a * d + b * c, d], [d, c]]
# Raise transformation matrix to power n-2
power(F, n - 2, a, b)
# Multiply initial matrix with result
multiply(initial, F)
# Top left element is the Nth term
return initial[0][0]
a, b, c, d, n = 1, 1, 0, 1, 6
print(Fibonacci(a, b, c, d, n))
using System;
class GFG {
static void multiply(int[,] F, int[,] M) {
// Multiply two 2x2 matrices and store result in F
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;
}
static void power(int[,] F, int n, int a, int b) {
// Raise transformation matrix F to the power n
int[,] M = { { a, 1 }, { b, 0 } };
for (int i = 1; i <= n; i++)
multiply(F, M);
}
static int Fibonacci(int a, int b, int c, int d, int n) {
// Transformation matrix
int[,] F = { { a, 1 }, { b, 0 } };
// Base cases
if (n == 1)
return c;
if (n == 2)
return d;
// Initial matrix using base case values
int[,] initial = { { a * d + b * c, d }, { d, c } };
// Raise transformation matrix to power n-2
power(F, n - 2, a, b);
// Multiply initial matrix with result
multiply(initial, F);
// Top left element is the nth term
return initial[0, 0];
}
static void Main() {
int a = 1, b = 1, c = 0, d = 1, n = 6;
Console.WriteLine(Fibonacci(a, b, c, d, n));
}
}
function multiply(F, M) {
// Multiply two 2x2 matrices and store result in F
let x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
let y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
let z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
let 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 power(F, n, a, b) {
// Raise transformation matrix F to the power n
let M = [[a, 1], [b, 0]];
for (let i = 1; i <= n; i++)
multiply(F, M);
}
function Fibonacci(a, b, c, d, n) {
// Transformation matrix
let F = [[a, 1], [b, 0]];
// Base cases
if (n === 1)
return c;
if (n === 2)
return d;
// Initial matrix using base case values
let initial = [[a * d + b * c, d], [d, c]];
// Raise transformation matrix to power n-2
power(F, n - 2, a, b);
// Multiply initial matrix with result
multiply(initial, F);
// Top left element is the nth term
return initial[0][0];
}
// drive code
let a = 1, b = 1, c = 0, d = 1, n = 6;
console.log(Fibonacci(a, b, c, d, n));
Output
13