int mulmod(int a, int b, int n){return (a*b)%n;} int expmod(int a, int e, int n) // assume e and n positive { if ( 1 == e ) // base case return a; if ( 0 == e%2 ) // e is even return expmod( mulmod(a, a, n), e/2, n ); // a^(2k) = (a^2)^k mod n else // e is odd return mulmod( a, expmod( mulmod(a, a, n), e/2, n ), n ); // a^(2k+1) = a(a^2)^k mod n }In the computation of expmod(2, e, 89) a certain number of calls to mulmod will be made, depending on e. For e from 1 to 1000, what is the most number of calls to mulmod for any given e? List all of the e's up to 1000 for which this maximum number of mulmod calls occurs. Briefly explain why your list is correct.
Hint: Let rk denote the k-th remainder, where a is r0 and b is r1. Show that rk+2 is not more than half of rk.
You may work with the recursive description of the algorithm in the text, or this iterative version:`
int extendedEuclideanAlg(int a, int b, int& i, int& j) /* a and b are the inputs. return value d and the parameters i and j are set so that d is the greatest common divisor of a and b and d = i*a + j*b. */ { int q; int r = a, s = 1, t = 0, int rr = b, ss = 0, tt = 1; int rrr, sss, ttt; while (rr != 0) { q = r/rr; // floor of exact quotient. rrr = r - q*rr; sss = s - q*ss; ttt = t - q*tt; // update for next step r = rr; rr = rrr; s = ss; ss = sss; t = tt; tt = ttt; } // output i = s; j = t; return r; }
Hint: in Maple ab mod c and a-1 mod c can be computed by expressions:
a &^ b mod c; a &^ (-1) mod c;
Hint: Let X = n*lg(n) and Y=n*lg(n)*lg(lg(n)). For starters which of these is the most accurate estimate: X + Y, X*Y, XY?
Also notice that no coefficient in the product polynomial is larger than n*(2n)2. Why is this fact useful/important?