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
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Fermat's Last Theorem
- Fermat's little theorem
- Midy's theorem
- Rosser's Theorem
- Nicomachu's Theorem
- Wilson's Theorem
- Lagrange's four square theorem
- Hardy-Ramanujan Theorem
- Compute nCr % p | Set 2 (Lucas Theorem)
- Corollaries of Binomial Theorem
- Extended Midy's theorem
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Euclid Euler Theorem
- Check if a M-th fibonacci number divides N-th fibonacci number
- Chinese Remainder Theorem | Set 1 (Introduction)
- Vantieghems Theorem for Primality Test
- An application on Bertrand's ballot theorem
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Orbit counting theorem or Burnside's Lemma