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.

464 Chapter 9 FAST ALGORITHMS FOR LARGE-INTEGER ARITHMETIC<br />

can be computed with a shift into oblivion of the low-order zeros; note also for<br />

theoretical convenience we may as well take v2(0) = ∞.)<br />

1. [2’s power in gcd]<br />

β =min{v2(x),v2(y)}; // 2 β gcd(x, y)<br />

x = x/2 v2(x) ;<br />

y = y/2 v2(y) ;<br />

2. [Binary gcd]<br />

while(x = y) {<br />

(x, y) =(min{x, y}, |y − x|/2 v2(|y−x|) );<br />

}<br />

return 2 β x;<br />

In actual practice on most machinery, the binary algorithm is often faster<br />

than the Euclid algorithm; and as we have said, Lehmer’s enhancements may<br />

also be applied to this binary scheme.<br />

But there are other, more modern, enhancements; in fact, gcd enhancements<br />

seem to keep appearing in the literature. There is a “k-ary” method<br />

due to Sorenson, in which reductions involving k>2 as a modulus are performed.<br />

There is also a newer extension of the Sorenson method that is claimed<br />

to be, on a typical modern machine that possesses hardware multiply, more<br />

than 5 times faster than the binary gcd we just displayed [Weber 1995]. The<br />

Weber method is rather intricate, involving several special functions for nonstandard<br />

modular reduction, yet the method should be considered seriously<br />

in any project for which the gcd happens to be a bottleneck. Most recently,<br />

[Weber et al. 2005] introduced a new modular GCD algorithm that could be<br />

an ideal choice for certain ranges of operands.<br />

It is of interest that the Sorenson method has variants for which the<br />

complexity of the gcd is O(n 2 / ln n) as opposed to the Euclidean O(n 2 )<br />

[Sorenson 1994]. In addition, the Sorenson method has an extended form for<br />

obtaining not just gcd but inverse as well.<br />

One wonders whether this efficient binary technique can be extended in the<br />

way that the classical Euclid algorithm can. Indeed, there is also an extended<br />

binary gcd that provides inverses. [Knuth 1981] attributes the method to<br />

M. Penk:<br />

Algorithm 9.4.3 (Binary gcd, extended for inverses). For positive integers<br />

x, y, this algorithm returns an integer triple (a, b, g) such that ax + by = g =<br />

gcd(x, y). We assume the binary representations of x, y, and use the exponent β<br />

as in Algorithm 9.4.2.<br />

1. [Initialize]<br />

x = x/2 β ; y = y/2 β ;<br />

(a, b, h) =(1, 0,x);<br />

(v1,v2,v3) =(y, 1 − x, y);<br />

if(x even) (t1,t2,t3) =(1, 0,x);<br />

else {<br />

(t1,t2,t3) =(0, −1, −y);

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

Saved successfully!

Ooh no, something went wrong!