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.
Input: N = 2, a = 0, b = 1, c = 2, d = 3
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
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.
Below is the implementation of the above approach:
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.
- Sum of Fibonacci Numbers
- Non Fibonacci Numbers
- GCD and Fibonacci Numbers
- Even Fibonacci Numbers Sum
- Sum of Fibonacci Numbers in a range
- Alternate Fibonacci Numbers
- The Magic of Fibonacci Numbers
- Sum of squares of Fibonacci numbers
- Prime numbers and Fibonacci
- Find the sum of first N odd Fibonacci numbers
- Program for Fibonacci numbers
- Interesting facts about Fibonacci numbers
- Sum of Fibonacci Numbers with alternate negatives
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Program to print first n Fibonacci Numbers | Set 1
- Find the GCD of N Fibonacci Numbers with given Indices
- Large Fibonacci Numbers in Java
- Sum of numbers in the Kth level of a Fibonacci triangle
- Count of total subarrays whose sum is a Fibonacci Numbers
- Last digit of sum of numbers in the given range in the Fibonacci series
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.