Minimum Fibonacci terms with sum equal to K (original) (raw)
Last Updated : 11 Feb, 2025
Given a number **k, the task is to find the minimum number of Fibonacci terms (including repetitions) whose sum equals **k.
**Note: You may use any Fibonacci number multiple times.
**Examples:
**Input: k = 7
**Output: 2
**Explanation: Possible ways to sum up to 7 using Fibonacci numbers:
5 + 2 = 7 (2 terms)
3 + 3 + 1 = 7 (3 terms)
2 + 2 + 2 + 1 = 7 (4 terms)
The minimum number of terms is 2, using 5 + 2.**Input: k = 13
**Output: 1
**Explanation: Possible ways to sum up to 13 using Fibonacci numbers:
13 = 13 (1 term)
8 + 5 = 13 (2 terms)
Using 13 directly gives the minimum number of terms, which is 1.
Like Binary Representation where we represent every number as sum of powers of 2, we can represent every every number as sum of Fibonacci Numbers. For example: 19 = 13+5+1. This is known as a Zeckendorf Representation. For example, to get n, we can n times add 1. Here we need 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.
Below is the implementation of the above approach:
C++ `
// C++ implementation to find the minimum number
// of Fibonacci terms required to sum up to k
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum Fibonacci terms int minimumFibonacciTerms(int k) { vector fib = {1, 1};
// Generate Fibonacci numbers up to k
while (fib.back() < k) {
fib.push_back(
fib[fib.size() - 1] +
fib[fib.size() - 2]
);
}
int count = 0;
// Start from the largest Fibonacci number
for (int i = fib.size() - 1;
i >= 0 && k > 0; i--) {
// Use the largest possible Fibonacci number
while (k >= fib[i]) {
k -= fib[i];
count++;
}
}
return count;
}
int main() {
int k = 7;
cout << minimumFibonacciTerms(k);
return 0;
}
Java
// Java implementation to find the minimum number // of Fibonacci terms required to sum up to k import java.util.ArrayList; import java.util.List;
class GfG {
// Function to find the minimum Fibonacci terms
static int minimumFibonacciTerms(int k) {
List<Integer> fib = new ArrayList<>();
fib.add(1);
fib.add(1);
// Generate Fibonacci numbers up to k
while (fib.get(fib.size() - 1) < k) {
fib.add(fib.get(fib.size() - 1) +
fib.get(fib.size() - 2));
}
int count = 0;
// Start from the largest Fibonacci number
for (int i = fib.size() - 1;
i >= 0 && k > 0; i--) {
// Use the largest possible Fibonacci number
while (k >= fib.get(i)) {
k -= fib.get(i);
count++;
}
}
return count;
}
public static void main(String[] args) {
int k = 7;
System.out.println(minimumFibonacciTerms(k));
}
}
Python
Python implementation to find the minimum number
of Fibonacci terms required to sum up to k
def minimumFibonacciTerms(k): fib = [1, 1]
# Generate Fibonacci numbers up to k
while fib[-1] < k:
fib.append(fib[-1] + fib[-2])
count = 0
# Start from the largest Fibonacci number
for i in range(len(fib) - 1, -1, -1):
# Use the largest possible Fibonacci number
while k >= fib[i]:
k -= fib[i]
count += 1
return count
if name == "main":
k = 7
print(minimumFibonacciTerms(k))
C#
// C# implementation to find the minimum number // of Fibonacci terms required to sum up to k using System; using System.Collections.Generic;
class GfG {
// Function to find the minimum Fibonacci terms
static int MinimumFibonacciTerms(int k) {
List<int> fib = new List<int>();
fib.Add(1);
fib.Add(1);
// Generate Fibonacci numbers up to k
while (fib[fib.Count - 1] < k) {
fib.Add(fib[fib.Count - 1] +
fib[fib.Count - 2]);
}
int count = 0;
// Start from the largest Fibonacci number
for (int i = fib.Count - 1;
i >= 0 && k > 0; i--) {
// Use the largest possible Fibonacci number
while (k >= fib[i]) {
k -= fib[i];
count++;
}
}
return count;
}
public static void Main(string[] args) {
int k = 7;
Console.WriteLine(MinimumFibonacciTerms(k));
}
}
JavaScript
// JavaScript implementation to find // the minimum number of Fibonacci // terms required to sum up to k
function minimumFibonacciTerms(k) { let fib = [1, 1];
// Generate Fibonacci numbers up to k
while (fib[fib.length - 1] < k) {
fib.push(fib[fib.length - 1]
+ fib[fib.length - 2]);
}
let count = 0;
// Start from the largest Fibonacci number
for (let i = fib.length - 1;
i >= 0 && k > 0; i--) {
// Use the largest possible Fibonacci number
while (k >= fib[i]) {
k -= fib[i];
count++;
}
}
return count;
}
const k = 7; console.log(minimumFibonacciTerms(k));
`
**Time Complexity: O(log k), as both generating and summing Fibonacci numbers takes O(log k)
**Space Complexity: O(log k), due to storing the Fibonacci sequence up to k.