Lecture 2: Continued fractions, rational approximations Algorithmic Number Theory (Fall 204) Rutgers University Swastik Kopparty Scribe: Cole Franks Continued Fractions We begin by calculating the continued fraction representation of a real number Let α be a real number Then α = α + α, α = α + α 2, α n = α n + α n where α i is greater than one for i {,, n} Let a 0 := α, and a i := α i At some point α N may be an integer, in which case this process terminates and α is rational In that case, we simply define a n = 0 for all n > N, and α n is undefined for n > N Note that a n for all n N If α is irrational, the process will never terminate, so we don t have to worry about the two previous lines Note that a i, α i 0 for all i By plugging each equation into the one above, we see that α = a 0 + a + a n + α n The n th convergent of α, provided n N (if N exists), is given by c n = a 0 + a + a n + a n
If n N, then the n th convergent of α is simply c n = a 0 + a + a N + a N = c N The list [a 0, ] is the continued fraction representation of α Lemma The convergents are given by c n = pn where [ ] [ ] [ pn+ p n pn p = n an+ 0 or + p n+ = p n a n+ + p n + = a n+ + ] with p 0 = a 0, q 0 =, p = a 0 a +, q = a Note that for all i 0, q i > 0 In particular, q i is strictly monotonically increasing until a i+ = 0, or when i = N Proof First we claim that for any n and a formal variable Z we have p n Z + p n Z + = a 0 + a + a n + Z, () which we prove by induction The base case n = is an exercise Let n From the inductive hypothesis with Z a n+ + Z, a 0 + a + a n+ + Z, = p ( n an+ + ) Z + pn ( an+ + Z ) + qn = p n (a n+ Z + ) + p n Z (a n+ Z + ) + Z = p na n+ Z + p n + p n Z a n+ Z + + Z = p n+z + p n + Z + If we substitute Z = a n+ in (??), we get that c n+ = p n+ /+ as desired 2
Observation 2 Equation (??) is valid even if n > N, for the proof did not use n N Note that so p p det 0 q q 0 an+ = det 0 =, pn p det n = ( ) n This implies that p n, are relatively prime (because the ideal they generate in Z contains ), and so the convergent pn is already in lowest terms 2 Approximation Now we move to the approximation of real numbers by rational numbers Our aim is to use the lowest denominator rational number possible and still get a nice approximation How good a rational approximation can one get to a given real number α? One trivial rational approximation to α is a number a q with α a q 2q, (for any q we can simply choose a Z at a distance at most /2 from qα) The following theorem shows that every α has a pretty good rational approximation: Theorem 3 (Dirichlet s Theorem) For every α R, either α is rational, or else there are infinitely many distinct rational numbers a q such that α a q < Observation 4 We prove Theorem?? by proving the following stronger theorem: Theorem 5 For all Q N, there is a rational number a q α a q < qq Lemma 6 Theorem?? implies Theorem?? such that q Q and Proof of Lemma?? Take some Q N; by Theorem?? we can find a q α a q < q Q q 2 3 with
If a q = α, then α is rational, and so we are done with Theorem?? in this case If not, ie α a q > 0, then we can take a new Q2 so large that α a q > Q 2 Let a 2 be the pair guaranteed by Theorem?? for Q 2 Then α a 2 < α a q < Q 2 α a, so a 2 is distinct from a q Furthermore, α a 2 < Q 2 q2 2 q Continuing this process, we either find that α is rational, or else we find infinitely many a q required in Theorem?? as This proof, due to Dirichlet, was possibly the first appearance of the Dirichlet Pigeon Hole Principle Proof of Theorem?? If we show that there is some q with qα closer than Q to some integer, then we can use that integer as our a and be done Consider {0 mod, α mod, Qα mod } This is [ an Q ) + element set lying in the interval [0, ) There must be two in one of the Q intervals i Q, i Q, i {,, Q} Hence there exist r, s such that 0 sα mod rα mod = (s r)α mod < Q, viewed as an element of [0, ) Then there is an integer, namely that integer directly below (s r)α, such that (s r)α a < Q Our q is s r Let us now give an algorithm to construct these good approximations Concretely, the problem we want to solve is this: Problem Given a rational number α = r s and an integer Q, find a q possible subject to q Q with α a q as small as The input size for this problem is O(log r + log s + log Q) bits, and we will be interested in getting an algorithm which runs in time polynomial in this Our algorithm will be based on continued fractions (and we will use the notation we used in that section) It is not true that the solution to our rational approximation problem will be a convergent, but convergents will help us compute the solution easily We begin with a lemma on the sign of c n α Lemma 7 If n is even, c n α, else c n α Proof If n > N, this is trivial because c n = α, so we assume that n N We proceed by induction on n (showing the result for all α at the same time) 4
The lemma is clearly true for n = 0 (since c 0 = a 0 = α ) Now let n N, and assume the lemma holds for n for every α Then c n+ = a 0 + a + a n + a n+ = a 0 + c, n where c n is the n th convergent of α, the number such that α = a 0 + α c n is either at least (if n is odd) or at most α (if n is even) by our inductive assumption By similar reasoning to that showing α n > 0 for n > 0, c n > 0 Then a 0 + c is either at least α (if n + is even) or at most α n (if n + is odd) Now we characterize the best rational approximation with denominator Q Theorem 8 Let n be largest number such that Q, and let t be the greatest integer such that t + Q Then either pn is a solution to the rational approximation problem or tpn+p n t+ Proof Let L = tpn+p n t+ We will assume n is even The proof when n is odd will be very similar Because n is even, pn α and p n+ + α Note that if s = a n+, then spn+p n s+ = p n+ + ; if s = 0, then spn+p n s+ p n+ + This is because = p n As s ranges from 0 to a n+, spn+p n s+ d ds ( ) spn + p n = s + pn p det n ranges monotonically from p n (s + ) 2 ; does not change sign Hence, L must be between p n+ + and p n, both of which are at least α Hence L is at least α If r s is strictly closer to α than both c n and L, then r s I = (c n, L) For contradiction, suppose 0 < s Q Then r s p n, s because r s pn, and similarly Observe that I = I = pn tpn+p n r s L s(t + ) t+ = (t+ ) Then (t + ) s(t + ) + = ( + ) s s t + So s (t + ) + > Q, a contradiction = s ( ) tqn + + (t + ) Observation 9 We saw in the proof that if n is even, α (c n, L), and if n is odd, α (L, c n ) Further, the interval has length exactly (t+ ) Then c n α (t+ ) 5 to
As we noted before, increases monotonically until n = N As a b is the best approximation with denominator at most Q for any Q b, we cannot have > b for any n If so, then + > b as well By Theorem??, either L or p n+ + would at least as good an approximation as a b with denominator at most + For any t > 0, t+ + > +, so t = 0 Then L = pn This is impossible, because p n qn and p n+ + are reduced and therefore neither are equal to a b This also applies that N exists for α if and only if α is rational, ie the continued fraction representation of α terminates iff α is rational 3 Running Time It will be of use to bound p n and We already know that b From Remark??, we know that a p n b Hence p n qn b a + = O(a) We know that the n from Theorem?? is O(log(Q)) because the grow at least as fast as the n th entry of the Fibonacci sequence provided n N At each step in the computation of the continued fraction, we compute the floor of a rational number α i = r i s i, which is simply the number of times the denominator of α i goes into the numerator This is division with remainder, which we know to take O(log(r i ) log(s i )) operations We know α i = α i α i, which has denominator the remainder of r i under division by s i (which is strictly smaller than s i ) and numerator s i As α 0 = α = a b, we can conclude by induction that r i, s i max(a, b) and so the number of operations per step is certainly at most O((log(a) + log(b)) 2 ) Finding t can take at most O(log(Q) 2 ) steps, as we find it by division with remainder of Q by t is at most a n+, which is at most + b p n, p n = O(a) Taken together, finding L must take O(poly(log(a), log(b), log(q))) Finding whether L or c n is closer to α can also certainly take at most O(poly(log(a), log(b), log(q))), and finding the continued fraction is also O(poly(log(a), log(b), log(q))) Hence the entire running time must be O(poly(log(a), log(b), log(q))) 6