Share on Facebook Share on Twitter Email
Answers.com

Fibonacci coding

 
Wikipedia: Fibonacci coding
Numeral systems by culture
Hindu-Arabic numerals
Western Arabic
Eastern Arabic
Indian family
Khmer
Mongolian
Thai
East Asian numerals
Chinese
Counting rods
Japanese
Korean
Suzhou
Alphabetic numerals
Abjad
Armenian
Āryabhaṭa
Cyrillic
Ge'ez
Greek (Ionian)
Hebrew
Other systems
Attic
Babylonian
Brahmi
Egyptian
Etruscan
Inuit
Mayan
Roman
Urnfield
List of numeral system topics
Positional systems by base
Decimal (10)
2, 4, 8, 16, 32, 64
1, 3, 6, 9, 12, 20, 24, 30, 36, 60, more…

In mathematics, Fibonacci coding is a universal code which encodes positive integers into binary code words. All tokens end with "11" and have no "11" before the end.

For a number N\!, if d(0),d(1),...,d(k)\! represent the digits of the coded form of N\! then we have:

N = \sum_{i=0}^{k-1} d(i) F(i)\!, and d(k)=1\!.

where F(i) is the ith Fibonacci number. No two adjacent coefficients d(i) can be 1.

It can be shown that such a coding is unique, and in the code "11" never appears anywhere but the end.

The code begins as follows:

Symbol Fibonacci representation Fibonacci code
1 F(1) 1 11
2 F(2) 2 011
3 F(3) 4 0011
4 F(1) + F(3) 5 1011
5 F(4) 8 00011
6 F(1) + F(4) 9 10011
7 F(2) + F(4) 10 01011
8 F(5) 16 000011

The Fibonacci code is closely related to the Zeckendorf representation, a positional numeral system that uses Zeckendorf's theorem and has the property that no number has a representation with consecutive 1's. The Zeckendorf representation for a particular integer is exactly that of the integer's Fibonacci representation, except with the order of its digits reversed and an additional "1" appended to the end.

To encode an integer X:

  1. Find the largest Fibonacci number equal to or less than X; subtract this number from X, keeping track of the remainder.
  2. If the number we subtracted was the Nth unique Fibonacci number, put a one in the Nth digit of our output.
  3. Repeat the previous steps, substituting our remainder for X, until we reach a remainder of 0.
  4. Place a one after the last naturally-occurring one in our output.

To decode a token in the code, remove the last "1", assign the remaining bits the values 1,2,3,5,8,13... (the Fibonacci numbers), and add the "1" bits.

Contents

Comparison with other universal codes

Fibonacci coding has a useful property that sometimes makes it attractive in comparison to other universal codes: it is an example of a self-synchronizing code, making it easier to recover data from a damaged stream. With most other universal codes, if a single bit is altered, none of the data that comes after it will be correctly read. With Fibonacci coding, on the other hand, a changed bit may cause one token to be read as two, or cause two tokens to be read incorrectly as one, but reading a "0" from the stream will stop the errors from propagating further. Since the only stream that has no "0" in it is a stream of "11" tokens, the total edit distance between a stream damaged by a single bit error and the original stream is at most three.

This approach - encoding using sequence of symbols, in which some patterns (like "11") are forbidden, can be freely generalized[1].

Example code

Encode

#include <list>
 
using namespace std;
 
list<int> result;
 
//This function encodes the integer n and stores the code in list<int> result.
void encode_fib(int n){
 
    result.clear(); //Clear the list before processing.
 
    if(n < 1) return;
 
    int a = 1, b = 1;
 
    do{
        //Find the largest Fibonacci number equal to or less than n, storing
        //that one and all the previous Fibonacci numbers in list<int> result.
        result.push_back(b);
        b += a;
        a = result.back();
    }while(b <= n);
 
    for(list<int>::reverse_iterator rli = result.rbegin(); rli != result.rend(); ++rli){
        //Traversing backwards through list<int> result, if the current number
        //is equal to or less than n, subtract it from n, substitute the
        //remainder for n and replace the current number with 1, otherwise
        //replace it with 0 without changing the value of n.
        if(*rli <= n){
            n -= *rli;
            *rli = 1;
        } else{
            *rli = 0;
        }
    }
 
    //Place a one after the last naturally-occurring one in our output.
    result.push_back(1);
}

See also

References

Fraenkel A, Klein S. "Robust universal complete codes for transmission and compression", Discrete Applied Mathematics 64(1996) 31-55. [2]


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Fibonacci coding" Read more