Given two numbers N and K. Find the number of ways to represent N as the sum of K Fibonacci numbers.
Input : n = 12, k = 1 Output : 0 Input : n = 13, k = 3 Output : 2 Explanation : 2 + 3 + 8, 3 + 5 + 5.
Approach: The Fibonacci series is f(0)=1, f(1)=2 and f(i)=f(i-1)+f(i-2) for i>1. Let’s suppose F(x, k, n) be the number of ways to form the sum x using exactly k numbers from f(0), f(1), …f(n-1). To find a recurrence for F(x, k, n), notice that there are two cases: whether f(n-1) in the sum or not.
- If f(n-1) is not in the sum, then x is formed as a sum using exactly k numbers from f(0), f(1), …, f(n-2).
- If f(n-1) is in the sum, then the remaining x-f(n-1) is formed using exactly k-1 numbers from f(0), f(1), …, f(n-1). (Notice that f(n-1) is still included because duplicate numbers are allowed.).
So the recurrence relation will be:
F(x, k, n)= F(x, k, n-1)+F(x-f(n-1), k-1, n)
- If k=0, then there are zero numbers from the series, so the sum can only be 0. Hence, F(0, 0, n)=1.
- F(x, 0, n)=0, if x is not equals to 0.
Also, there are other cases that make F(x, k, n)=0, like the following:
- If k>0 and x=0 because having at least one positive number must result in a positive sum.
- If k>0 and n=0 because there’s no possible choice of numbers left.
- If x<0 because there's no way to form a negative sum using a finite number of nonnegative numbers.
Below is the implementation of above approach:
# Python3 implementation of the above approach
# To store fibonacci numbers 42 second
# number in fibonacci series largest
# possible integer
fib =  * 43
# Function to generate fibonacci
fib = 1
fib = 2
for i in range(2, 43):
fib[i] = fib[i – 1] + fib[i – 2]
# Recursive function to return the
# number of ways
def rec(x, y, last):
# base condition
if y == 0:
if x == 0:
Sum, i = 0, last
# for recursive function call
while i >= 0 and fib[i] * y >= x:
if fib[i] > x:
i -= 1
Sum += rec(x – fib[i], y – 1, i)
i -= 1
# Driver code
if __name__ == “__main__”:
n, k = 13, 3
print(“Possible ways are:”, rec(n, k, 42))
# This code is contributed
# by Rituraj Jain
Possible ways are: 2
- Ways to represent a number as a sum of 1's and 2's
- Check if a M-th fibonacci number divides N-th fibonacci number
- Bell Numbers (Number of ways to Partition a Set)
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Different ways to represent N as sum of K non-zero integers
- Finding number of digits in n'th Fibonacci number
- How to check if a given number is Fibonacci number?
- Number of ways to get a given sum with n number of m-faced dices
- Nth Even Fibonacci Number
- Fibonacci number in an array
- n'th multiple of a number in Fibonacci Series
- Python Program for n-th Fibonacci number
- Count the nodes whose sum with X is a Fibonacci number
- Program to find last two digits of Nth Fibonacci number
- An efficient way to check whether n-th Fibonacci number is multiple of 10
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.