Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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</p> <p>F(n) = (1 / &radic;5) (&Phi;<sup>n</sup> - &phi;<sup>n</sup>)</p> <p>Where &Phi; = (1 + &radic;5)/2 and &phi; = 1 - &Phi;.</p> <p>The first observation is that &phi;<sup>n</sup> &lt; 1 for any n > 1. This means that for any n > 2, we have that</p> <p>F(n) = &lfloor; &Phi;<sup>n</sup> / &radic;5 &rfloor;</p> <p>Now, take n and write it in binary as b<sub>k-1</sub>b<sub>k-2</sub>...b<sub>1</sub>b<sub>0</sub>. This means that</p> <p>n = 2<sup>k-1</sup> b<sub>k-1</sub> + 2<sup>k-2</sup> b<sub>k-2</sub> + ... + 2<sup>1</sup> b<sub>1</sub> + 2<sup>0</sup> b<sub>0</sub>.</p> <p>This means that</p> <p>F(n) = &lfloor; &Phi;<sup>2<sup>k-1</sup> b<sub>k-1</sub> + 2<sup>k-2</sup> b<sub>k-2</sub> + ... + 2<sup>1</sup> b<sub>1</sub> + 2<sup>0</sup> b<sub>0</sub></sup> / &radic;5 &rfloor;</p> <p>Or, more readably, that</p> <p>F(n) = &lfloor; &Phi;<sup>2<sup>k-1</sup> b<sub>k-1</sub></sup>&Phi;<sup>2<sup>k-2</sup> b<sub>k-2</sub></sup> ... &Phi;<sup>2<sup>1</sup> b<sub>1</sub></sup>&Phi;<sup>2<sup>0</sup> b<sub>0</sub></sup> / &radic;5 &rfloor;</p> <p>This suggests the following algorithm. First, start computing &Phi;<sup>2<sup>k</sup></sup> for all k until you compute a number &Phi;<sup>z</sup> such that &lfloor; &Phi;<sup>z</sup> / &radic;5 &rfloor; that's greater than your number F(n). Now, from there, iterate backwards across all of the powers of &Phi; you generated this way. If the current number is bigger than the indicated power of &Phi;, then divide it by that power of &Phi; 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.</p> <p>The runtime of this algorithm is O(lg n), since you can generate &Phi;<sup>2<sup>i</sup></sup> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload