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.7 Exercises 519<br />

(3) Write out complete algorithms for addition and subtraction of integers in<br />

base B, with signs arbitrary.<br />

9.3. Assume that each of two nonnegative integers x, y is given in balanced<br />

base-B representation. Give an explicit algorithm for calculating the sum<br />

x + y, digit by digit, but always staying entirely within the balanced base-B<br />

representation for the sum. Then write out a such a self-consistent multiply<br />

algorithm for balanced representations.<br />

9.4. It is known to children that multiplication can be effected via addition<br />

alone, as in 3 · 5 = 5 + 5 + 5. This simple notion can actually have<br />

practical import in some scenarios (actually, for some machines, especially<br />

older machines where word multiply is especially costly), as seen in the<br />

following tasks, where we study how to use storage tricks to reduce the amount<br />

of calculation during a large-integer multiply. Consider the multiplication of<br />

D-digit, base-(B =2 b ) integers of size 2 n ,sothatn ≈ bD. For the tasks below,<br />

define a “word” operation (word multiply or word add) as one involving two<br />

size-B operands (each having b bits).<br />

(1) Argue first that standard grammar-school multiply, whereby one constructs<br />

via word multiplies a parallelogram and then adds up the columns<br />

via word adds, requires O(D 2 ) word multiplies and O(D 2 ) word adds.<br />

(2) Noting that there can be at most B possible rows of the parallelogram,<br />

argue that all possible rows can be precomputed in such a way that the<br />

full multiply requires O(BD) word multiplies and O(D 2 ) word adds.<br />

(3) Now argue that the precomputation of all possible rows of the parallelogram<br />

can be done with successive additions and no multiplies of any kind,<br />

so that the overall multiply can be done in O(D 2 + BD) word adds.<br />

(4) Argue that the grammar-school paradigm of task (1) above can be done<br />

with O(n) bits of temporary memory. What, then, are the respective<br />

memory requirements for tasks (2), (3)?<br />

If one desires to create an example program, here is a possible task: Express<br />

large integers in base B = 256 = 2 8 and implement via machine task (2)<br />

above, using a 256-integer precomputed lookup table of possible rows to create<br />

the usual parallelogram. Such a scheme may well be slower than other largeinteger<br />

methods, but as we have intimated, a machine with especially slow<br />

word multiply can benefit from these ideas.<br />

9.5. Write out an explicit algorithm (or an actual program) that uses the<br />

wn relation (9.3) to effect multiple-precision squaring in about half a multipleprecision<br />

multiply time. Note that you do not need to subtract out the term<br />

δn explicitly, if you elect instead to modify slightly the i sum. The basic point<br />

is that the grammar-school rhombus is effectively cut (about) in half. This<br />

exercise is not as trivial as it may sound—there are precision considerations<br />

attendant on the possibility of huge column sums.

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

Saved successfully!

Ooh no, something went wrong!