Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation) - GeeksforGeeks
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.
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.
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
// 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;
}
}
How does above Greedy Algorithm work?
Let the greatest Fibonacci number smaller than or equal to ‘n’ be fib(i) [i’th Fibonacci Number].
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.
Read full article from Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation) - GeeksforGeeksLike 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.