Zeckendorf’s theorem states that every positive Every positive integer can be written uniquely as a sum of distinct non-neighbouring Fibonacci numbers. Two Fibonacci numbers are neighbours if they one come after other in Fibonacci Sequence (0, 1, 1, 2, 3, 5, ..). For example, 3 and 5 are neighbours, but 2 and 5 are not.

Given a number, find a representation of number as sum of non-consecutive Fibonacci numbers.

Examples:

Input: n = 10 Output: 8 2 8 and 2 are two non-consecutive Fibonacci Numbers and sum of them is 10. Input: n = 30 Output: 21 8 1 21, 8 and 1 are non-consecutive Fibonacci Numbers and sum of them is 30.

**We strongly recommend you to minimize your browser and try this yourself first.**

The idea is to use Greedy Algorithm.

1) Let n be input number 2) While n >= 0 a) Find the greatest Fibonacci Number smaller than n. Let this number be 'f'. Print 'f' b) n = n - f

## C/C++

// C++ program for Zeckendorf's theorem. It finds representation // of n as sum of non-neighbouring Fibonacci Numbers. #include <bits/stdc++.h> using namespace std; // Returns the greatest Fibonacci Numberr smaller than // or equal to n. int nearestSmallerEqFib(int n) { // Corner cases if (n == 0 || n==1) return n; // Find the greatest Fibonacci Number smaller // than n. int f1 = 0, f2 = 1, f3 = 1; while (f3 <= n) { f1 = f2; f2 = f3; f3 = f1+f2; } return f2; } // Prints Fibonacci Representation of n using // greedy algorithm void printFibRepresntation(int n) { while (n>0) { // Find the greates Fibonacci Number smaller // than or equal to n int f = nearestSmallerEqFib(n); // Print the found fibonacci number cout << f << " "; // Reduce n n = n-f; } } // Driver method to test int main() { int n = 30; cout << "Non-neighbouring Fibonacci Representation of " << n << " is \n"; printFibRepresntation(n); return 0; }

## Python

# Python program for Zeckendorf's theorem. It finds # representation of n as sum of non-neighbouring # Fibonacci Numbers. # Returns the greatest Fibonacci Numberr smaller than # or equal to n. def nearestSmallerEqFib(n): # Corner cases if (n == 0 or n == 1): return n # Finds the greatest Fibonacci Number smaller # than n. f1,f2,f3 = 0,1,1 while (f3 <= n): f1 = f2; f2 = f3; f3 = f1+f2; return f2; # Prints Fibonacci Representation of n using # greedy algorithm def printFibRepresntation(n): while (n>0): # Find the greates Fibonacci Number smaller # than or equal to n f = nearestSmallerEqFib(n); # Print the found fibonacci number print f, # Reduce n n = n-f # Driver code test above functions n =30 print "Non-neighbouring Fibonacci Representation of", n, "is" printFibRepresntation(n)

## Java

// Java program for Zeckendorf's theorem. It finds // representation of n as sum of non-neighbouring // Fibonacci Numbers. class GFG { public static int nearestSmallerEqFib(int n) { // Corner cases if (n == 0 || n==1) return n; // Find the greatest Fibonacci Number smaller // than n. int f1 = 0, f2 = 1, f3 = 1; while (f3 <= n) { f1 = f2; f2 = f3; f3 = f1+f2; } return f2; } // Prints Fibonacci Representation of n using // greedy algorithm public static void printFibRepresntation(int n) { while (n>0) { // Find the greates Fibonacci Number smaller // than or equal to n int f = nearestSmallerEqFib(n); // Print the found fibonacci number System.out.print(f+" "); // Reduce n n = n-f; } } // Driver method to test public static void main(String[] args) { int n = 30; System.out.println("Non-neighbouring Fibonacci " + " Representation of " + n + " is"); printFibRepresntation(n); } } // Code Contributed by Mohit Gupta_OMG

**Output:**

Non-neighbouring Fibonacci Representation of 30 is 21 8 1

**How does above Greedy Algorithm work?**

Let the greatest Fibonacci number smaller than or equal to ‘n’ be fib(i) [i’th Fibonacci Number].

Then n – fib(i) will have its own representation as sum of non-neighbouring Fibonacci numbers.

All we want to make sure is that there is no neighbouring problem. By induction, n-fib(i) does not have neighbouring problem, then the only way n could have a neighbouring problem is if n-fib(i) uses fib(i-1) in its representation.

So all we have to further prove is that n-fib(i) does not use fib(i-1) in its representation

Let us prove it using contradiction. If n-fib(i) = fib(i-1) + fib(i-x) +…, then fib(i) cannot be the closest smallest Fibonacci number to n, since fib(i) + fib(i-1) itself is fib(i+1).

So if n-fib(i) contains fib(i-1) in its representation then fib(i+1) would be closer smaller fib number to n, contradicting our assumption that fib(i) is the closest smaller fib number to n.

**Can this representation be useful?**

Like Binary Representation. This can be an alternate representation to represent positive numbers. One important observation about this representation is, number of 1’s in the Fibonacci representation tends to be much less than the number of 1’s in the binary representation. Hence if in any application where it is more costly to store a 1 than to store a 0, it would make sense to use the fibonacci representation.

This article is contributed by **Gaurav Saxena**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above