71

There are dozens of ways of computing F(n) for an arbitrary n, many of which have great runtime and memory usage.

However, suppose I wanted to ask the opposite question:

Given F(n) for n > 2, what is n?

(The n > 2 restriction is in there since F(1) = F(2) = 1 and there's no unambiguous inverse).

What would be the most efficient way of solving this problem? It's easy to do this in linear time by enumerating the Fibonacci numbers and stopping when you hit the target number, but is there some way of doing this any faster than that?

EDIT: currently, the best solution posted here runs in O(log n) time using O(log n) memory, assuming that mathematical operations run in O(1) and that a machine word can hold any number in O(1) space. I'm curious if it's possible to drop the memory requirements, since you can compute Fibonacci numbers using O(1) space.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • You can find some useful discussion in the math.exchange related question: [checking-if-a-number-is-a-fibonacci-or-not]: http://math.stackexchange.com/questions/9999/checking-if-a-number-is-a-fibonacci-or-not – ypercubeᵀᴹ Mar 02 '11 at 23:34
  • 13
    I might call this the fibonacci logarithm – President James K. Polk Mar 04 '11 at 12:03
  • This is a very interesting problem, because it really asks if it is possible to do efficient binary search on a general group with comparison. That is we can use only plus and minus, no division or other fancy operations. – Thomas Ahle Jun 14 '14 at 22:15

10 Answers10

53

Since OP has asked about matrix solution not involving any floating point computations, here it is. We can achieve O(logn) complexity this way, assuming numeric operations have O(1) complexity.

Let's take 2x2 matrix A having following structure

1 1
1 0

Now consider vector (8, 5), storing two consecutive fibonacci numbers. If you multiply it by this matrix, you'll get (8*1 + 5*1, 8*1 + 5*0) = (13, 8) - the next fibonacci number.
If we generalize, A^n * (1, 0) = (f(n), f(n - 1)).

The actual algorithm takes two steps.

  1. Calculate A^2, A^4, A^8, etc. until we pass desired number.
  2. Do a binary search by n, using calculated powers of A.

On a side note, any sequence of the form f(n) = k1*f(n-1) + k2*f(n-2) + k3*f(n-3) + .. + kt*f(n-t) can be presented like this.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • I'm still a bit fuzzy on what in particular you do once you pass the desired number. How exactly do you recover the answer? – templatetypedef Mar 02 '11 at 03:57
  • 2
    @templatetypedef Imagine we passed `f` at `A^16`, therefore we do binary search in range `[0, 16]`. `mid` is 8, and we have `A^8` computed already. Let's say `f > A^8`, then the range is reduced to `[8, 16]`. Now `mid` is 12, but `A^12` is `A^8*A^4`. 8 is a current search border and 4 is a power of 2: therefore we have both computed and can calculate `A^12` in one multiplication. And so on. – Nikita Rybak Mar 02 '11 at 04:02
  • @templatetypedef Comparing matrixes with numbers (`f`) is a bit of simplification, but that should give the idea. – Nikita Rybak Mar 02 '11 at 04:03
  • @Nikita, I'm not sure I'd call it a binary search. What we need is a base-2 decomposition of n, and then the matrices can be produced up to that point. Take 11 = 1+2+8, which implies that F(11) = A^11 = A*A^2*A^8. So, we don't need to calculate A^16. – rcollyer Mar 02 '11 at 04:17
  • @rcollyer You might be solving opposite task: we don't know `n` and thus can't decompose it. Or do I miss something in your idea? – Nikita Rybak Mar 02 '11 at 04:20
  • @Nikita, my apologies, I missed what you were doing completely. – rcollyer Mar 02 '11 at 04:28
  • +1 I've never seen the matrix method (it's obvious once presented). Makes me want to revisit continued fraction expansions using them. – phkahler Mar 02 '11 at 16:45
  • @Nikita Rybak- This solution rocks, but is there a way to do it in O(1) memory? – templatetypedef Mar 02 '11 at 22:44
  • 1
    @templatetypedef I'm afraid no, nothing I can think of. (We can switch to recursion, but that's the same `O(logn)` memory, only hidden.) On the upside, we can memorize and reuse powers of `A`. – Nikita Rybak Mar 02 '11 at 23:02
  • Great solution. Anyway, I suggest you edit your answer to include the comment you made at 4:02 on Mar 2. – Haozhun Mar 10 '11 at 15:00
  • Looks like you can do this in O(1) memory using the Zeckendorf trick below. Given that, this is a really great answer. Thanks so much! – templatetypedef Mar 11 '11 at 23:04
