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.
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++
#include <bits/stdc++.h>
using namespace std;
int nearestSmallerEqFib( int n)
{
if (n == 0 || n == 1)
return n;
int f1 = 0, f2 = 1, f3 = 1;
while (f3 <= n)
{
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return f2;
}
void printFibRepresntation( int n)
{
while (n > 0)
{
int f = nearestSmallerEqFib(n);
cout << f << " " ;
n = n - f;
}
}
int main()
{
int n = 30;
cout << "Non-neighbouring Fibonacci Representation of "
<< n << " is \n" ;
printFibRepresntation(n);
return 0;
}
|
Java
class GFG {
public static int nearestSmallerEqFib( int n)
{
if (n == 0 || n == 1 )
return n;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while (f3 <= n) {
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return f2;
}
public static void printFibRepresntation( int n)
{
while (n > 0 ) {
int f = nearestSmallerEqFib(n);
System.out.print(f + " " );
n = n - f;
}
}
public static void main(String[] args)
{
int n = 30 ;
System.out.println( "Non-neighbouring Fibonacci "
+ " Representation of " + n + " is" );
printFibRepresntation(n);
}
}
|
Python3
def nearestSmallerEqFib(n):
if (n = = 0 or n = = 1 ):
return n
f1, f2, f3 = 0 , 1 , 1
while (f3 < = n):
f1 = f2;
f2 = f3;
f3 = f1 + f2;
return f2;
def printFibRepresntation(n):
while (n> 0 ):
f = nearestSmallerEqFib(n);
print (f,end = " " )
n = n - f
n = 30
print ( "Non-neighbouring Fibonacci Representation of" , n, "is" )
printFibRepresntation(n)
|
C#
using System;
class GFG {
public static int nearestSmallerEqFib( int n)
{
if (n == 0 || n == 1)
return n;
int f1 = 0, f2 = 1, f3 = 1;
while (f3 <= n) {
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return f2;
}
public static void printFibRepresntation( int n)
{
while (n > 0) {
int f = nearestSmallerEqFib(n);
Console.Write(f + " " );
n = n - f;
}
}
public static void Main()
{
int n = 40;
Console.WriteLine( "Non-neighbouring Fibonacci "
+ " Representation of " + n + " is" );
printFibRepresntation(n);
}
}
|
PHP
<?php
function nearestSmallerEqFib( $n )
{
if ( $n == 0 || $n ==1)
return $n ;
$f1 = 0;
$f2 = 1;
$f3 = 1;
while ( $f3 <= $n )
{
$f1 = $f2 ;
$f2 = $f3 ;
$f3 = $f1 + $f2 ;
}
return $f2 ;
}
function printFibRepresntation( $n )
{
while ( $n > 0)
{
$f = nearestSmallerEqFib( $n );
echo $f , " " ;
$n = $n - $f ;
}
}
$n = 30;
echo "Non-neighbouring Fibonacci Representation of " ,
$n , " is \n" ;
printFibRepresntation( $n );
?>
|
Javascript
<script>
function nearestSmallerEqFib(n)
{
if (n == 0 || n==1)
return n;
let f1 = 0;
let f2 = 1;
let f3 = 1;
while (f3 <= n)
{
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return f2;
}
function printFibRepresntation(n)
{
while (n > 0)
{
let f = nearestSmallerEqFib(n);
document.write(f, " " );
n = n - f;
}
}
let n = 30;
document.write( "Non-neighbouring Fibonacci Representation of " +
n + " is <br>" );
printFibRepresntation(n);
</script>
|
Output
Non-neighbouring Fibonacci Representation of 30 is
21 8 1
Time Complexity: O(N*LogN)
Auxiliary Space: O(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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Nov, 2022
Like Article
Save Article