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.

348 Chapter 7 ELLIPTIC CURVE ARITHMETIC<br />

is empty? And if nonempty, what is the precise intersection A ∩ B? Anaive<br />

method is simply to check A1 against every Bi, then check A2 against every<br />

Bi, and so on. This inefficient procedure gives, of course, an O(N 2 ) complexity.<br />

Much better is the following procedure:<br />

(1) Sort each list A, B, say into nondecreasing order;<br />

(2) Track through the sorted lists, logging any comparisons.<br />

As is well known, the sorting step (1) requires O(N ln N) operations<br />

(comparisons), while the tracking step (2) can be done in only O(N)<br />

operations. Though the concepts are fairly transparent, we think it valuable<br />

to lay out an explicit and general list-intersection algorithm. In the following<br />

exposition the input sets A, B are multisets, that is, repetitions are allowed,<br />

yet the final output A ∩ B is a set devoid of repetitions. We shall<br />

assume a function sort() that returns a sorted version of a list, having<br />

the same elements, but arranged in nondecreasing order; for example,<br />

sort({3, 1, 2, 1}) ={1, 1, 2, 3}.<br />

Algorithm 7.5.1 (Finding the intersection of two lists). Given two finite<br />

lists of numbers A = {a0,...,am−1} and B = {b0,...,bn−1}, this algorithm<br />

returns the intersection set A ∩ B, written in strictly increasing order. Note<br />

that duplicates are properly removed; for example, if A = {3, 2, 4, 2},B =<br />

{1, 0, 8, 3, 3, 2}, thenA ∩ B is returned as {2, 3}.<br />

1. [Initialize]<br />

A = sort(A); // Sort into nondecreasing order.<br />

B = sort(B);<br />

i = j =0;<br />

S = {}; // Intersection set initialized empty.<br />

2. [Tracking stage]<br />

while((i

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

Saved successfully!

Ooh no, something went wrong!