10.12.2012 Views

Prime Numbers

Prime Numbers

Prime Numbers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

}<br />

q =0;<br />

while(v2(r) ≤ v2(y)) {<br />

q = q − 2 v2(r)−v2(x) ;<br />

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

}<br />

q = q cmod 2 v2(y)−v2(x)+1 ;<br />

r = x + qy/2 v2(y)−v2(x) ;<br />

return (q, r);<br />

5. [Half-binary gcd function (recursive)]<br />

hbingcd(k, x, y) {<br />

// Matrix returned; G, u, v, k1,k2,k3,q,r are local.<br />

1 0<br />

G = ;<br />

0 1<br />

if(v2(y) >k) return G;<br />

k1 = ⌊k/2⌋;<br />

k3 =22k1+1 ;<br />

u = x mod k3;<br />

v = y mod k3;<br />

G = hbingcd(k1,u,v); // Recurse.<br />

(u, v) T = G(x, y) T ;<br />

k2 = k − v2(v);<br />

if(k2 < 0) return G;<br />

(q, r) =divbin(u, v);<br />

k3 =2v2(v)−v2(u) ;<br />

0 k3<br />

G =<br />

G;<br />

k3 q<br />

k3 =22k2+1 ;<br />

u = v2−v2(v) mod k3;<br />

v = r2−v2(v) mod k3;<br />

G = hbingcd(k2,u,v)G;<br />

return G;<br />

}<br />

See the last part of Exercise 9.20 for some implicit advice on what operations<br />

in Algorithm 9.4.7 are relevant to good performance. In addition, note that<br />

to achieve the remarkably low complexity of either of these recursive gcds,<br />

the implementor should make sure to have an efficient large-integer multiply.<br />

Whether the multiply occurs in a matrix multiplication, or anywhere else, the<br />

use of breakover techniques should be in force. That is, for small operands<br />

one uses grammar-school multiply, then for larger operands one may employ<br />

a Karatsuba or Toom–Cook approach, but use one of the optimal, FFT-based<br />

options for very large operands. In other words, the multiplication complexity<br />

M(N) appearing in the complexity formula atop the present section needs<br />

be taken seriously upon implementation. These various fast multiplication<br />

algorithms are discussed later in the chapter (Sections 9.5.1 and 9.5.2).

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

Saved successfully!

Ooh no, something went wrong!