CS681: Computational Numer Theory and Algera (Fall 009) Lecture 1 & : Integer and Modular Arithmetic July 30, 009 Lecturer: Manindra Agrawal Scrie: Purushottam Kar 1 Integer Arithmetic Efficient recipes for performing integer arithmetic are indispensale as they are widely used in several algorithms in diverse areas such as cryptology, computer graphics and other engineering areas. Hence our first oject of study would e the most asic integer operations - namely addition, sutraction, multiplication and division. We will start off with algorithms that are typically referred to as high-school or peasant algorithms and move on to more efficient ones wherever scope for improvement is found. 1.1 Integer Addition and Sutraction Given two n-it numers a and, the high-school algorithms take O (n) time to perform addition and sutraction. Now this is optimal upto constant factors since the input size itself is Ω(n). Hence these do not pose a challenge to us. Of course for someone designing a processor, these constant factors may decide whether the chip ecomes a market leader or not ut we shall consider the issue of integer addition and sutraction closed from now on having otained optimal algorithms for the same (many thanks to our respective high school Math teachers for this). 1. Integer Multiplication Unfortunately we have less to thank our high school teachers on this matter since the high school algorithm takes O (n ) time to multiply two n-it numers. This is too slow for most scientific calculations. A slight improvement comes due to an oservation of Gauss on multiplication of complex numers. Gauss oserved that the product (a+i)(c+di) can e calculated using just three multiplications and five additions instead of four multiplications and four additions. Let k 1 = ac, k = d, k 3 = (a + )(c + d). Then (a + i)(c + di) = (k 1 k ) + (k 3 k 1 k )i. This was the trick employed y Karatsua and Ofman in [KO6] to arrive at an O ( n log 3) algorithm for integer multiplication. What is done in this algorithm is to express each n-it numer using two n -it numers. Thus a = a 1 n + a 0 and = 1 n + 0. Hence we can use the aove trick to multiply these two numers using three n -it multiplications and some constant numer of O (n) operations which include adding the intermediate results, left shifts etc. So the time complexity of this algorithm is M(n) = 3M ( ) ( ) n + O (n) which gives M(n) = O n log 3. 1
This method of reaking up numers can e generalized to reak numers into more parts and save on some multiplications using similar tricks. This led to a family of multiplication algorithms known as Toom-Cook methods [Too63, Coo66] which give an asymptotic time complexity of O (n log(k 1) log(k) ) for any k > 1 and hence can give a complexity of O (n 1+ɛ ) for any ɛ > 0. This was improved y Schönage and Strassen using Spectral techniques (more specifically Fourier Analysis) to O (n log n log log n) in [SS71]. After this came a long period of waiting efore Fürer and De-Kurur-Saha-Saptharishi in a series of closely spaced papers [F 07, DKSS08] improved the time complexity to O ( n log n log n ) where log denotes the iterated log function. Of course one does not typically use the fastest method on all instances. As it turns out the algorithms y Fürer and De et al outperform the Schönage-Strassen algorithm in practice only on astronomically large numers due to the large constants involved. Thus for really huge numers, one uses the algorithms due to Fürer and De et al at the top level ut as the recursive calls are made to smaller numers, one switches to the Schönage-Strassen algorithm only to switch to the Karatsua algorithm for still smaller numers and finally to the high school algorithm for the ottom level recursive calls. 1.3 Integer Division As it turns out, if asymptotic complexity is all that we are interested in then integer division has the same time complexity as integer multiplication. In fact the operations of multiplication, squaring, division, inversion and square-rooting all have the same time complexity. However in this lecture our aim is only to show that division is no more difficult than multiplication. For this we require the Newton-Raphson method for inversion. This method is used in general to find roots of a real valued function f y starting with a wellinformed guess x 0 for the root and improving it iteratively using the update rule x i+1 = x i f(x i) f (x i ). The method typically has a quadratic convergence ut is also known to go astray at times. But in our case we will show that it approaches its goal in a steadfast manner. We shall of course use a slightly modified version of the algorithm keeping in mind not to keep very high precision numers. Note that all logarithms are taken to ase unless otherwise mentioned. Let us take the two n-it integers to e divided as a and where < a. We want to find integers d and r such that a = d + r and r <. We will always work only with positive integers for simplicity. For now let us assume that has een scaled down to the range ( 1, 1) y dividing y an appropriate power or. For this assume >. The case = is clearly very simple to handle. From now on, till otherwise mentioned, we shall call this scaled down n-it numer as. Our goal here is to find a good approximation of 1. Note that 1 (1, ). Let us choose x 0 = 1 as our first approximation. Clearly x 0 < 1 and 1 x 0 < 1. Truncate 1 x 0 to the first it after the decimal point and call this quantity y 0. Clearly y 0 < 1 as well. Define y i = yi 1 and x i+1 = x i (1 + y i ) for all i > 0. Consider the following results : Lemma 1.1. For any i > 0, we have the following to e true 1. y i < 1 i
. 1 x i < 1 i 3. y i is at most i its long 4. x i is at most i its long Proof. We present the respective proofs elow. All proofs are y induction on i. 1. For i = 0 we have already shown the result. Suppose the result also holds upto some i, then we have y i+1 = y i < 1 i+1.. For i = 0 we have already shown the result. Suppose the result also holds ( upto some ) i, then we have 1 x i+1 = 1 x i (1 + y i ) = 1 x i x i y i < 1 1 + 1 ( ) i i y i < 1 1 1 + 1 = 1. i i i i+1 3. For i = 0 this is true ecause of choice of y 0. Suppose the result also holds upto some i, then we have y i+1 = y i and hence the numer of its in y i+1 can e atmost twice that of y i. Hence the result holds. 4. For i = 0 this is true y choice of x 0. Suppose the result also holds upto some i, then we have x i+1 = x i (1 + y i ). Since y i < 1 and oth x i and y i are at most i its long, the result holds. At this point we fix the issue of scaling to within the range ( 1, 1). Let m > 0 such that = [ 1, 1). Then it is clear that if x is such that 1 x < ɛ then if x = x then 1 x < ɛ m m as well. Now we state a final lemma efore moving on to prove our desired result. Lemma 1.. Fix i = log n + 1. Then if a = d + r then a x i d 1. Proof. Clearly a x i d = d( x i 1) + r x i < d + r d + 1 1 i i and log d n. 1 since r <, log n The preceding lemmata give us an O (M(n)) algorithm for integer division where M(n) is the asymptotic complexity for integer multiplication. This can e shown as follows : at the i-th iteration, all we do is multiply and add i -it numers (this includes the squaring step). This takes M (O ( i )) + O ( i ) time which is O (M ( i )) since M (O (n)) = O (M(n)) and M(n) = Ω(n). Another outcome of M(n) = Ω(n) is that M(a) + M() M(a + ) for all a, 1. In the eginning we calculate 1 x 0 which takes O (M(n)) time. The calculations at the end to find d from x log n+1 also takes O (M(n)) time. Due to some uncertainty in the value of d, we may have to try out a constant numer of values in the vicinity of the d otained. All this will take time O (M(n)). Hence integer division is no more difficult than integer multiplication. 3
Note: There are some minute details of the algorithm that need to e taken care of. It can e seen that the algorithm will fail if y 0 = 0. However this is actually a good thing for us ecause it tells us that our very first approximation is as good as something we hoped to get several iterations down 1 the line. Thus if 1 x l 1 0 > 1, then we can retain log l its of 1 x l 0 and call this the log l -th iteration and proceed. Modular Arithmetic In this section we will deal with how to do the four asic integer operations on n-it numers a and modulo another n-it numer m. It should e clear that modular addition, sutraction and multiplication can e done in O (M(n)) time. As far as division is concerned we first have to define what does it mean for a to e z mod m. Definition.1. For any three integers a, and c, a mod m is said to e defined if there exists an integer z such that z a mod m. In such a situation we say a z mod m. a Lemma.. mod m for integers a and such that (a, ) = 1 is defined iff (m, ) = 1. = rm + z where z Z and r Q such that Proof. ( ) Suppose a mod m is defined and a r Z. Hence a = rm + z. Now suppose (m, ) > 1. This implies from the aove equation that (a, ) > 1 as well which is contradictory to our assumption. Hence (m, ) = 1. ( ) Suppose (m, ) = 1, hence there exist p, q Z such that pm + q = 1. Hence apm + aq = a which implies (aq) a mod m. Hence a mod m is defined. Using the aove lemma we can easily find a mod m in O (n ) time. First using the extended Euclid s algorithm determine if (m, ) = 1. If this is indeed the case then the algorithm will also output the pair (p, q) and clearly q 1 mod m. Hence aq 1 mod m. Since the Euclid s algorithm works in quadratic time and M(n) = o(n ) hence the algorithm takes only O (n ) time. References [Coo66] Stephen A. Cook. On the Minimum Computation Time of Functions. PhD thesis, Department of Mathematics, Harvard University, 1966. [DKSS08] Anindya De, Piyush Kurur, Chandan Saha, and Ramprasad Saptharishi. Fast Integer Multiplication Using Modular Arithmetic. In Fortieth Annual ACM Symposium on Theory of Computing, 008. [F 07] [KO6] Martin Fürer. Faster Integer Multiplication. In Thirty Ninth Annual ACM Symposium on Theory of Computing, 007. A. Karatsua and Yu. Ofman. Multiplication of Many-Digital Numers y Automatic Computers. Proceedings of the USSR Academy of Sciences, 145:93 94, 196. 4
[SS71] A. Schönhage and V. Strassen. Schnelle Multiplikation großer Zahlen. Computing, 7:81 9, 1971. [Too63] Andrei Toom. The Complexity of a Scheme of Functional Elements Realizing the Multiplication of Integers. Soviet Mathematics - Doklady, 3:714 716, 1963. 5