10.12.2012 Views

Prime Numbers

Prime Numbers

Prime Numbers

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

9.4 Enhancements for gcd and inverse 465<br />

}<br />

2. [Halve t3]<br />

goto [Check even];<br />

if(t1,t2 both even) (t1,t2,t3) =(t1,t2,t3)/2;<br />

else (t1,t2,t3) =(t1 + y, t2 − x, t3)/2;<br />

3. [Check even]<br />

if(t3 even) goto [Halve t3];<br />

4. [Reset max]<br />

if(t3 > 0) (a, b, h) =(t1,t2,t3);<br />

else (v1,v2,v3) =(y − t1, −x − t2, −t3);<br />

5. [Subtract]<br />

(t1,t2,t3) =(a, b, h) − (v1,v2,v3);<br />

if(t1 < 0) (t1,t2) =(t1 + y, t2 − x)<br />

if(t3 = 0) goto [Halve t3];<br />

return (a, b, 2 β h);<br />

Like the basic binary gcd algorithm, this one tends to be efficient in actual<br />

machine implementations. When something is known as to the character of<br />

either operand (for example, say y is prime) this and related algorithms can<br />

be enhanced (see Exercises).<br />

9.4.2 Special inversion algorithms<br />

Variants on the inverse-finding, extended gcd algorithms have appeared over<br />

the years, in some cases depending on the character of the operands x, y. One<br />

example is the inversion scheme in [Thomas et al. 1986] for x −1 mod p, for<br />

primes p. Actually, the algorithm works for unrestricted moduli (returning<br />

either a proper inverse or zero if the inverse does not exist), but the authors<br />

were concentrating on moduli p for which a key quantity ⌊p/z⌋ within the<br />

algorithm can be easily computed.<br />

Algorithm 9.4.4 (Modular inversion). For modulus p (not necessarily<br />

prime) and x ≡ 0(modp), this algorithm returns x −1 mod p.<br />

1. [Initialize]<br />

z = x mod p;<br />

a =1;<br />

2. [Loop]<br />

while(z = 1) {<br />

q = −⌊p/z⌋; // Algorithm is best when this is fast.<br />

z = p + qz;<br />

a =(qa) modp;<br />

}<br />

return a; // a = x −1 mod p.<br />

This algorithm is conveniently simple to implement, and furthermore (for<br />

some ranges of primes), is claimed to be somewhat faster than the extended

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!