Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn integers into Fibonacci coding efficiently?

Tags:

algorithm

math

Fibonacci sequence is obtained by starting with 0 and 1 and then adding the two last numbers to get the next one.

enter image description here

All positive integers can be represented as a sum of a set of Fibonacci numbers without repetition. For example: 13 can be the sum of the sets {13}, {5,8} or {2,3,8}. But, as we have seen, some numbers have more than one set whose sum is the number. If we add the constraint that the sets cannot have two consecutive Fibonacci numbers, than we have a unique representation for each number.

We will use a binary sequence (just zeros and ones) to do that. For example, 17 = 1 + 3 + 13. Then, 17 = 100101. See figure 2 for a detailed explanation.

enter image description here

I want to turn some integers into this representation, but the integers may be very big. How to I do this efficiently.

like image 630
Larry Lu Avatar asked May 27 '16 09:05

Larry Lu


People also ask

What is the Fibonacci coding?

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-neighbouring Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ……..).

How many Fibonacci numbers are there in a positive integer?

The first two Fibonacci numbers (0 and 1) are not used, and an additional 1 is always appended. The Fibonacci encodings for the positive integers are binary strings that end with "11" and contain no other instances of "11". This can be generalized to binary strings that end with N consecutive 1's and contain no other instances of N consecutive 1's.

How to find the Fibonacci numbers using two equations?

Using above method we can find these equations: F 2 k = F k ( 2 F k + 1 − F k). F 2 k + 1 = F k + 1 2 + F k 2. Thus using above two equations Fibonacci numbers can be calculated easily by the following code: The above code returns F n and F n + 1 as a pair.

Why do we use the Fibonacci sequence?

Fibonacci numbers are the worst possible inputs for Euclidean algorithm (see Lame's theorem in Euclidean algorithm) We can use the sequence to encode positive integers into binary code words. According to Zeckendorf's theorem, any natural number n can be uniquely represented as a sum of Fibonacci numbers:


1 Answers

The problem itself is simple. You always pick the largest fibonacci number less than the remainder. You can ignore the the constraint with the consecutive numbers (since if you need both, the next one is the sum of both so you should have picked that one instead of the initial two).

So the problem remains how to quickly find the largest fibonacci number less than some number X. There's a known trick that starting with the matrix (call it M)

1 1
1 0

You can compute fibbonacci number by matrix multiplications(the xth number is M^x). More details here: https://www.nayuki.io/page/fast-fibonacci-algorithms . The end result is that you can compute the number you're look in O(logN) matrix multiplications.

You'll need large number computations (multiplications and additions) if they don't fit into existing types. Also store the matrices corresponding to powers of two you compute the first time, since you'll need them again for the results.

Overall this should be O((logN)^2 * large_number_multiplications/additions)).

like image 167
Sorin Avatar answered Nov 15 '22 10:11

Sorin