Fibonacci coding encodes an integer into binary number using Fibonacci Representation of the number. The idea is based on Zeckendorf’s Theorem which states that every positive integer can be written uniquely as a sum of distinct non-neighboring Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..).
The Fibonacci code word for a particular integer is exactly the integer's Zeckendorf representation with the order of its digits reversed and an additional "1" appended to the end. The extra 1 is appended to indicate the end of code (Note that the code never contains two consecutive 1s as per Zeckendorf’s Theorem. The representation uses Fibonacci numbers starting from 1 (2'nd Fibonacci Number). So the Fibonacci Numbers used are 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, …….
Given a number n, print its Fibonacci code.
Examples:
Input: n = 1 Output: 11 1 is first Fibonacci number in this representation and an extra 1 is appended at the end. Input: n = 11 Output: 001011 11 is sum of 8 and 3. The last 1 represents extra 1 that is always added. A 1 before it represents 8. The third 1 (from beginning) represents 3.
We strongly recommend you to minimize your browser and try this yourself first.
The following algorithm takes an integer as input and generates a string that stores Fibonacci Encoding.
Find the largest Fibonacci number f less than or equal to n. Say it is the i'th number in the Fibonacci series. The length of codeword for n will be i+3 characters (One for extra 1 appended at the end, One because i is an index, and one for '\0'). Assuming that the Fibonacci series is stored:
- Let f be the largest Fibonacci less than or equal to n, prepend ‘1’ in the binary string. This indicates usage of f in representation for n. Subtract f from n: n = n - f
- Else if f is greater than n, prepend ‘0’ to the binary string.
- Move to the Fibonacci number just smaller than f .
- Repeat until zero remainder (n = 0)
- Append an additional ‘1’ to the binary string. We obtain an encoding such that two consecutive 1s indicate the end of a number (and the start of the next).
Below is the implementation of above algorithm.
C++ /* C++ program for Fibonacci Encoding of a positive integer n */ #include <bits/stdc++.h> using namespace std; // To limit on the largest Fibonacci number to be used #define N 30 /* Array to store fibonacci numbers. fib[i] is going to store (i+2)'th Fibonacci number*/ int fib[N]; // Stores values in fib and returns index of the largest // fibonacci number smaller than n. int largestFiboLessOrEqual(int n) { fib[0] = 1; // Fib[0] stores 2nd Fibonacci No. fib[1] = 2; // Fib[1] stores 3rd Fibonacci No. // Keep Generating remaining numbers while previously // generated number is smaller int i; for (i=2; fib[i-1]<=n; i++) fib[i] = fib[i-1] + fib[i-2]; // Return index of the largest fibonacci number // smaller than or equal to n. Note that the above // loop stopped when fib[i-1] became larger. return (i-2); } /* Returns pointer to the char string which corresponds to code for n */ char* fibonacciEncoding(int n) { int index = largestFiboLessOrEqual(n); //allocate memory for codeword char *codeword = (char*)malloc(sizeof(char)*(index+3)); // index of the largest Fibonacci f <= n int i = index; while (n) { // Mark usage of Fibonacci f (1 bit) codeword[i] = '1'; // Subtract f from n n = n - fib[i]; // Move to Fibonacci just smaller than f i = i - 1; // Mark all Fibonacci > n as not used (0 bit), // progress backwards while (i>=0 && fib[i]>n) { codeword[i] = '0'; i = i - 1; } } //additional '1' bit codeword[index+1] = '1'; codeword[index+2] = '\0'; //return pointer to codeword return codeword; } /* driver function */ int main() { int n = 143; cout <<"Fibonacci code word for " <<n <<" is " << fibonacciEncoding(n); return 0; } // This code is contributed by shivanisinghss2110
C /* C program for Fibonacci Encoding of a positive integer n */ #include<stdio.h> #include<stdlib.h> // To limit on the largest Fibonacci number to be used #define N 30 /* Array to store fibonacci numbers. fib[i] is going to store (i+2)'th Fibonacci number*/ int fib[N]; // Stores values in fib and returns index of the largest // fibonacci number smaller than n. int largestFiboLessOrEqual(int n) { fib[0] = 1; // Fib[0] stores 2nd Fibonacci No. fib[1] = 2; // Fib[1] stores 3rd Fibonacci No. // Keep Generating remaining numbers while previously // generated number is smaller int i; for (i=2; fib[i-1]<=n; i++) fib[i] = fib[i-1] + fib[i-2]; // Return index of the largest fibonacci number // smaller than or equal to n. Note that the above // loop stopped when fib[i-1] became larger. return (i-2); } /* Returns pointer to the char string which corresponds to code for n */ char* fibonacciEncoding(int n) { int index = largestFiboLessOrEqual(n); //allocate memory for codeword char *codeword = (char*)malloc(sizeof(char)*(index+3)); // index of the largest Fibonacci f <= n int i = index; while (n) { // Mark usage of Fibonacci f (1 bit) codeword[i] = '1'; // Subtract f from n n = n - fib[i]; // Move to Fibonacci just smaller than f i = i - 1; // Mark all Fibonacci > n as not used (0 bit), // progress backwards while (i>=0 && fib[i]>n) { codeword[i] = '0'; i = i - 1; } } //additional '1' bit codeword[index+1] = '1'; codeword[index+2] = '\0'; //return pointer to codeword return codeword; } /* driver function */ int main() { int n = 143; printf("Fibonacci code word for %d is %s\n", n, fibonacciEncoding(n)); return 0; }
Java // Java program for Fibonacci Encoding // of a positive integer n import java.io.*; class GFG{ // To limit on the largest Fibonacci // number to be used public static int N = 30; // Array to store fibonacci numbers. // fib[i] is going to store (i+2)'th // Fibonacci number public static int[] fib = new int[N]; // Stores values in fib and returns index of // the largest fibonacci number smaller than n. public static int largestFiboLessOrEqual(int n) { // Fib[0] stores 2nd Fibonacci No. fib[0] = 1; // Fib[1] stores 3rd Fibonacci No. fib[1] = 2; // Keep Generating remaining numbers while // previously generated number is smaller int i; for(i = 2; fib[i - 1] <= n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } // Return index of the largest fibonacci // number smaller than or equal to n. // Note that the above loop stopped when // fib[i-1] became larger. return(i - 2); } // Returns pointer to the char string which // corresponds to code for n public static String fibonacciEncoding(int n) { int index = largestFiboLessOrEqual(n); // Allocate memory for codeword char[] codeword = new char[index + 3]; // Index of the largest Fibonacci f <= n int i = index; while (n > 0) { // Mark usage of Fibonacci f(1 bit) codeword[i] = '1'; // Subtract f from n n = n - fib[i]; // Move to Fibonacci just smaller than f i = i - 1; // Mark all Fibonacci > n as not used // (0 bit), progress backwards while (i >= 0 && fib[i] > n) { codeword[i] = '0'; i = i - 1; } } // Additional '1' bit codeword[index + 1] = '1'; codeword[index + 2] = '\0'; String string = new String(codeword); // Return pointer to codeword return string; } // Driver code public static void main(String[] args) { int n = 143; System.out.println("Fibonacci code word for " + n + " is " + fibonacciEncoding(n)); } } // This code is contributed by avanitrachhadiya2155
Python3 # Python3 program for Fibonacci Encoding # of a positive integer n # To limit on the largest # Fibonacci number to be used N = 30 # Array to store fibonacci numbers. # fib[i] is going to store # (i+2)'th Fibonacci number fib = [0 for i in range(N)] # Stores values in fib and returns index of # the largest fibonacci number smaller than n. def largestFiboLessOrEqual(n): fib[0] = 1 # Fib[0] stores 2nd Fibonacci No. fib[1] = 2 # Fib[1] stores 3rd Fibonacci No. # Keep Generating remaining numbers while # previously generated number is smaller i = 2 while fib[i - 1] <= n: fib[i] = fib[i - 1] + fib[i - 2] i += 1 # Return index of the largest fibonacci number # smaller than or equal to n. Note that the above # loop stopped when fib[i-1] became larger. return (i - 2) # Returns pointer to the char string which # corresponds to code for n def fibonacciEncoding(n): index = largestFiboLessOrEqual(n) # allocate memory for codeword codeword = ['a' for i in range(index + 2)] # index of the largest Fibonacci f <= n i = index while (n): # Mark usage of Fibonacci f (1 bit) codeword[i] = '1' # Subtract f from n n = n - fib[i] # Move to Fibonacci just smaller than f i = i - 1 # Mark all Fibonacci > n as not used (0 bit), # progress backwards while (i >= 0 and fib[i] > n): codeword[i] = '0' i = i - 1 # additional '1' bit codeword[index + 1] = '1' # return pointer to codeword return "".join(codeword) # Driver Code n = 143 print("Fibonacci code word for", n, "is", fibonacciEncoding(n)) # This code is contributed by Mohit Kumar
C# // C# program for Fibonacci Encoding // of a positive integer n using System; class GFG{ // To limit on the largest Fibonacci // number to be used public static int N = 30; // Array to store fibonacci numbers. // fib[i] is going to store (i+2)'th // Fibonacci number public static int[] fib = new int[N]; // Stores values in fib and returns index of // the largest fibonacci number smaller than n. public static int largestFiboLessOrEqual(int n) { // Fib[0] stores 2nd Fibonacci No. fib[0] = 1; // Fib[1] stores 3rd Fibonacci No. fib[1] = 2; // Keep Generating remaining numbers while // previously generated number is smaller int i; for(i = 2; fib[i - 1] <= n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } // Return index of the largest fibonacci // number smaller than or equal to n. // Note that the above loop stopped when // fib[i-1] became larger. return(i - 2); } // Returns pointer to the char string which // corresponds to code for n public static String fibonacciEncoding(int n) { int index = largestFiboLessOrEqual(n); // Allocate memory for codeword char[] codeword = new char[index + 3]; // Index of the largest Fibonacci f <= n int i = index; while (n > 0) { // Mark usage of Fibonacci f(1 bit) codeword[i] = '1'; // Subtract f from n n = n - fib[i]; // Move to Fibonacci just smaller than f i = i - 1; // Mark all Fibonacci > n as not used // (0 bit), progress backwards while (i >= 0 && fib[i] > n) { codeword[i] = '0'; i = i - 1; } } // Additional '1' bit codeword[index + 1] = '1'; codeword[index + 2] = '\0'; string str = new string(codeword); // Return pointer to codeword return str; } // Driver code static public void Main() { int n = 143; Console.WriteLine("Fibonacci code word for " + n + " is " + fibonacciEncoding(n)); } } // This code is contributed by rag2127
JavaScript <script> // Javascript program for Fibonacci Encoding // of a positive integer n // To limit on the largest Fibonacci // number to be used let N = 30; // Array to store fibonacci numbers. // fib[i] is going to store (i+2)'th // Fibonacci number let fib = new Array(N); // Stores values in fib and returns index of // the largest fibonacci number smaller than n. function largestFiboLessOrEqual(n) { // Fib[0] stores 2nd Fibonacci No. fib[0] = 1; // Fib[1] stores 3rd Fibonacci No. fib[1] = 2; // Keep Generating remaining numbers while // previously generated number is smaller let i; for(i = 2; fib[i - 1] <= n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } // Return index of the largest fibonacci // number smaller than or equal to n. // Note that the above loop stopped when // fib[i-1] became larger. return(i - 2); } // Returns pointer to the char string which // corresponds to code for n function fibonacciEncoding(n) { let index = largestFiboLessOrEqual(n); // Allocate memory for codeword let codeword = new Array(index + 3); // Index of the largest Fibonacci f <= n let i = index; while (n > 0) { // Mark usage of Fibonacci f(1 bit) codeword[i] = '1'; // Subtract f from n n = n - fib[i]; // Move to Fibonacci just smaller than f i = i - 1; // Mark all Fibonacci > n as not used // (0 bit), progress backwards while (i >= 0 && fib[i] > n) { codeword[i] = '0'; i = i - 1; } } // Additional '1' bit codeword[index + 1] = '1'; codeword[index + 2] = '\0'; let string =(codeword).join(""); // Return pointer to codeword return string; } // Driver code let n = 143; document.write("Fibonacci code word for " + n + " is " + fibonacciEncoding(n)); // This code is contributed by unknown2108 </script>
Output:
Fibonacci code word for 143 is 01010101011
Time complexity :- O(N)
Space complexity :- O(N+K)
Illustration

