# Minimum Fibonacci terms with sum equal to K

Given a number k, find the required minimum number of Fibonacci terms whose sum equal to k. We can choose a Fibonacci number multiple times.

**Examples:**

Input : k = 4 Output : 2 Fibonacci term added twice that is 2 + 2 = 4. Other combinations are 1 + 1 + 2. 1 + 1 + 1 + 1 Among all cases first case has the minimum number of terms = 2. Input : k = 17 Output : 3

We can get any sum using Fibonacci numbers as 1 is a Fibonacci number. For example to get n, we can n times add 1. Here we need to to minimize the count of Fibonacci numbers that contribute to sum. So this problem is basically coin change problem with coins having fibonacci values. By taking some examples, we can notice that With Fibonacci coin values Greedy approach works.

Firstly we calculate Fibonacci terms till less than or equal to k. then start from the last term and keep subtracting that term from k until k >(nth term). Also along with this keep increasing the count of the number of terms.

When k < (nth Fibonacci term) move to next Fibonacci term which is less than or Equal to k. at last, print the value of count.

The stepwise algorithm is:

1. Find all Fibonacci Terms less than or equal to K. 2. Initialize count = 0. 3. j = Index of last calculated Fibonacci Term. 4. while K > 0 do: // Greedy step count += K / (fibo[j]) // Note that division // is repeated subtraction. K = K % (fibo[j]) j--; 5. Print count.

Below is the C++/java implementation of the above approach.

## C++

`// C++ code to find minimum number of fibonacci ` `// terms that sum to K. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate Fibonacci Terms ` `void` `calcFiboTerms(vector<` `int` `>& fiboTerms, ` `int` `K) ` `{ ` ` ` `int` `i = 3, nextTerm; ` ` ` `fiboTerms.push_back(0); ` ` ` `fiboTerms.push_back(1); ` ` ` `fiboTerms.push_back(1); ` ` ` ` ` `// Calculate all Fibonacci terms ` ` ` `// which are less than or equal to K. ` ` ` `while` `(1) { ` ` ` `nextTerm = fiboTerms[i - 1] + fiboTerms[i - 2]; ` ` ` ` ` `// If next term is greater than K ` ` ` `// do not push it in vector and return. ` ` ` `if` `(nextTerm > K) ` ` ` `return` `; ` ` ` ` ` `fiboTerms.push_back(nextTerm); ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Function to find the minimum number of ` `// Fibonacci terms having sum equal to K. ` `int` `findMinTerms(` `int` `K) ` `{ ` ` ` ` ` `// Vector to store Fibonacci terms. ` ` ` `vector<` `int` `> fiboTerms; ` ` ` ` ` `calcFiboTerms(fiboTerms, K); ` ` ` ` ` `int` `count = 0, j = fiboTerms.size() - 1; ` ` ` ` ` `// Subtract Fibonacci terms from sum K ` ` ` `// until sum > 0. ` ` ` `while` `(K > 0) { ` ` ` ` ` `// Divide sum K by j-th Fibonacci term to find ` ` ` `// how many terms it contribute in sum. ` ` ` `count += (K / fiboTerms[j]); ` ` ` `K %= (fiboTerms[j]); ` ` ` ` ` `j--; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `K = 17; ` ` ` ` ` `cout << findMinTerms(K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find minimum number of fibonacci terms ` `// that sum to k. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calaculate Fibonacci Terms ` ` ` `public` `static` `void` `calcFiboTerms(ArrayList<Integer> fiboterms, ` ` ` `int` `k) ` ` ` `{ ` ` ` `int` `i = ` `3` `, nextTerm = ` `0` `; ` ` ` ` ` `fiboterms.add(` `0` `); ` ` ` `fiboterms.add(` `1` `); ` ` ` `fiboterms.add(` `1` `); ` ` ` ` ` `// Calculate all Fibonacci terms ` ` ` `// which are less than or equal to k. ` ` ` `while` `(` `true` `) ` ` ` `{ ` ` ` `nextTerm = fiboterms.get(i - ` `1` `) + fiboterms.get(i - ` `2` `); ` ` ` ` ` `// If next term is greater than k ` ` ` `// do not add in arraylist and return. ` ` ` `if` `(nextTerm>k) ` ` ` `return` `; ` ` ` ` ` `fiboterms.add(nextTerm); ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to find the minimum number of ` ` ` `// Fibonacci terms having sum equal to k. ` ` ` `public` `static` `int` `fibMinTerms(` `int` `k) ` ` ` `{ ` ` ` `// ArrayList to store Fibonacci terms. ` ` ` `ArrayList<Integer> fiboterms = ` `new` `ArrayList<Integer>(); ` ` ` `calcFiboTerms(fiboterms,k); ` ` ` ` ` `int` `count = ` `0` `, j = fiboterms.size() - ` `1` `; ` ` ` ` ` `// Subtract Fibonacci terms from sum k ` ` ` `// until sum > 0. ` ` ` `while` `(k > ` `0` `) ` ` ` `{ ` ` ` `// Divide sum k by j-th Fibonacci term to find ` ` ` `// how many terms it contribute in sum. ` ` ` `count += (k / fiboterms.get(j)); ` ` ` `k %= (fiboterms.get(j)); ` ` ` ` ` `j--; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `k = ` `17` `; ` ` ` ` ` `System.out.println(fibMinTerms(k)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Akash Singh*/` |

*chevron_right*

*filter_none*

## Python3

# Python3 code to find minimum number

# of fibonacci terms that sum to K.

# Function to calculate Fibonacci Terms

def calcFiboTerms(fiboTerms, K):

i = 3

fiboTerms.append(0)

fiboTerms.append(1)

fiboTerms.append(1)

# Calculate all Fibonacci terms

# which are less than or equal to K.

while True:

nextTerm = (fiboTerms[i – 1] +

fiboTerms[i – 2])

# If next term is greater than K

# do not push it in vector and return.

if nextTerm > K:

return

fiboTerms.append(nextTerm)

i += 1

# Function to find the minimum number of

# Fibonacci terms having sum equal to K.

def findMinTerms(K):

# Vector to store Fibonacci terms.

fiboTerms = []

calcFiboTerms(fiboTerms, K)

count, j = 0, len(fiboTerms) – 1

# Subtract Fibonacci terms from

# sum K until sum > 0.

while K > 0:

# Divide sum K by j-th Fibonacci

# term to find how many terms it

# contribute in sum.

count += K // fiboTerms[j]

K %= fiboTerms[j]

j -= 1

return count

# Driver code

if __name__ == “__main__”:

K = 17

print(findMinTerms(K))

# This code is contributed

# by Rituraj Jain

**Output:**

3

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Sum of Fibonacci numbers at even indexes upto N terms
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Make array elements equal in Minimum Steps
- Minimum increment by k operations to make all elements equal
- Minimum Increment / decrement to make array elements equal
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Check if a M-th fibonacci number divides N-th fibonacci number
- K- Fibonacci series
- Fibonacci Search
- GCD and Fibonacci Numbers
- Nth Even Fibonacci Number
- Even Fibonacci Numbers Sum

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.