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 471<br />

266 = (−3/4)392 + 560;<br />

392 = (−1/2)560 + 672;<br />

560 = (−1/2)672 + 896;<br />

672 = (3/4)896 + 0.<br />

Now gcd(525, 266) is seen to be the odd part of 896, namely 7. At each step<br />

we choose the fractional “quotient” so that the 2-power in the remainder<br />

increases. Thus the algorithm below is entirely 2-adic, and is especially suited<br />

for machinery with fast binary operations, such as vector-shift and so on. Note<br />

that the divbin procedure in Algorithm 9.4.7 is merely a single iteration of the<br />

above type, and that one always arranges to apply it when the first integer is<br />

not divisible by as high a power of 2 as the second integer.<br />

Following Stehlé–Zimmermann, we employ a signed modular reduction<br />

x cmod m defined as the unique residue of x modulo m that lies in [−⌊m/2⌋+<br />

1, ⌊m/2⌋]. The function v2, returning the number of trailing zero bits, is as<br />

in Algorithm 9.4.2. As with previous algorithms, B(n) denotes the number of<br />

bits in the binary representation of a nonnegative integer n.<br />

Algorithm 9.4.7 (Stehlé–Zimmermann binary-recursive gcd). For nonnegative<br />

integers x, y this algorithm returns gcd(x, y). The top-level function<br />

SZgcd() calls a recursive, half-binary function hbingcd(), with a classical binary<br />

gcd invoked when operands have sufficiently decreased.<br />

1. [Initialize]<br />

thresh = 10000; // Tunable breakover threshold for binary gcd.<br />

2. [Set up top-level function that returns the gcd]<br />

SZgcd(x, y) { // Variables u, v, k, q, r, G are local.<br />

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

if(v2(v)

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

Saved successfully!

Ooh no, something went wrong!