-1

What is an algorithm to calculate $\lfloor n\phi \rfloor$ given some integer $n$, where $\phi$ is the golden ratio?

I am thinking the easiest way will involve calculating multiples of its continued fraction representation, since the golden ratio has a simple representation as a continued fraction ($[1;1,1,1,\dots]$) and it is easy to find the floor of a continued fraction (it is just the integer part of the fraction). However, I do not know how to calculate multiples of a continued fraction.

Also, by algorithm, I mean that it could be efficiently implemented on a computer. The algorithm may use arbitrary-precision integer arithmetic. It should not, however, be sensitive to rounding errors if floating arithmetic is used (I would prefer to avoid floats entirely). Pseudocode would be great, but if you do not include it, that is fine.

PyRulez
  • 9,530
  • 3
  • 34
  • 98

2 Answers2

1

The golden ratio satisfies $\phi^2-\phi-1=0$, so $n\phi$ is the positive solution to $x^2-nx-n^2=0$. You can use standard numerical methods (bisection, if you're in a hurry coding-wise, but you could save running time by starting with approximate Newton-Raphson until the deltas get small) to bracket the root between two neighboring integers.

hmakholm left over Monica
  • 276,945
  • 22
  • 401
  • 655
0

The Golden ratio can be approximated with arbitrary precision as the ratio of two consecutive Fibonacci numbers. These are easy to compute incrementally. Then compute and round

$$\left[n\frac{F_{k+1}}{F_k}\right].$$

It is even possible to compute $nF_k$ simultaneously to $F_k$. So the total cost is $2k$ additions and one division.

  • What value is $k$? – PyRulez Mar 20 '19 at 14:02
  • Unfriendly downvote. –  Mar 20 '19 at 14:24
  • @PyRulez Such that the error on $\phi$ is smaller than $1/n$. –  Mar 20 '19 at 14:25
  • @HenningMakholm: obviously. The iterates are alternatively by default and by excess, so the sign is predictable. –  Mar 20 '19 at 14:35
  • Hmm, right, so if you get the same floor for two successive $k$s you'll know that is it. All in all, this probably has better asymptotic efficiency than my proposal, if you use matrix powers of $[{}^1_1\,{}^1_0]$ and a sub-quadratic multiplication algorithm to compute the Fibonacci numbers. – hmakholm left over Monica Mar 20 '19 at 14:49
  • @HenningMakholm that depends on how efficiently $k$ can be calculated (unless you compute the floor for each $k$ like you said) – PyRulez Mar 20 '19 at 14:58
  • @PyRulez: $k$ must be a simple function of $\log_\phi n$, nothing critical. An overestimate by a few units should be no sin. –  Mar 20 '19 at 15:02
  • 1
    @PyRulez: You can estimate the error in Yves's approximation using $F_n=\frac{\phi^n+(-1)^{n+1}\phi^{-n}}{\sqrt 5}$. You will find that choosing $k$ as some _constant_ times $\log n$ will make the error less than $1$. Thus doing Yves' computation for this $k$ as well as $k+1$ will either tell you the final result immediately, or an integer $a$ such that $a – hmakholm left over Monica Mar 20 '19 at 15:18
  • (and even an overestimate of $k$ by a _factor_ of $2$ or $3$ will vanish in the asymptotic analysis anyway). – hmakholm left over Monica Mar 20 '19 at 15:21