Zeckendorf’s theorem states that 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.
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
Non-neighbouring Fibonacci Representation of 30 is 21 8 1
Time Complexity : O(N*LogN)
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
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.