44

Wikipedia gives the result as

n(F) = Floor[ Log(F Sqrt(5) + 1/2)/Log(Phi)]

where Phi is the golden ratio.

rcollyer
  • 10,475
  • 4
  • 48
  • 75
  • How fast can this logarithm be computed? And with what memory usage? – templatetypedef Mar 02 '11 at 02:45
  • (I ask because if this still takes linear time it's not much better than the naive solution) – templatetypedef Mar 02 '11 at 02:54
  • 4
    This `n(F)` is the fastest way to compute `n` for a given `F(n)` (ignoring the fact that `n(1)` returns 2). However, it does *not* guarantee that `F` is *actually* a member of the Fibonacci sequence (given a large `F`, the numbers around `F` will give the same result). – Michelle Tilley Mar 02 '11 at 02:57
  • Good point. But, the log can be split into two terms `log(F) + log(5)/2`, and both `log(5)` and `log(phi)` can be precomputed. Use base 5, and at least some of it is even easier. So, only `log(F)` itself has to be computed whenever you want an answer. – rcollyer Mar 02 '11 at 02:58
  • 3
    This can be computed in constant time, as there are functions in almost every language that perform log and sqrt in constant time. – Dan Mar 02 '11 at 02:59
  • @Brandon: it doesn't, but quickly computing `Fib(n)` with the n you found using n(F) and comparing that with the original F makes it clear whether it was a fib or not. – Dan Mar 02 '11 at 03:02
  • 3
    @Dan I found this interesting: You can also check to see if `phi * n - (1.0 / n)` and `phi * n + (1.0 / n)` crosses a positive integer. E.g. for `n = 144` you get `232.9899` and `233.0038`, which crosses `233`. Using the same calculation on `n = 143` gives `231.3718` and `231.3858`, and so is not a Fibonacci number. – Michelle Tilley Mar 02 '11 at 03:07
  • 10
    @Dan: It's constant time *only* if you consider numbers with a fixed upper bound. – R. Martinho Fernandes Mar 02 '11 at 03:10
  • (continued) and can be computed programatically via the boolean statement `floor(phi * n - 1.0 / n) != floor(phi * n + 1.0 / n)` – Michelle Tilley Mar 02 '11 at 03:10
  • 4
    @Dan- I am skeptical that you can take a log in constant time unless you bound the precision of your numbers. This would be a *practically* good solution, but I'm more interested in something that scales as well as possible given just basic mathematical operations as primitives. – templatetypedef Mar 02 '11 at 03:34
  • 1
    @templatetypedef @Martinho: you're right I was assuming ordinary data types. – Dan Mar 02 '11 at 03:38
  • 3
    You can't do anything in constant time with unbounded numbers (even addition takes O(log(N)). If the hypothetical big-number system stored the length of a number, then that is a good first approximation to the log base 2 and you can read the bits only to the level of precision needed. – phkahler Mar 02 '11 at 16:36
  • @templatetypedef: you can compute log_phi(F) in time logarithmic in F. Just use the same repeated squaring trick we use in exponentiation (http://en.wikipedia.org/wiki/Exponentiation_by_squaring). So, under your computation model, this solution gives a log time, constant space method. – mhum Mar 09 '11 at 06:11
  • 1
    @mhum- I think that this approach requires O(lg n) memory, since if your binary search ever exceeds the number in question you somehow need to be able to narrow the search down. I can see how to get one bit at a time using this approach for a grand total of O(lg^2 n) time, and without seeing a more rigorous algorithm for doing it in O(lg n) with constant space I'm not convinced you can do it. user635541's answer below does outline one approach for computing the log in O(lg n) time and O(1) space using the Fibonacci numbers (!!) though, so it does indeed seem possible. – templatetypedef Mar 09 '11 at 07:15
  • @templatetypedef: Actually it's even easier than I thought, we don't even need something like repeated squaring or binary search. To compute, say, log_phi(n), start with 1, keep multiplying by phi until you exceed n. The number of multiplications is ceil(log_phi(n)), and hence O(lg(n)). – mhum Mar 09 '11 at 17:16
  • @mhum- I think that this runs in O(n), since log phi^n = n. Remember that n is the index of the Fibonacci number, not the Fibonacci number itself. – templatetypedef Mar 09 '11 at 18:30
  • @templatetypedef: Ah, I misunderstood. In this case, we do need a binary search. Do we get exponentiation in constant time? If so, then we compute phi, phi^2, phi^4, phi^16, ... until phi^{2^k} < F < phi^{2^{k+1}}. From here, we do a binary search for i between 2^k and 2^k+1 such that phi^i < F < phi^{i+1}. Since F(n) is approx. phi^n, the first part takes O(log(n)) steps and the second also takes O(log(n)). – mhum Mar 10 '11 at 05:00
  • 1
    @mhum- No, exponentiation isn't free. Assume that the standard ring operations (add, subtract, multiply, divide) are of unit cost. I actually just coded up user635541's excellent idea to use Fibonacci numbers for the log, which works like a charm. – templatetypedef Mar 10 '11 at 05:31
  • @rcollyer: I tried this with some big Fibonaccis. It returns immediately for `n <= 1000`, but I get overflow problem later on. – Thomas Ahle Jun 15 '14 at 10:13
  • I tried to get this equation using wolfram alpha like this: [wolfram alpha](https://www.wolframalpha.com/input/?i=solve+n%3B+x+%3D+%285%5E0.5+*+%281%2B5%5E0.5%29%5En+-+5%5E0.5+*+%281-5%5E0.5%29%5En%29+*+2%5E-n+*+0.2) What am I doing wrong? – gkucmierz Nov 06 '19 at 16:21
  • I have noticed that in Python functions like these fail for relatively large F_n, because Python doesnt like multiplying large integers with floats. You can get around this using simple log properties log(F sqrt(5)) = (1/2)log(5 F^2), and modify from there. Python takes no issue with logging a large integer. – SquishyRhode Jan 22 '23 at 00:44
14

If you can easily interpret F(n) in binary,

formula

You may be suspicious of the constants 1.7 and 1.1. These work because d*1.44042009041... + C never gets very close to an integer.

I can post a derivation tomorrow if there is interest.

Here is a table with n = 2 through 91, which shows the formula result before flooring:

 n  formula w/o floor     F(n) F(n) in binary

 2  2.540                    1 1
 3  3.981                    2 10
 4  4.581                    3 11
 5  5.421                    5 101
 6  6.862                    8 1000
 7  7.462                   13 1101
 8  8.302                   21 10101
 9  9.743                   34 100010
10 10.343                   55 110111
11 11.183                   89 1011001
12 12.623                  144 10010000
13 13.223                  233 11101001
14 14.064                  377 101111001
15 15.504                  610 1001100010
16 16.104                  987 1111011011
17 17.545                 1597 11000111101
18 18.385                 2584 101000011000
19 19.825                 4181 1000001010101
20 20.425                 6765 1101001101101
21 21.266                10946 10101011000010
22 22.706                17711 100010100101111
23 23.306                28657 110111111110001
24 24.147                46368 1011010100100000
25 25.587                75025 10010010100010001
26 26.187               121393 11101101000110001
27 27.028               196418 101111111101000010
28 28.468               317811 1001101100101110011
29 29.068               514229 1111101100010110101
30 30.508               832040 11001011001000101000
31 31.349              1346269 101001000101011011101
32 32.789              2178309 1000010011110100000101
33 33.389              3524578 1101011100011111100010
34 34.230              5702887 10101110000010011100111
35 35.670              9227465 100011001100110011001001
36 36.270             14930352 111000111101000110110000
37 37.111             24157817 1011100001001111001111001
38 38.551             39088169 10010101000111000000101001
39 39.151             63245986 11110001010000111010100010
40 40.591            102334155 110000110010111111011001011
41 41.432            165580141 1001110111101000110101101101
42 42.032            267914296 1111111110000000110000111000
43 43.472            433494437 11001110101101001100110100101
44 44.313            701408733 101001110011101010010111011101
45 45.753           1134903170 1000011101001010011111110000010
46 46.353           1836311903 1101101011100111110010101011111
47 47.193           2971215073 10110001000110010010010011100001
48 48.634           4807526976 100011110100011010000101001000000
49 49.234           7778742049 111001111101001100010111100100001
50 50.074          12586269025 1011101110001100110011100101100001
51 51.515          20365011074 10010111101110110010110100010000010
52 52.115          32951280099 11110101100000011001010000111100011
53 53.555          53316291173 110001101001111001100000101001100101
54 54.396          86267571272 1010000010101111100101010110001001000
55 55.836         139583862445 10000001111111110110001011011010101101
56 56.436         225851433717 11010010010101110010110110001011110101
57 57.276         365435296162 101010100010101101001000001100110100010
58 58.717         591286729879 1000100110101011011011110111110010010111
59 59.317         956722026041 1101111011000001000100111001011000111001
60 60.157        1548008755920 10110100001101100100000110001001011010000
61 61.598        2504730781961 100100011100101101100101101010100100001001
62 62.198        4052739537881 111010111110011010000110011011101111011001
63 63.038        6557470319842 1011111011011000111101100000110010011100010
64 64.478       10610209857723 10011010011001100001110010100010000010111011
65 65.078       17167680177565 11111001110100101001011110101000010110011101
66 66.519       27777890035288 110010100001110001011010001001010011001011000
67 67.359       44945570212853 1010001110000010110100101111110010101111110101
68 68.800       72723460248141 10000100010010001000000000000111101001001001101
69 69.400      117669030460994 11010110000010011110100110000101111111001000010
70 70.240      190392490709135 101011010010100100110100110001101101000010001111
71 71.681      308061521170129 1000110000010111000101001100010011100111011010001
72 72.281      498454011879264 1110001010101011101011110010100001001111101100000
73 73.121      806515533049393 10110111011000010110000111110110100110111000110001
74 74.561     1304969544928657 100101000101101110011100110001010110000110110010001
75 75.161     2111485077978050 111100000000110001001101110000001010111101111000010
76 76.602     3416454622906707 1100001000110011111101010100001100001000100101010011
77 77.442     5527939700884757 10011101000111010000111000010001101100000010100010101
78 78.042     8944394323791464 11111110001101110000100010110011001101000111001101000
79 79.483    14472334024676221 110011011010101000001011011000100111001001001101111101
80 80.323    23416728348467685 1010011001100010110001111101111000000110010000111100101
81 81.764    37889062373143906 10000110100110111110011011000111100111111011010101100010
82 82.364    61305790721611591 11011001110011010100101010110110101000101101011101000111
83 83.204    99194853094755497 101100000011010010011000101111110010000101000110010101001
84 84.644   160500643816367088 1000111010001101100111110000110100111001010110001111110000
85 85.244   259695496911122585 1110011010100111111010110110110011001001111111000010011001
86 86.085   420196140727489673 10111010100110101100010100111101000000011010101010010001001
87 87.525   679891637638612258 100101101111011101011101011110011011001101010100010100100010
88 88.125  1100087778366101931 111101000100010011000000000110000011010000101001100110101011
89 89.566  1779979416004714189 1100010110011110000011101100100011110011101111101111011001101
90 90.406  2880067194370816120 10011111111000000011011101101010100001101110100111100001111000
91 91.846  4660046610375530309 100000010101011110011111011001111000000001100100101011101000101

Derivation

enter image description here

Required Precision for α = log 2/log Φ ≈ 1.44042...

enter image description here

Tom Sirgedas
  • 3,208
  • 20
  • 17
  • This answer is O(1) and an absolute triumph - @rcollyer's answer reduced to a very slick calculation. Given the original constraints of the problem (knowing the input certainly is Fibonacci), surely this can't be beat. – Chris Nash Mar 11 '11 at 21:40
  • I don't know why you bothered writing out an approximation of 1/log_2(phi), since you need lg d + O(1) bits of accuracy. This is most definitely **not** O(1). – userOVER9000 Mar 11 '11 at 22:54
  • @userOVER9000 So getting a better double approximation would be good enough to answer the question for an input that's 2^53 bits long? 1 pebibyte? – Chris Nash Mar 12 '11 at 01:33
  • This seems dangerously close to erroring at `91`. Did you run it for `92` as well? – Thomas Ahle Jun 15 '14 at 00:03
  • No, but we can calculate it. F(92) = F(91) + F(90). Looking at binary form of F(91) and F(90) we can tell that their sum will have the same number of digits as F(91), but start with "11". So "Formula w/o floor" for F(92) will be exactly .6 more than for F(91), which gives ~92.446. – Tom Sirgedas Jun 24 '14 at 18:13
  • It is pretty genius approach, but at what value of n does this fail? Obviously youre relying on floating point precision here. At the values youre using n=91, its a trivially fast computation to just iterate through the Fibonacci numbers directly. At values of n where speed may be of concern, this algorithm is likely to be imprecise. – SquishyRhode Jan 16 '23 at 02:04
  • The derivation is fairly straight forward but Im curious where the 1.1 and 1.7 came from. My approach at the derivation is yielding different numeric results for these values. Are you using a particular kind of mean value? – SquishyRhode Jan 16 '23 at 17:08
  • 1
    @SquishyRhode: Thanks, I edited the post to answer your questions. – Tom Sirgedas Jan 21 '23 at 04:59
11

Measuring memory usage by counting unbounded words is sort of silly, but as long as that's the model, there's an O(log n) time, O(1) word solution similar to Nikita Rybak's that in essence computes n via its Zeckendorf representation, which is based on the Fibonacci numbers (YO DAWG).

Define the matrix

      1  1
A  =       ,
      1  0

which satisfies

        F(m + 1)    F(m)
A^m  =                      .
          F(m)    F(m - 1)

Instead of the sequence A^(2^k), we're going to use the sequence A^F(k). The latter sequence has the property that we can move forward with a matrix multiply

A^F(k + 1) = A^F(k - 1) * A^F(k)

and backward with a matrix inverse and multiplication

A^F(k - 1) = A^F(k + 1) (A^F(k))^-1,

so we can build a bidirectional iterator with only eight six twelve words assuming we store everything as rationals (to avoid assuming the existence of a unit-cost divide). The rest is just adapting this O(1)-space algorithm for finding a Zeckendorf representation.

def zeck(n):
    a, b = (0, 1)
    while b < n:
        a, b = (b, a + b)
    yield a
    n1 = a
    while n1 < n:
        a, b = (b - a, a)
        if n1 + a <= n:
            yield a
            n1 += a
            a, b = (b - a, a)

>>> list(zeck(0))
[0]
>>> list(zeck(2))
[1, 1]
>>> list(zeck(12))
[8, 3, 1]
>>> list(zeck(750))
[610, 89, 34, 13, 3, 1]
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
user635541
  • 1,214
  • 6
  • 7
  • From this it's obvious that the basic Fib eqn F(m + 1) = F(m-1) + F(m) is the log, base the matrix A, of the matrix multiply eqn A^F(m+1)=A^F(m)*A^F(m-1). Sweet mathy answer! – Reb.Cabin Feb 29 '12 at 12:49
  • 2
    I'm not sure I quite understand how this works. If you create the Zeckendorf representation, don't you still spend `logn` memory? Do you also make a list of all `A^F(n)` powers? – Thomas Ahle Jun 15 '14 at 00:08
  • @ThomasAhle (this answer is old but) As stated in the answer, only two A^F(n) is stored at a time. – user202729 Sep 22 '20 at 06:45
3

It's been proven that the formula for a fib n is fib(n) = ( (phi)^n - (-phi)^(-n) ) / sqrt(5) where phi = (1+sqrt(5)) / 2, the golden section number. (see this link).

You could try to find a mathematical inverse to the fib function above, or otherwise do a binary search in 32/64 operations (depending on how big your searchable maximum is) to find the n that matches the number (try each n by computing fib(n) and splitting your sample space in two according to how fib(n) compares to the given fibonacci number).

Edit: @rcollyer's solution is faster, as mine is in O(lg n) and the one he found is in O(1) = constant time.

Dan
  • 3,490
  • 2
  • 22
  • 27
2

So I was thinking about this problem and I think that it's possible to do this in O(lg n) time with O(lg n) memory usage. This is based on the fact that

F(n) = (1 / √5) (Φn - φn)

Where Φ = (1 + √5)/2 and φ = 1 - Φ.

The first observation is that φn < 1 for any n > 1. This means that for any n > 2, we have that

F(n) = ⌊ Φn / √5 ⌋

Now, take n and write it in binary as bk-1bk-2...b1b0. This means that

n = 2k-1 bk-1 + 2k-2 bk-2 + ... + 21 b1 + 20 b0.

This means that

F(n) = ⌊ Φ2k-1 bk-1 + 2k-2 bk-2 + ... + 21 b1 + 20 b0 / √5 ⌋

Or, more readably, that

F(n) = ⌊ Φ2k-1 bk-1Φ2k-2 bk-2 ... Φ21 b1Φ20 b0 / √5 ⌋

This suggests the following algorithm. First, start computing Φ2k for all k until you compute a number Φz such that ⌊ Φz / √5 ⌋ that's greater than your number F(n). Now, from there, iterate backwards across all of the powers of Φ you generated this way. If the current number is bigger than the indicated power of Φ, then divide it by that power of Φ and record that the number was divided by this value. This process essentially recovers one bit of n at a time by subtracting out the largest power of 2 that you can at a time. Consequently, once you're done, you'll have found n.

The runtime of this algorithm is O(lg n), since you can generate Φ2i by repeated squaring, and we only generate O(lg n) terms. The memory usage is O(lg n), since we store all of these values.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • You can escape floating point computations if you use 2x2 matrixes instead. It should be faster and somewhat simpler. – Nikita Rybak Mar 02 '11 at 03:28
  • Don't agree with this. Compute phi^2^k itself is a problem. How precise? Then you need to take the floors etc. What is wrong with a simple binary search, computing Fibonacci using matrix multiplication? :-P –  Mar 02 '11 at 03:29
  • @Moron, @Nikita Rybak- I like the idea to use the matrix representation. However, I don't see how to recover individual bits out of those representations. Could you clarify that step? I definitely would like something numerically stable! – templatetypedef Mar 02 '11 at 03:31
  • @templatetypedef I've posted a description in a separate answer. – Nikita Rybak Mar 02 '11 at 03:56
  • @Moron Solution based on matrix multiplication will have the same problems, as `n` grows. Only here we need lots of signs after decimal point, and with matrix multiplication we need huge numbers. – Nikita Rybak Mar 02 '11 at 03:58
  • @Nikita: If you are capable of processing F(n), I don't see how there will be issues... –  Mar 02 '11 at 04:13
  • @Moron Not exactly "issues". If f(n) fits in `int`, then we can probably use this formula with `double` precision. And if it doesn't, we'll need to pay the cost of long arithmetics in both solutions to make them work. I do like 'precise' version with matrixes more, just saying the there's not likely any difference in asymptotical complexity. – Nikita Rybak Mar 02 '11 at 04:18
  • @Nikita: IMO, it would get difficult to prove the correctness of the algorithms which use operations of floating points: the 'long arithmetics' might just get too long as compared to the integer arithmetic version. Of course, powers of phi can probably be computed to a reasonable degree by computing the continued fraction approximations, which I suppose involve fibonacci! Search the web for sum of square roots problem for the difficulties in picking the right precision... –  Mar 02 '11 at 04:29
2

You can find n for any Fib(n) in O(1) time and O(1) space.

You can use a fixed-point CORDIC algorithm to compute ln() using only shift and add on integer data types.

If x = Fib(n), then n can be determined by

     n = int(2.0801 * ln(x) + 2.1408)

CORDIC run-time is determined by the desired level of precision. The two floating-point values would be encoded as fixed-point values.

The only issue with this proposal is that it returns a value for numbers that are not in the Fibonacci sequence, but the original problem specifically stated that the input to the function would be Fib(n), which implies that only valid Fibonacci numbers would be used.

oosterwal
  • 1,479
  • 8
  • 16
1

This might be similar to user635541's answer. I don't fully understand his approach.

Using the matrix representation for Fibonacci numbers, discussed in other answers, we get a way to go from F_n and F_m to F_{n+m} and F_{n-m} in constant time, using only plus, multiplication, minus and division (actually not! see the update). We also have a zero (the identity matrix), so it is a mathematical group!

Normally when doing binary search we also want a division operator for taking averages. Or at least division by 2. However if we want to go from F_{2n} to F_n it requires a square root. Luckily it turns out that plus and minus are all we need for a logarithmic time 'nearly' binary search!

Wikipedia writes about the approach, ironically called Fibonacci_search, but the article is not very clearly written, so I don't know if it is exactly the same approach as mine. It is very important to understand that the Fibonacci numbers used for the Fibonacci search have nothing to do with the numbers we are looking for. It's a bit confusing. To demonstrate the approach, here is first an implementation of standard 'binary search' only using plus and minus:

def search0(test):
    # Standard binary search invariants:
    #   i <= lo then test(i)
    #   i >= hi then not test(i)
    # Extra invariants:
    #   hi - lo = b
    #   a, b = F_{k-1}, F_k
    a, b = 0, 1
    lo, hi = 0, 1
    while test(hi):
        a, b = b, a + b
        hi = b
    while b != 1:
        mi = lo + a
        if test(mi):
            lo = mi
            a, b = 2*a - b, b - a
        else:
            hi = mi
            a, b = b - a, a
    return lo

>>> search0(lambda n: n**2 <= 25)
5
>>> search0(lambda n: 2**n <= 256)
8

Here test is some boolean function; a and b are consecutive fibonacci numbers f_k and f_{k-1} such that the difference between out upper bound hi and lower bound lo is always f_k. We need both a and b so we can increase and decrease the implicit variable k efficiently.

Alright, so how do we use this to solve the problem? I found it useful to create a wrapper around our Fibonacci representation, that hides the matrix details. In practice (is there such a thing for a Fibonacci searcher?) you would want to inline everything manually. That would spare you the redundancy in the matrices and make some optimization around the matrix inversion.

import numpy as np
class Fib:
    def __init__(self, k, M):
        """ `k` is the 'name' of the fib, e.g. k=6 for F_6=8.
             We need this to report our result in the very end.
            `M` is the matrix representation, that is
             [[F_{k+1}, F_k], [F_k, F_{k-1}]] """
        self.k = k
        self.M = M
    def __add__(self, other):
        return Fib(self.k + other.k, self.M.dot(other.M))
    def __sub__(self, other):
        return self + (-other)
    def __neg__(self):
        return Fib(-self.k, np.round(np.linalg.inv(self.M)).astype(int))
    def __eq__(self, other):
        return self.k == other.k
    def value(self):
        return self.M[0,1]

However the code does work, so we can test it as follows. Notice how little different the search function is from when our objects were integers and not Fibonaccis.

def search(test):
    Z = Fib(0, np.array([[1,0],[0,1]])) # Our 0 element
    A = Fib(1, np.array([[1,1],[1,0]])) # Our 1 element
    a, b = Z, A
    lo, hi = Z, A
    while test(hi.value()):
        a, b = b, a + b
        hi = b
    while b != A:
        mi = lo + a
        if test(mi.value()):
            lo = mi
            a, b = a+a-b, b-a
        else:
            hi = mi
            a, b = b-a, a
    return lo.k

>>> search(lambda n: n <= 144)
12
>>> search(lambda n: n <= 0)
0

The remaining open question is whether there is an efficient search algorithm for monoids. That is one that doesn't need a minus / additive inverse. My guess is no: that without minus you need the extra memory of Nikita Rybak.

Update

I just realized that we don't need division at all. The determinant of the F_n matrix is just (-1)^n, so we can actually do everything without division. In the below I removed all the matrix code, but I kept the Fib class, just because everything got so extremely messy otherwise.

class Fib2:
    def __init__(self, k, fp, f):
        """ `fp` and `f` are F_{k-1} and F_{k} """
        self.k, self.fp, self.f = k, fp, f
    def __add__(self, other):
        fnp, fn, fmp, fm = self.fp, self.f, other.fp, other.f
        return Fib2(self.k + other.k, fn*fm+fnp*fmp, (fn+fnp)*fm+fn*fmp)
    def __sub__(self, other):
        return self + (-other)
    def __neg__(self):
        fp, f = self.f + self.fp, -self.f
        return Fib2(-self.k, (-1)**self.k*fp, (-1)**self.k*f)
    def __eq__(self, other):
        return self.k == other.k
    def value(self):
        return self.f

def search2(test):
    Z = Fib2(0, 1, 0)
    A = Fib2(1, 0, 1)
    ...

>>> search2(lambda n: n <= 280571172992510140037611932413038677189525)
200
>>> search2(lambda n: n <= 4224696333392304878706725602341482782579852840250681098010280137314308584370130707224123599639141511088446087538909603607640194711643596029271983312598737326253555802606991585915229492453904998722256795316982874482472992263901833716778060607011615497886719879858311468870876264597369086722884023654422295243347964480139515349562972087652656069529806499841977448720155612802665404554171717881930324025204312082516817125)
2000
>>> search2(lambda n: n <= 2531162323732361242240155003520607291766356485802485278951929841991312781760541315230153423463758831637443488219211037689033673531462742885329724071555187618026931630449193158922771331642302030331971098689235780843478258502779200293635651897483309686042860996364443514558772156043691404155819572984971754278513112487985892718229593329483578531419148805380281624260900362993556916638613939977074685016188258584312329139526393558096840812970422952418558991855772306882442574855589237165219912238201311184749075137322987656049866305366913734924425822681338966507463855180236283582409861199212323835947891143765414913345008456022009455704210891637791911265475167769704477334859109822590053774932978465651023851447920601310106288957894301592502061560528131203072778677491443420921822590709910448617329156135355464620891788459566081572824889514296350670950824208245170667601726417091127999999941149913010424532046881958285409468463211897582215075436515584016297874572183907949257286261608612401379639484713101138120404671732190451327881433201025184027541696124114463488665359385870910331476156665889459832092710304159637019707297988417848767011085425271875588008671422491434005115288334343837778792282383576736341414410248994081564830202363820504190074504566612515965134665683289356188727549463732830075811851574961558669278847363279870595320099844676879457196432535973357128305390290471349480258751812890314779723508104229525161740643984423978659638233074463100366500571977234508464710078102581304823235436518145074482824812996511614161933313389889630935320139507075992100561077534028207257574257706278201308302642634678112591091843082665721697117838726431766741158743554298864560993255547608496686850185804659790217122426535133253371422250684486113457341827911625517128815447325958547912113242367201990672230681308819195941016156001961954700241576553750737681552256845421159386858399433450045903975167084252876848848085910156941603293424067793097271128806817514906531652407763118308162377033463203514657531210413149191213595455280387631030665594589183601575340027172997222489081631144728873621805528648768511368948639522975539046995395707688938978847084621586473529546678958226255042389998718141303055036060772003887773038422366913820397748550793178167220193346017430024134496141145991896227741842515718997898627269918236920453493946658273870473264523119133765447653295022886429174942653014656521909469613184983671431465934965489425515981067546087342348350724207583544436107294087637975025147846254526938442435644928231027868701394819091132912397475713787593612758364812687556725146456646878912169274219209708166678668152184941578590201953144030519381922273252666652671717526318606676754556170379350956342095455612780202199922615392785572481747913435560866995432578680971243966868110016581395696310922519803685837460795358384618017215468122880442252343684547233668502313239328352671318130604247460452134121833305284398726438573787798499612760939462427922917659263046333084007208056631996856315539698234022953452211505675629153637867252695056925345220084020071611220575700841268302638995272842160994219632684575364180160991884885091858259996299627148614456696661412745040519981575543804847463997422326563897043803732970397488471644906183310144691243649149542394691524972023935190633672827306116525712882959108434211652465621144702015336657459532134026915214509960877430595844287585350290234547564574848753110281101545931547225811763441710217452979668178025286460158324658852904105792472468108996135476637212057508192176910900422826969523438985332067597093454021924077101784215936539638808624420121459718286059401823614213214326004270471752802725625810953787713898846144256909835116371235019527013180204030167601567064268573820697948868982630904164685161783088076506964317303709708574052747204405282785965604677674192569851918643651835755242670293612851920696732320545562286110332140065912751551110134916256237884844001366366654055079721985816714803952429301558096968202261698837096090377863017797020488044826628817462866854321356787305635653577619877987998113667928954840972022833505708587561902023411398915823487627297968947621416912816367516125096563705174220460639857683971213093125)
20000

This all works like a charm. My only worry is that the bit complexity such dominates the calculation, that we might as well have just done a sequential search. Or actually, just looking at the number of digits could probably tell you pretty much which you were looking at. That's not as fun though.

Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
1

EDIT: Never mind. The asker has stated in comments that exponentiation is definitely not constant time.


Is exponentiation one of the mathematical operations that you'll allow in constant time? If so, we can compute F(n) in constant time via the closed-form formula. Then, given some F, we can do the following:

  1. Compute F(1), F(2), F(4), F(16), F(256), ... until F(2^k) <= F < F(2^{k+1})
  2. Do a binary search for i between 2^k and 2^{k+1} until F(i) <= F < F(i+1)

If F = F(n), then first part takes k = O(log(n)) steps. The second part is a binary search over a range of size O(2^k), so it also takes k = O(log(n)). So, in total, we have O(log(n)) time in O(1) space if (and it's a big if) we have exponentiation in O(1) time.

mhum
  • 2,928
  • 1
  • 16
  • 11
1

A closed form of the Fibonacci number formula is:

Fn = Round(φ^n / Sqrt(5))

Where φ is the golden ratio.

If we ignore the rounding factor this is invertible and the inverse function is:

F(-1)n= log(n*Sqrt(5))/logφ 

Because we ignored the rounding factor there is an error in the formula which could be calculated. However if we consider that a number n is a Fibonacci number iff the interval [n*φ - 1/n, n*φ + 1/n] contains a natural number then:

A number is a Fibonacci number iff the interval [n*φ - 1/n, n*φ + 1/n] contains a natural number and that number's index in the Fibonacci sequence is given by rounding log(n*Sqrt(5))/logφ

This should be doable in (pseudo)-constant time depending on the algorithms used for calculating the log and square roots etc.

Edit: φ = (1+Sqrt(5))/2

apokryfos
  • 38,771
  • 9
  • 70
  • 114