Field of application:
Data Processing & Compression - representing the data (which can be text, image, video...) in such a way that the space needed to store or transmit data is less than the size of input data. Statistical methods use variable-length codes, with the shorter codes assigned to symbols or group of symbols that have a higher probability of occurrence. If the codes are to be used over a noisy communication channel, their resilience to bit insertions, deletions and to bit-flips is of high importance.
Read more about the application here.
Similar Reads
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 : YesInput : 34Output : YesInput : 41Output : NoApproach 1:A simple way is to generate Fibonacci numbers until the generated number
15 min read
Nth Fibonacci Number Given a positive integer n, the task is to find the nth Fibonacci number.The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21Example:Input:
15+ min read
C++ Program For Fibonacci Numbers The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1, and they are used to generate the entire series.Examples:Input: 5Output: 5Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5
5 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-2With seed values F0 = 0 and F1 = 1.Table of ContentPython Program for n-th Fibonacci number Using Formula Python Program for n-th Fibonacci number Using RecursionPython Program for n-th
6 min read
Interesting Programming facts about Fibonacci numbers We know Fibonacci number, Fn = Fn-1 + Fn-2. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, .... . Here are some interesting facts about Fibonacci number : 1. Pattern in Last digits of Fibonacci numbers : Last digits of first few Fibonacci Numbers ar
15+ min read
Find nth Fibonacci number using Golden ratio Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ........Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. Golden ratio: \varphi ={\fr
6 min read
Fast Doubling method to find the Nth Fibonacci number Given an integer N, the task is to find the N-th Fibonacci numbers.Examples: Input: N = 3 Output: 2 Explanation: F(1) = 1, F(2) = 1 F(3) = F(1) + F(2) = 2 Input: N = 6 Output: 8 Approach: The Matrix Exponentiation Method is already discussed before. The Doubling Method can be seen as an improvement
14 min read
Tail Recursion for Fibonacci Write a tail recursive function for calculating the n-th Fibonacci number. Examples : Input : n = 4 Output : fib(4) = 3 Input : n = 9 Output : fib(9) = 34 Prerequisites : Tail Recursion, Fibonacci numbersA recursive function is tail recursive when the recursive call is the last thing executed by the
4 min read
Sum of Fibonacci Numbers Given a number positive number n, find value of f0 + f1 + f2 + .... + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, ... Examples : Input : n = 3Output : 4Explanation : 0 + 1 + 1 + 2 = 4Input : n = 4Output : 7Explanation : 0 + 1 + 1 + 2 + 3
9 min read
Fibonacci Series
Program to Print Fibonacci SeriesEver wondered about the cool math behind the Fibonacci series? This simple pattern has a remarkable presence in nature, from the arrangement of leaves on plants to the spirals of seashells. We're diving into this Fibonacci Series sequence. It's not just math, it's in art, nature, and more! Let's dis
8 min read
Program to Print Fibonacci Series in JavaThe Fibonacci series is a series of elements where the previous two elements are added to generate the next term. It starts with 0 and 1, for example, 0, 1, 1, 2, 3, and so on. We can mathematically represent it in the form of a function to generate the n'th Fibonacci number because it follows a con
5 min read
Print the Fibonacci sequence - PythonTo print the Fibonacci sequence in Python, we need to generate a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The Fibonacci sequence follows a specific pattern that begins with 0 and 1, and every subsequent number is the sum of the two previous num
5 min read
C Program to Print Fibonacci SeriesThe Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series.ExampleInput: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.In
4 min read
JavaScript Program to print Fibonacci SeriesThe Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The recurrence relation defines the sequence Fn of Fibonacci numbers:Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1Examples:Input : 5
4 min read
Length of longest subsequence of Fibonacci Numbers in an ArrayGiven an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9,
5 min read
Last digit of sum of numbers in the given range in the Fibonacci seriesGiven two non-negative integers M, N which signifies the range [M, N] where M ? N, the task is to find the last digit of the sum of FM + FM+1... + FN where FK is the Kth Fibonacci number in the Fibonacci series. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Examples: Input: M = 3, N = 9 Output:
5 min read
K- Fibonacci seriesGiven integers 'K' and 'N', the task is to find the Nth term of the K-Fibonacci series. In K - Fibonacci series, the first 'K' terms will be '1' and after that every ith term of the series will be the sum of previous 'K' elements in the same series. Examples: Input: N = 4, K = 2 Output: 3 The K-Fibo
7 min read
Fibonacci Series in BashPrerequisite: Fibonacci Series Write a program to print the Fibonacci sequence up to nth digit using Bash. Examples: Input : 5 Output : Fibonacci Series is : 0 1 1 2 3 Input :4 Output : Fibonacci Series is : 0 1 1 2 The Fibonacci numbers are the numbers in the following integer sequence . 0, 1, 1, 2
1 min read
R Program to Print the Fibonacci SequenceThe Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many applications in various fie
2 min read