New modular multplcaton and dvson algorthms based on contnued fracton expanson Mourad Goucem a a UPMC Unv Pars 06 and CNRS UMR 7606, LIP6 4 place Jusseu, F-75252, Pars cedex 05, France Abstract In ths paper, we apply results on number systems based on contnued fracton expansons to modular arthmetc. We provde two new algorthms n order to compute modular multplcaton and modular dvson. The presented algorthms are based on the Eucldean algorthm and are of quadratc complexty.. Introducton Contnued fractons are commonly used to provde best ratonal approxmatons of an rratonal number. Ths sequence of best ratonal approxmatons (p /q ) N s called the convergents sequence. In the begnnng of the 20 th century, Ostrowsk ntroduced number systems derved from the contnued fracton expanson of any rratonal α []. He proved that the sequence (q ) N of the denomnators of the convergents of any rratonal α forms a number scale, and any nteger can be unquely wrtten n ths bass. In the same way, the sequence (q α p ) N also forms a number scale. In ths paper, we show how such number systems based on contnued fracton expansons can be used to perform modular arthmetc, and more partcularly modular multplcaton and modular dvson. The presented algorthms are of quadratc complexty lke many of the exstng mplemented algorthms [2, Chap. 2.4]. Furthermore, they present the advantage of beng only based on the extended Eucldean algorthm, and to ntegrate the reducton step. In the followng, we wll frst ntroduce notatons and some propertes of the number systems based on contnued fracton expansons n Secton 2. Then we descrbe the new algorthms n Secton 3. Fnally, we gve elements of complexty analyss of these algorthms n Secton 4, and perspectves n Secton 5. 2. Number systems and contnued fractons 2.. Notatons Frst, we gve some notatons on the contnued fracton expanson of an rratonal α wth 0 < α < [3]. We call the tals of the contnued fracton
expanson of α the real sequence (r ) N defned by r 0 = α, r = /r /r. We denote (k ) N the nteger sequence of the partal quotents of the contnued fracton expanson of α. They are computed as k = /r. We have α = k + k 2 +... + k + r := [0; k, k 2,..., k + r ]. We wrte p /q the th convergent of α. The sequences (p ) N and (q ) N are nteger valued and postve, p q = [0; k, k 2,..., k ]. We wll also wrte (θ ) N the postve real sequence of ( ) (q α p ) whch we call the sequence of the partal remanders as they are related to the tals by r = θ /θ. Hereafter, we recall the recurrence relatons to compute these sequences, p = p 0 = 0 p = p 2 + k p, q = 0 q 0 = q = q 2 + k q, θ = θ 0 = α θ = θ 2 k θ. We also wrte η = q α p the sequence of the sgned partal remanders, whch elements are of sgn ( ). The sequence (η ) N of the sgned partal remanders can be computed as (( ) θ ) N. 2.2. Related number systems over rratonal numbers In ths secton, we present two number systems based on the sequences of the sgned partal remanders (η ) N and the denomnators of the convergents (q ) N of an rratonal α. They have been extensvely studed durng the second part of the 20 th century [, 4]. Property 2. ([, Proposton ]). Gven (q ) N the denomnators of the convergents of any rratonal 0 < α <, every postve nteger N can be unquely wrtten as m N = + n q { 0 n k where, 0 n k, for 2, n = 0 f n + = k + ( Markovan condtons). 2
Algorthm : Integer decomposton n Ostrowsk number system. nput : N N, (q ) <m m output: n such that N = + n q tmp N ; 2 m; 3 whle do 4 n tmp/q ; 5 tmp tmp n q ; 6 ; Ths number system assocated to the (q ) N s named the Ostrowsk number system. To wrte an nteger n ths number system, we use a classcal decomposton algorthm (Algorthm ). The rank m s chosen such that q m > N. Property 2.2 ([, Proposton 2]). Gven (η ) N the sequence of the sgned partal remanders of any rratonal 0 < α <, every real β, wth 0 β < can be unquely wrtten as + β = α + b η { 0 b k where, 0 b k, for 2, b = 0 f b + = k + ( Markovan condtons). There also exsts two other number systems that are dual to these two. One decomposes ntegers n the bass (( ) q ) N and the other decomposes reals n the bass of the unsgned partal remanders (θ ) N []. The second Markovan condton then becomes b + = 0 f b = k. An algorthm to wrte real numbers n the (θ ) N number scale has been proposed by Ito [5]. It proceeds by teratng the mappng T : (α, β) (/α /α, β/α β/α ). 2.3. Related number systems over ratonal numbers In ths subsecton, we consder α = p/q ratonal. We recall that the contnued fracton expanson of a ratonal s fnte. We denote p q = [0; k, k 2,..., k n ] the contnued fracton expanson of p/q, and recall p n = p and q n = q. The Ostrowsk number system stll holds for ntegers N < q n, snce the keypont n the Ostrowsk number system s that there exsts q m such that q m > N. The (η ) <n number system also stll holds under one supplemental condton: β must be ratonal wth precson at most q (.e. the denomnator of β must be less or equal than q). 3
3. Modular arthmetc and contnued fracton In ths secton, we consder α = a/d. We hghlght that the same decomposton (b,..., b n+ ) can be nterpreted n two ways dependng on the number system used. In the Ostrowsk number system, we obtan an nteger N whereas n the number scale (η ) N, we obtan the reduced value of Nα mod []. Hence, we wll use the fact that studyng an nteger a modulo d s smlar to consderng the ratonal a/d modulo. Ths enables us to use propertes 2. and 2.2 to compute modular multplcaton and dvson. 3.. Modular arthmetc and contnued fracton Frst, we brefly recall how contnued fracton expanson and the Eucldean algorthm are lnked. We wrte (θ ) N the nteger sequence of remanders when computng gcd(a, d). Ths sequence s composed of decreasng values less than d. We also wrte (η ) N the sequence (( ) θ ) N. We obtan the followng recurrence relaton, and recall the recurrence relaton over the (θ ) N sequence of partal remanders of the contnued fracton expanson of a/d : θ = d θ 0 = a θ = θ 2 θ 2 /θ θ θ = θ 0 = a/d θ = θ 2 θ 2 /θ θ. It s wdely known and can be easly proved by nducton that both sequences compute the same partal quotents, that we wll note k. Proof of k + = θ /θ = θ /θ. We prove t by provng θ /θ = θ /θ. Base case : θ /θ 0 = d/a = θ /θ 0 Inducton : Let such that θ /θ = θ /θ. θ θ θ + + θ /θ θ θ = θ θ = θ + + θ /θ θ θ whch mples θ /θ + = θ /θ +. θ + + θ /θ = θ + θ θ + θ /θ It can also be notced that η = η d. Actuallly, θ = θ d as the extended Eucldean algorthm compute the relatons θ = ( ) (q a p d). In partcular, t gves the Bezout s dentty wth θ n = ( ) n (q n a p n d) = gcd(a, d), and q n the nverse of a f a s nvertble modulo d (gcd(a, d) = ). 4
3.2. Modular multplcaton Now, gven a, b Z/dZ, we wrte c = a b mod d the nteger 0 c < d such that ab ab/d d = c. We can observe that the decompostons presented n propertes 2. and 2.2 are both unque and both need the same Markovan condton over ther coeffcents. Hence, we can nterpret the same decomposton n both bass. Theorem 3.. Gven a, b Z/dZ, and (q ) n, (η ) n from Eucldean algorthm on a and d, f we wrte b n the (q ) n number scale as then n+ b = + b q, n+ a b mod d = a + b η. Proof. Frst, we consder b < q n, t can be wrtten n the Ostrowsk number system as n b = + b q, and the coeffcents b respect the Markovan condton of the Ostrowsk number system. Hence, n α b = α + b q α. By defnton, η = q α p, thus α b = α + n b η + n b p. As the coeffcents b s verfy the Markovan condton, the unqueness of the decomposton n property 2.2 gves 0 α + n b η < and n b p N. Hence, n α b mod = α + b η. By multplyng ths nequalty by d, as α = a/d and η = η d, we obtan a b mod d = a + n b η. whch fnalzes the proof of the theorem for b < q n. Now f b q n and b = b n+ q n + b wth b < q n the remander of the dvson of b by q n, b can be unquely wrtten n the Ostrowsk number system. Furthermore, as η n = 0, b n+ η n = 0, whch fnshes the proof. 5
3.3. Modular dvson Inversely, gven a, b Z/dZ, wth a nvertble modulo d (gcd(a, d) = ) we can effcently compute a b mod d. Theorem 3.2. Gven a, b Z/dZ wth gcd(a, d) =, and (q ) n, (θ ) n from Eucldean algorthm on a and d, f we wrte b n the (θ ) <n number scale as n+ b = b θ, n+ then f we denote c = b ( ) q, a b mod d {c, d + c}. Proof. The proof of correctness s smlar to the one of theorem 3., usng the facts that θ = θ d and that θ = ( ) (q α p ). Now, the greatest nteger c s clearly the one assocated to the decomposton (k, 0, k 3, 0,..., k n ) when n s odd. However, k q = q q 2 by defnton, whch mples (n )/2 =0 k 2+ q 2 = q n. The smallest nteger that can be returned s clearly the one assocated to the decomposton (0, k 2, 0, k 4,..., k n ) when n s even. Once agan, as k q = q q 2, we get n/2 k 2 q 2 = q n. Hence, d < n+ b ( ) q < d, that s to say, the result needs at most a correcton by an addton by d. We menton that we also tred to decompose b n the (η ) n sgned remanders number scale and evaluate ths same decomposton n the (q ) n number scale to compute modular dvson. We used Ito T 2 transform [5] T 2 : (α, β) (/α /α, β/α β/α). In practce, t returns the rght result wthout the need of any correcton. However, as the decomposton computed by Ito T 2 transform does not verfy the same Markovan condtons as n the Ostrowsk number system, we were not able to gve a theoretcal proof that t always returns the reduced result of the modular dvson. 4. Elements of Complexty Analyss In ths secton, we ntroduce elements of complexty analyss of the proposed modular multplcaton algorthm based on theorem 3.. The same analyss holds for the dvson. 6
0.95 Probablty 0.9 0.85 0.8 0.75 0.7 0 5 0 5 20 25 30 35 40 45 Max expected b n+ Fgure : Probablty law of the value of the coeffcent b n+ Frst, the algorthm computes (q ) n and (η ) n. Ths can be computed usng the classcal extended Eucldean algorthm n O(log (d) 2 ) bnary operatons. We notce here that the dvsons computed n the Eucldean algorthm can be computed by subtracton as the mean computed quotent equals to Khnchn s constant (approxmately 2.69) [3, p. 93]. Furthermore, bg quotents are very unlkely to occur as the quotents of any contnued fracton follow the Gauss- Kuzmn dstrbuton [3, p. 83] [6, p. 352], ) P(k = k) = log 2 ( (k + ) 2. Second, the decomposton n (q ) n as n algorthm also clearly has complexty n O(log (d) 2 ). By the same arguments, the coeffcents of the decomposton n (q ) n can be computed by subtracton as they are lkely small. The only quotent not followng the Gauss-Kuzmn dstrbuton s the coeffcent b n+ as t corresponds to the quotent b/q n. We prove n AppendxA that f a, d are unformly chosen ntegers n [, N] and b s unformly chosen n [, d], then when N tends to nfnty, P(b n+ k) tends to [ k+ ζ(2) (k + ) 3 + (k + )ζ(3) Fgure shows the probablty dstrbuton of P(b n+ k). In partcular, we obtan P(b n+ 3) 92.5%. To fnsh the complexty analyss, evaluatng the sum to return the fnal result can also be done n O(log (d) 2 ). ]. 7
5. Perspectves In ths paper, we presented an algorthm for modular multplcaton and an algorthm for modular dvson. Both are based on the extended Eucldean algorthm and are of quadratc complexty n the sze of the modulus. Furthermore, the two stated theorems mply that, knowng the remanders generated when computng the gcd of a number a and the modulus d, one can compute effcently reduced multplcatons by a or a. Ths can be useful n algorthms computng several multplcatons and/or dvsons by the same number a, as n the Gaussan elmnaton algorthm for example. The presented algorthms can also be useful n hardware mplementaton of modular arthmetc. They allow to perform nverson, multplcaton and dvson wth the same crcut. Further nvestgatons have to be led to fnd optmal decomposton algorthms, that mnmze the number of coeffcents of the produced decomposton and ther sze. Also, we are workng on an effcent software mplementaton of these algorthms. 6. Aknowledgement Ths work was supported by the TaMaD project of the french ANR (grant ANR 200 BLAN 0203 0). Ths work has also been greatly supported and mproved by many helpful proof readngs and dscussons wth Jean-Claude Bajard, Valére Berthé, Perre Fortn, Stef Grallat and Emmanuel Prouff. References [] V. Berthé, L. Imbert, Dophantne approxmaton, Ostrowsk numeraton and the double-base number system, Dscrete Mathematcs & Theoretcal Computer Scence () (2009) 53 72. [2] R. Brent, P. Zmmermann, Modern computer arthmetc, Vol. 8, Cambrdge Unversty Press, 200. [3] A. Y. Khnchn, Contnued fractons, Dover, 997. [4] A. Vershk, N. Sdorov, Arthmetc expansons assocated wth a rotaton of the crcle and wth contnued fractons, Sant Petersburg Mathematcal Journal 5 (6) (994) 2-36. [5] S. Ito, Some skew product transformatons assocated wth contnued fractons and ther nvarant measures, Tokyo Journal of Mathematcs 9 () (986) 5 33. [6] D. E. Knuth, The Art of Computer Programmng, 2nd Edton, Vol. 2 (Semnumercal Algorthms), Addson-Wesley, 98. [7] G. H. Hardy, E. M. Wrght, An Introducton to the Theory of Numbers, 6 th Edton, Oxford Unversty Press, 2008. 8
AppendxA. Detaled proof of the dstrbuton functon of {b n+ < k}. Let U, U 2 and U 3 be three ndependent unform dstrbutons over [0, ]. We wrte a = U N, d = U 2 N and b = U 3 d. We denote A = {b < (k + )q n }, B = {gcd(a, d) k + }, B = {gcd(a, d) > k + } and B = {gcd(a, d) = }. Hence usng the law of total probablty we have P(A) = P(A B) + P(A B), = P(A B ) + k+ = k+ >k+ P(A B ) P(B ) + As the B are dsjont events, we have P(A) = k+ P(A B ) P(B ) + P(A B ), >k+ + =k+2 P(A B ) P(B ). P(A B ) P(B ). Frst, P(A B ) = for k + as b < d = gcd(a, d) q n (k + ) q n. Hence, k+ + P(A) = P(B ) + P(A B ) P(B ). =k+2 Now we want to determne P(A B ) for k + 2. Q ( ) = P( B ) and Hereafter, we wrte P(A B ) = Q (A), N N = Q ({a = l} {d = m}) Q (A {a = l} {d = m}). However, l= m= Q (A {a = l} {d = m}) = k + as b s unformly dstrbuted between and d = q n. If we consder the segment of length d and slce t n segments of length q n, t can be nterpreted as the probablty that b s n the frst k + slces. Hence P(A B ) = N N l= m= = k + Q ({a = l} {d = m}) k +, N l= m= N Q ({a = l} {d = m}). As {a = l} and {d = m} are ndependent by hypothess (U and U 2 are ndependent), Q ({a = l} {d = m}) = Q ({a = l}) Q ({d = m}), 9
and P(A B ) = k + N N Q ({a = l}) Q ({d = m}). l= Now, we use the fact that the sum of the probabltes over the whole sample space always sum to to obtan m= If we recaptulate, P(A B ) = k +. P(A) = k+ P(B ) + + =k+2 k + P(B ). Fnally, t s wdely known that P(B ) tends to ζ(2) 2 nfnty [7, p. 353]. Hence, we get when N tends to whch equals to k+ lm P(A) = N + ζ(2) 2 + =k+2 k + [ k+ = ζ(2) + 2 + (k + ) ζ(2) 2, =k+2 [ k+ ( ζ(2) + k+ + (k + ) 2 3 [ k+ = ζ(2) (k + ) 3 + (k + ) By defnton, Remann zeta functon equals ζ(s) = ( + 3 ] )], Hence we get the followng smplfcaton, whch s more convenent for computaton and has been used to generate Fg., lm N + P(A)=ζ(2) [ k+ + s. 3 3 )] ] (k + ) 3 +(k+) ζ(3).., 0