Acta Polytechnica Acta Polytechnica 53(4):322–328, 2013 © Czech Technical University in Prague, 2013 available online at http://ctn.cvut.cz/ap/ CONTINUED FRACTIONS OF SQUARE ROOTS OF NATURAL NUMBERS Ľubomíra Balkováa,∗, Aranka Hruškováb a Department of Mathematics, Faculty of Nuclear Sciences and Physical Engineering, Czech Technical University in Prague, Trojanova 13, 120 00 Praha 2, Czech Republic b Gymnázium Christiana Dopplera, Zborovská 45, 150 00 Praha 5 – Smíchov, Czech Republic ∗ corresponding author: lubomira.balkova@fjfi.cvut.cz Abstract. In this paper, we will first summarize known results concerning continued fractions. Then we will limit our consideration to continued fractions of quadratic numbers. The second author describes periods and sometimes the precise form of continued fractions of √ N, where N is a natural number. In cases where we have been able to find such results in the literature, we recall the original authors, however many results seem to be new. Keywords: continued fractions, periodic continued fractions, periods of continued fractions, quadratic numbers. 1. Introduction Continued fractions have a long history behind them — their origin may go back to the age of Euclid’s algo- rithm for the greatest common divisor, or even earlier. However, they are experiencing a revival nowadays, thanks to their applications in high-speed and high- accuracy computer arithmetics. Some of the advan- tages of continued fractions in computer arithmetics are: faster division and multiplication than with po- sitional number representations, fast and precise eval- uation of trigonometric, logarithmic and other func- tions, precise representation of transcendental num- bers, no roundoff or truncation errors ([6], Kahan’s method in [3, p. 179]). 2. Continued fractions In this section we summarize some basic definitions and results that can be found in any number theory course [1, 2, 4]. We use bxc to denote the integer part of a real number x. Definition 2.1. The continued fraction (expansion) of a real number x is the sequence of integers (an)n∈N obtained by the following algorithm x0 = x, an = bxnc, xn+1 = { 1 xn−an if xn /∈ Z, 0 otherwise. Note that a0 ∈ Z and an ∈ N. The algorithm producing the continued fraction is closely related to the Euclidean algorithm for computing the greatest common divisor of two integers. It is thus readily seen that if the number x is rational, then the algorithm eventually produces zeroes, i.e. there exists N ∈ N such that an = 0 for all n > N, thus x = a0 + 1 a1 + 1 a2 + 1 ... + 1 aN−1 + 1 aN . (1) We write x = [a0, . . . ,aN ]. On the other hand, if we want to find an expression of the form (1) with a0 ∈ Z and an ∈ N \ {0} otherwise, then there are exactly two of them – the continued fraction [a0, . . . ,aN ] and x = a0 + 1 a1 + 1 a2 + 1 ... + 1 aN−1 + 1 (aN − 1) + 1 1 . If the number x is irrational, then the sequence of the so-called convergents a0, a0 + 1 a1 , . . . ,a0 + 1 a1 + 1 a2 + 1 ... + 1 an−1 + 1 an (2) converges to x for n →∞. On the other hand, every sequence of rational numbers of the form (2) with 322 http://ctn.cvut.cz/ap/ vol. 53 no. 4/2013 Continued Fractions of Square Roots of Natural Numbers a0 ∈ Z and an ∈ N \ {0} converges to an irrational number (and for every irrational number there is only one such sequence — the sequence of convergents). We write x = [a0, . . . ,an, . . . ]. The convergents of the continued fraction are known to represent irrational numbers better than any other fractions. Theorem 2.2 (Lagrange). Let x ∈ R\Q and let pn qn be its n-th convergent (where pn and qn are coprime) and let p q with p,q ∈ Z be distinct from pn qn and such that 0 < q ≤ qn. Then∣∣∣x− pn qn ∣∣∣ < ∣∣∣x− p q ∣∣∣. It is also known how well continued fractions ap- proximate irrational numbers. Theorem 2.3. Let x ∈ R \ Q and let pn qn be its n- th convergent (where pn and qn are coprime). Then either∣∣∣x− pn qn ∣∣∣ < 1 2q2n or ∣∣∣x− pn+1 qn+1 ∣∣∣ < 1 2q2n+1 . And in a certain way, only continued fractions get very close to irrational numbers. Theorem 2.4 (Legendre). Let x ∈ R \ Q and let p q with p,q ∈ Z satisfy |x − p q | < 12q2 . Then p q is a convergent of x. 2.1. Continued fractions and continuants The convergents of continued fractions are closely related to the so-called continuants Kn(x1, . . . ,xn). Theorem 2.5. Let a0 ∈ R, ai > 0, i ∈ N. Then it holds a0 + 1 a1 + 1 ... + 1 an = Kn+1(a0,a1, . . . ,an) Kn(a1, . . . ,an) , where the polynomial Kn(x1, . . . ,xn) is given by the recurrence relation K−1 = 0, K0 = 1 and for n ≥ 1 by Kn(x1, . . . ,xn) = Kn−2(x1, . . . ,xn−2) + xnKn−1(x1, . . . ,xn−1). Corollary 2.6. Let [a0, . . . ,an, . . . ] be the continued fraction of an irrational number x. Then its n-th convergent pn qn satisfies pn = Kn+1(a0, . . . ,an), qn = Kn(a1, . . . ,an). Theorem 2.7. For every n ∈ N and a1, . . . ,an ∈ R, we have Kn(a1, . . . ,an) = Kn(an, . . . ,a1). 2.2. Continued fractions of quadratic numbers We will call a quadratic irrational an irrational root α of a quadratic equation Ax2 + Bx + C = 0, where A,B,C ∈ Z. The second root of the equation will be denoted α′ and called the (algebraic) conjugate of α. In order to state the theorem describing con- tinued fractions of quadratic irrationals, we need to recall that a continued fraction [a0, . . . ,an, . . . ] is called eventually periodic if [a0, . . . ,an, . . . ] = [a0, . . . ,ak−1,ak, . . . ,a`] starts with a preperiod a0, . . . ,ak−1 and then a period ak, . . . ,a` is repeated an infinite number of times. It is called purely periodic if [a0, . . . ,an, . . . ] = [a0, . . . ,a`], i.e., if the preperiod is empty. Theorem 2.8 (Lagrange). Let α ∈ R \ Q. The continued fraction of α is eventually periodic if and only if α is a quadratic irrational. Theorem 2.9 (Galois). Let α be a quadratic ir- rational and α′ its conjugate. The continued frac- tion of α is purely periodic if and only if α > 1 and α′ ∈ (−1, 0). Example 2.10. Let α = 1+ √ 5 2 , i.e., the so-called Golden ratio, then it is the root of x2 −x−1 = 0 and α′ = 1− √ 5 2 ∈ (−1, 0). The continued fraction of α is indeed purely periodic since α = 1 + −1 + √ 5 2 = 1 + 1 1+ √ 5 2 = 1 + 1 α , consequently α = [1]. In the sequel when we restrict our consideration to square roots of natural numbers, we will make use of the following lemma from [4]. Lemma 2.11. Let α be a quadratic irrational and α′ its conjugate. If α has a purely periodic continued fraction [a0,a1, . . . ,an], then −1α′ = [an, . . . ,a1,a0]. 3. Continued fractions of √ N Let us consider N ∈ N \ {0}. If N = k2 for some k ∈ N, then √ N = k and the continued fraction is√ N = [k]. Therefore, we limit our considerations to N ∈ N \ {0} which is not a square in the sequel. Then there exists a unique n ∈ N\{0} and a unique j ∈{1, . . . , 2n} such that N = n2 + j. The proofs of the two following theorems can be found in [4] page 15. However we repeat them here since they follow almost immediately from the previ- ous statements and they give an insight into the form of continued fractions of quadratic numbers. 323 Ľ. Balková, A. Hrušková Acta Polytechnica Theorem 3.1. For every n ∈ N \ {0} and every j ∈ {1, . . . , 2n} the continued fraction of √ n2 + j is of the form [n,a1, . . . ,ar, 2n], where a1 . . .ar is a palindrome. Proof. Denote α = n + √ n2 + j. Then α is a quadratic irrational greater than 1 and α′ = n −√ n2 + j ∈ (−1, 0). Therefore α has by Theorem 2.9 a purely periodic continued fraction, i.e., there exist a1, . . . ,ar ∈ N such that α = [2n,a1, . . . ,ar ]. It is thus evident that √ n2 + j = [n,a1, . . . ,ar, 2n]. It re- mains to prove that a1 . . .ar is a palindrome. Accord- ing to Lemma 2.11 the number −1 α′ has its continued fraction equal to [ar, . . . ,a1, 2n]. We obtain thus √ n2 + j = n + 1 −1 n− √ n2+j = n + 1 −1 α′ = [n,ar, . . . ,a1, 2n]. Since the continued fraction of irrational numbers is unique and we have√ n2 + j = [n,a1, . . . ,ar, 2n] = [n,ar, . . . ,a1, 2n], it follows that a1 = ar, a2 = ar−1 etc. Consequently, a1 . . .ar is a palindrome. Theorem 3.2. The continued fraction of the form [n,a1, . . . ,ar, 2n], where a1 . . .ar is a palindrome, cor- responds to √ N for a rational number N. Proof. Denote by x the number whose continued frac- tion equals [n,a1, . . . ,ar, 2n], i.e., x = n + 1 a1 + 1 ... + 1 ar + 1 2n + (x−n) . Hence by Theorem 2.5, x−n = Kr(a2, . . . ,ar,x + n) Kr+1(a1, . . . ,ar,x + n) = Kr−2(a2, . . . ,ar−1) + (x + n)Kr−1(a2, . . . ,ar) Kr−1(a1, . . . ,ar−1) + (x + n)Kr(a1, . . . ,ar) . By Theorem 2.7 and since a1 . . .ar is a palindrome, we have Kr−1(a1, . . . ,ar−1) = Kr−1(a2, . . . ,ar). Consequently, we obtain x = √ n2 + 2nKr−1(a1,...,ar−1)+Kr−2(a2,...,ar−1) Kr (a1,...,ar ) , where under the square root, there is certainly a ra- tional number since by their definition, continuants with integer variables are integers. In the sequel, let us study the length of the period and the form of the continued fraction of √ N =√ n2 + j in dependence on n and j, where n ∈ N \ {0} and j ∈ {1, . . . , 2n}. We will prove only some observations since the proofs are quite technical and space-demanding. The rest of the proofs may be found in [5]. In Table 1, we have highlighted all classes of n and j for which their continued fractions of √ N = √ n2 + j have been described. Observation 3.3. The continued fraction of √ N has period of length 1 if and only if N = n2 + 1. It holds then √ N = [n, 2n]. Proof. This observation has already been made in [7]. (⇐) : √ n2 + 1 = n + √ n2 + 1 −n 1 = = n + 1 √ n2 + 1 + n = n + 1 2n + √ n2 + 1 −n 1 , hence √ N = [n, 2n]. (⇒) : If the length of the period equals 1, then by Theorem 3.1 we have √ N = [n, 2n]. √ n2 + j = n + (√ n2 + j −n ) = n + 1 2n + √ n2 + j −n , hence we have √ n2 + j −n = 1 2n + √ n2 + j −n , j√ n2 + j + n = 1√ n2 + j + n , j = 1. Observation 3.4. The continued fraction of √ N has period of length 2 if and only if 2n j is an integer. It holds then √ N = [n, 2n j , 2n]. Proof. (⇐):√ n2 + j = n + ( √ n2 + j −n) = n + j√ n2 + j + n = n + 1 2n j + √ n2 + j −n j , = n + 1 2n j + 1√ n2 + j + n , = n + 1 2n j + 1 2n + ( √ n2 + j −n) , thus √ N = [n, 2n j , 2n]. 324 vol. 53 no. 4/2013 Continued Fractions of Square Roots of Natural Numbers Table 1. All classes of n ≤ 40 (first column) and j ≤ 31 (first row) for which their continued fractions of√ N = √ n2 + j have been described are highlighted. (⇒): If the length of the period equals 2, then by Theorem 3.1 we have √ N = [n,x, 2n].√ n2 + j = n + ( √ n2 + j −n) = n + 1 x + 1 2n + ( √ n2 + j −n) , hence we have √ n2 + j −n = 1 x( √ n2 + j + n) + 1√ n2 + j + n , x = 2n j . Observation 3.5. The continued fraction of √ N has period of length 3 if and only if j = 4ak + 1 and n = aj + k for some a,k ∈ N, a ≥ 1, k ≥ 1. It holds then √ N = [n, 2a, 2a, 2n] and 5 ≤ j ≤ n− 1. Proof. (⇒) : If the length of the period equals 3, then by Theorem 3.1 we have √ N = [n,x,x, 2n]. √ n2 + j = n + 1 x + 1 x + 1 2n + ( √ n2 + j −n) , hence we get j = 2xn + 1 x2 + 1 . Since j is an integer, x must be even. Furthermore, as j 6= 1 by Observa- tion 3.3, there exists a ≥ 1 such that x = 2a. It follows then from j = 4an + 1 4a2 + 1 that n = aj + j−14a . Since n is an integer, we obtain finally j = 4ak + 1 and n = aj + k for some k ≥ 1. It is easy to verify that j ≥ 5 and j ≤ n− 1. (⇐) : The reverse implication is only an exercise in manipulation with square roots and integer parts. We have it for the reader. In order to save space in the proofs, let us introduce 325 Ľ. Balková, A. Hrušková Acta Polytechnica n j √ N ` = 4 2k + 1, k ≥ 2 2n− 3 [n, 1, n−32 , 1, 2n] 2k + 1, k ≥ 1 3n+12 [n, 1, 2, 1, 2n] 3k + 2 5n+23 [n, 1, 4, 1, 2n] 3k + 2, k ≥ 1 2n− 2 [n, 1, 2n−43 , 1, 2n] 3k + 2 4n+13 [n, 1, 1, 1, 2n] 5k + 2, k ≥ 1 n− 1 [n, 2, 2n−45 , 2, 2n] 5k + 4 8n+35 [n, 1, 3, 1, 2n] 6k + 1, k ≥ 1 5n+16 [n, 2, 2, 2, 2n] 6k + 5 2n−13 [n, 3, n−1 2 , 3, 2n] 9k + 4, k ≥ 1 n− 2 [n, 2, 2n−89 , 2, 2n] ` = 5 2k + 1, k ≥ 1 4 [n, n−12 , 1, 1, n−1 2 , 2n] 5k + 3 6n+25 [n, 1, 1, 1, 1, 2n] ` = 6 2k, k ≥ 2 2n− 3 [n, 1, n2 − 1, 2, n 2 − 1, 1, 2n] 10k + 7 2n+15 [n, 4, 1, n−3 2 , 1, 4, 2n] 3k + 1, k ≥ 1 2n+13 [n, 2, 1,n− 1, 1, 2, 2n] 3k + 1, k ≥ 1 n + 1 [n, 1, 1, 2n−23 , 1, 1, 2n] 3k + 1, k ≥ 1 4n+23 [n, 1, 2,n, 2, 1, 2n] 6k + 4 7n+26 [n, 1, 1, 2, 1, 1, 2n] 7k + 3, k ≥ 1 n + 2 [n, 1, 1, 2n−67 , 1, 1, 2n] ` = 8 4k + 1, k ≥ 2 2n− 7 [n, 1, n−54 , 2, n−1 2 , 2, n−5 4 , 1, 2n] 6k, k ≥ 1 4n3 [n, 1, 1, 1, n−2 2 , 1, 1, 1, 2n] 6k + 2, k ≥ 1 2n−13 [n, 3, n−2 2 , 1, 4, 1, n−2 2 , 3, 2n] 7k + 5 8n+27 [n, 1, 1, 3,n, 3, 1, 1, 2n] 9k + 3, k ≥ 1 9 [n, 2n−69 , 1, 2, 2n−6 9 , 2, 1, 2n−6 9 , 2n] 9k + 6, k ≥ 1 9 [n, 2n−39 , 2, 1, 2n−12 9 , 1, 2, 2n−3 9 , 2n] ` = 10 6k + 3, k ≥ 1 4n3 [n, 1, 1, 1, n−1 2 , 6, n−1 2 , 1, 1, 1, 2n] 9k + 6 10n+39 [n, 1, 1, 3, 1,n− 1, 1, 3, 1, 1, 2n] 10k + 5, k ≥ 1 4n5 [n, 2, 1, 1, n−1 2 , 10, n−1 2 , 1, 1, 2, 2n] Table 2. Lengths ` of periods and the form of continued fractions for several classes. the following notation 〈a0,a1, . . . ,aN−1,aN〉 = a0 + 1 a1 + 1 ... + 1 aN−1 + 1 aN , where ai ∈ N for i ∈{0, 1, 2, . . . ,N − 1}, but aN ∈ R. Observation 3.6. Let j = 4. If n is even, then the length of the period is 2 and √ N = [ n, 2n j , 2n ] . If n is odd, then the length of the period is 5 and√ N = [ n, n−12 , 1, 1, n−1 2 , 2n ] . Proof. If n is even, then 2n j is an integer and the statement is a corollary of Observation 3.4. If n is odd, it holds√ n2 + 4 = n + (√ n2 + 4 −n ) = 〈 n, √ n2 + 4 + n 4 〉 = 〈 n, n− 1 2 , √ n2 + 4 + n− 2 n 〉 = 〈 n, n− 1 2 , 1, √ n2 + 4 + 2 n 〉 = 〈 n, n− 1 2 , 1, 1, √ n2 + 4 + n− 2 4 〉 = 〈 n, n− 1 2 , 1, 1, n− 1 2 , 2n + ( √ n2 + 4 −n) 〉 , thus √ N = [n, n−12 , 1, 1, n−1 2 , 2n]. Observation 3.7. For n > 1 and j = 2n − 1 the length of the period is 4 and the continued fraction is then √ N = [n, 1,n− 1, 1, 2n]. Proof.√ n2 + 2n− 1 = n + (√ n2 + 2n− 1 −n ) = 〈 n, √ n2 + 2n− 1 + n 2n− 1 〉 326 vol. 53 no. 4/2013 Continued Fractions of Square Roots of Natural Numbers = 〈 n, 1, √ n2 + 2n− 1 + (n− 1) 2 〉 = 〈 n, 1,n− 1, √ n2 + 2n− 1 + (n− 1) 2n− 1 〉 = 〈 n, 1,n− 1, 1, 2n + (√ n2 + 2n− 1 −n )〉 , hence √ N = [n, 1,n− 1, 1, 2n] Observation 3.8. For n > 3 and j = 2n− 3, either the length of the period is 4 if n is odd and the con- tinued fraction is then √ N = [ n, 1, n−32 , 1, 2n ] , or the length of the period is 6 if n is even and the continued fraction is then √ N = [n, 1, n2 − 1, 2, n 2 − 1, 1, 2n]. Proof. For n odd:√ n2 + 2n− 3 = n + (√ n2 + 2n− 3 −n ) = 〈 n, √ n2 + 2n− 3 + n 2n− 3 〉 = 〈 n, 1, √ n2 + 2n− 3 − (n− 3) 4 〉 = 〈 n, 1, n− 3 2 , √ n2 + 2n− 3 + (n− 3) 2n− 3 〉 = 〈 n, 1, n− 3 2 , 1, 2n + (√ n2 + 2n− 3 −n )〉 , thus √ N = [ n, 1, n−32 , 1, 2n ] . For n even:√ n2 + 2n− 3 = n + (√ n2 + 2n− 3 −n ) = 〈 n, √ n2 + 2n− 3 + n 2n− 3 〉 = 〈 n, 1, √ n2 + 2n− 3 − (n− 3) 2n− 3 〉 = 〈 n, 1, n 2 − 1, √ n2 + 2n− 3 + (n− 1) n− 1 〉 = 〈 n, 1, n 2 − 1, 2, √ n2 + 2n− 3 + (n− 1) 4 〉 = 〈 n, 1, n 2 − 1, 2, n 2 − 1, √ n2 + 2n− 3 + (n− 3) 2n− 3 〉 = 〈 n, 1, n 2 − 1, 2, n 2 − 1, 1, 2n + ( √ n2 + 2n− 3 −n) 〉 . Thus √ N = [n, 1, n2 − 1, 2, n 2 − 1, 1, 2n]. The following table will include all remaining cases of continued fractions of √ n2 + j that we were able to determine in terms of n and j. Observation 3.9. Let k ∈ N. Let us summarize in Table 2 the lengths ` of periods and the form of continued fractions for several classes (described in an analogous way) of n and j. The next observation was made in a different way than all previous ones. We prescribed the form of the continued fraction and searched for √ N having such a continued fraction. Observation 3.10. If the period of the continued fraction of √ N = √ n2 + j contains p ≥ 1 ones as its palindromic part, i.e., √ N = [n, 1, . . . , 1︸ ︷︷ ︸ p , 2n] then n = kFp + Fp+1 2 for some k ∈ N, p + 1 6= 3`, where ` ∈ N, and j = 2nFp−1+Fp−2 Fp , where Fn denotes the n- th Fibonacci number given by the recurrence relation F−1 = 0, F0 = 1 and Fn = Fn−2 +Fn−1 for all n ≥ 1. Proof. It is a direct consequence of the proof of The- orem 3.2 and the definition of continuants. The last observation is also of a different form than the previous ones since j and n depend on two pa- rameters. Observation 3.11. Let n = 4ka + 2a, where k,a ∈ N,k ≥ 1,a ≥ 1, and j = 8a. Then the continued fraction of √ N = √ n2 + j equals [ n, 4n− j 2j , 1, 1, n− 2 2 , 1, 1, 4n− j 2j , 2n ] . Proof. The proof may be found in [5]. We also made one conjecture that turned out to be false. Conjecture 3.12. For √ N the length of the period of the continued fraction is less than or equal to 2n. This observation was made when contemplating a table of periods of √ N for N ≤ 1000. However, in [8] it is shown that for N = 1726 with n = 41, the period of the continued fraction of √ N is of length 88 > 82 = 2n. A rougher upper bound comes from [7]. Theorem 3.13. For √ N the length of the period of the continued fraction is less than or equal to 2N. Let us terminate with two conjectures that have not been proved yet. Conjecture 3.14. No element of the period of √ N apart from the last one is bigger than n. Conjecture 3.15. There is no period of an odd length for j = 4k + 3, where k ∈ N. Acknowledgements The first author acknowledges financial support from Czech Science Foundation grant 13-03538S. References [1] Klazar, M.: Introduction to Number Theory, KAM-DIMATIA Series 782, 2006. [2] Masáková, Z., Pelantová, E.: Teorie čísel, skriptum ČVUT, 2010. [3] Muller, J.-M.: Elementary Functions: Algorithms and Implementation, 2nd edition, Birkhäuser Boston, 2006. [4] Lauritzen, N.: Continued fractions and factoring, http://home.imf.au.dk/niels/cfracfact.pdf, 2009. 327 Ľ. Balková, A. Hrušková Acta Polytechnica [5] Hrušková, A.: Řetězové zlomky kvadratických čísel, SOČ práce, http://bimbo.fjfi.cvut.cz/∼soc/, 2013–2014 [6] Seidensticker, R.: Continued fractions for high-speed and high-accuracy computer arithmetic, IEEE Symposium on Computer Arithmetic (1983), 184–193 [7] Sierpinski, W.: Elementary Theory of Numbers, Panstwowe Wydawnictwo Naukowe, Warszawa, 1964. [8] Hickerson, D. R.: Length of period of simple continued fraction expansion of √ d, Pacific Journal of Mathematics 46(2) (1973), 429–432 328 Acta Polytechnica 53(4):322–328, 2013 1 Introduction 2 Continued fractions 2.1 Continued fractions and continuants 2.2 Continued fractions of quadratic numbers 3 Continued fractions of sq. root of N Acknowledgements References