Open In App

Python Program for Zeckendorf\’s Theorem (Non-Neighbouring Fibonacci Representation)

Last Updated : 20 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 

Python3




# Python program for Zeckendorf's theorem. It finds representation
# of n as sum of non-neighbouring Fibonacci Numbers.
 
# Returns the greatest Fibonacci Number 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,end=" ")
  
        # Reduce n
        n = n-f
 
# Driver code test above functions
n = 30
print ("Non-neighbouring Fibonacci Representation of", n, "is")
printFibRepresntation(n)


Output: 

Non-neighbouring Fibonacci Representation of 30 is
21 8 1

 

Please refer complete article on Zeckendorf’s Theorem (Non-Neighbouring Fibonacci Representation) for more details!
 



Similar Reads

Python Program to Convert String Matrix Representation to Matrix
Given a String with matrix representation, the task here is to write a python program that converts it to a matrix. Input : test_str = "[gfg,is],[best,for],[all,geeks]"Output : [['gfg', 'is'], ['best', 'for'], ['all', 'geeks']]Explanation : Required String Matrix is converted to Matrix with list as data type. Input : test_str = "[gfg,is],[for],[all
4 min read
Python Program for n-th Fibonacci number
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 With seed values F0 = 0 and F1 = 1.Python Program for n-th Fibonacci number Using recursion Here we will use recursion function. The code defines a function Fibonacci(n) that calculates the nth Fibonacci number recursively. It checks f
6 min read
Python Program to Print the Fibonacci sequence
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation. Fn = Fn-1 + Fn-2 with seed values : F0 = 0 and F1 = 1. Fibonacci Numbers using Native ApproachFibonacci series using a Pyth
3 min read
Python Program for How to check if a given number is Fibonacci number?
Given a number \'n\', how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .. Examples : Input: 8Output: Yes Input: 34Output: Yes Input: 41Output: No Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A n
2 min read
Fibonacci Series Program In Python Using Iterative Method
Fibonacci series is a series where each number is the sum of its two previous numbers. In this article, we are going to generate Fibonacci series in Python using Iterative methods. We will be covering both the loops i.e. for loop and while loop. In this article, we will be covering all the concepts related to the topic with clear and concise exampl
4 min read
Python Program to Display Fibonacci Sequence Using Recursion
We are given a task to write the Fibonacci sequence using recursion. we will take the range as input of integer and then print the Fibonacci Sequence. In this article, we will see the method of Python Program to Display Fibonacci Sequence Using Recursion. Example: Input: n = 9Output: 0 1 1 2 3 5 8 13 21Explanation: Here, we have n = 9, and we print
2 min read
Python Program for nth multiple of a number in Fibonacci Series
Given two integers n and k. Find position the nth multiple of K in the Fibonacci series. Examples: Input: k = 2, n = 3Output: 9, 3rd multiple of 2 in Fibonacci Series is 34 that appears at position 9. Input: k = 4, n = 5 Output: 30, 5th multiple of 4 in Fibonacci Series is 832040 which appears at position 30. An Efficient Solution is based on the b
5 min read
Check if Binary representation is Palindrome in Python
Given an integer ā€˜nā€™, write a Python function that returns true if binary representation of x is palindrome else return false. Examples: Input : n = 9 Output : True Binary representation of n=9 is 1001 which is palindrome as well. Input : n = 10 Output : False Binary representation of n=10 is 1010 which is not palindrome. We have existing solution
4 min read
Python Map | Length of the Longest Consecutive 1's in Binary Representation of a given integer
Given a number n, find length of the longest consecutive 1s in its binary representation. Examples: Input : n = 14 Output : 3 The binary representation of 14 is 1110. Input : n = 222 Output : 4 The binary representation of 222 is 11011110. We have existing solution for this problem please refer Length of the Longest Consecutive 1s in Binary Represe
3 min read
Python | Convert a string representation of list into list
Many times, we come across the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to a list in Python to perform tasks is quite common in web development. Convert a string of a list in
6 min read
Article Tags :