1 / 156

Chapter 00

Chapter 00. Introducing Foundations. Elementary Number-Theoretic Notions An important application of number-theoretic algorithms is in cryptography

billc
Download Presentation

Chapter 00

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 00 Introducing Foundations

  2. Elementary Number-Theoretic Notions • An important application of number-theoretic algorithms is in cryptography • - the discipline concerned with encrypting a message sent from one party to another, such that someone who intercepts the message will not be able to decode it. • Let the set Z = { …., -2, -1, 0, 1, 2, 3, ….} of integers. • Let the set N = {0, 1, 2, 3, ….} of natural numbers. • The notation d | a (read “d divides a”) means that a = k*d for some integer k, (i.e., a is k multiple of d).

  3. Prime and Composite Numbers • Definition: • An integer a > 1 is prime if, and only if for all integers m and n, if a = mn, then either m = 1 and n = a or m = a and n = 1. • Or, equivalently, an integer a > 1, whose only divisors are the trivial divisors 1 and a, is said to be a prime number (or, more simply, a prime). • Note: • A prime number has no factors. • The first 20 primes, in order, are • 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71. • There are infinitely many primes (prove!).

  4. Prime and Composite Numbers • Definition: • An integer a > 1 is composite if, and only if there exists integers m and n such that a = m*n where 1 < m, n < a. • An integer a > 1 that is not prime is said to be a composite number (or more simply, a composite). • For example: • 39 is a composite because 3 | 39 and 13 | 39. • That is, it is because 39 has the divisors 3 and 13. • A composite number has at least one factor. • Exceptions: • The integer 1 is said to be a unit and is neither prime nor composite. • The integer 0 and all negative integers are neither prime nor composite.

  5. Common Divisors and Greatest Common Divisor (GCD) Definition: An integer n is the common divisor of x and y if, and only if n is a divisor of x (denoted n | x) and n is also divisor of y (denoted n | y). Example: The common divisors of 24 and 30 are 1, 2, 3 and 6. x but not vice versa

  6. Common Divisors and Greatest Common Divisor (GCD) An important property of common divisors is that n | x and n | y implies that n | (x + y) and n | (x – y). (a0.2) x but not vice versa

  7. The following statement will be used in Theorem 0.4. If x | y, then either |x| ≤ |y| or y = 0, and x | y and y | x implies that x = y. (a0.4)

  8. Definition: Let x and y are integers that are not both zero. Then the greatest common divisor of x and y, denoted gcd(x, y) is the largest integer d that divides both of them. i.e., d | x and d | y.

  9. We define gcd(0, 0) = 0. This definition is necessary to make standard properties of the gcd function (such as gcd(x, 0) = |x| ) universally valid. The following are elementary properties of the gcd function: gcd(a, b) = gcd(b, a), gcd(a, b) = gcd(-a, b), gcd(a, b) = gcd(|a|, |b|), gcd(a, 0) = |a|, and gcd(a, ka) = |a|, for k ɛ Z = { …., -2, -1, 0, 1, 2, …} of integers.

  10. Lemma 0.1.1 If x is a positive integer, then gcd(x, 0) = x. Proof: Suppose x is a positive integer. Certainly x is a common divisor of both x and 0 because x divides itself (i.e., x | x) and also x divides 0 (i.e., x | 0). Also no integer greater than x can be a common divisor of x and 0, (since no integer greater than x can divide x). Hence x is the greatest common divisor of x and 0. QED

  11. Lemma 0.1.2 If x and y are any integers not both zero, and if q and r are any integers such that x = y * q + r, then GCD(x, y) = GCD(y, r). Proof: [The proof is divided into two parts: (1) proof that gcd(a, b) gcd(b, r), and (2) proof that gcd(b, r) gcd(a, b). Since each gcd is less than or equal to the other, the two must be equal.] Note that r = x mod y.

  12. Example 0.30: According to the previous Lemma 0.1.2, gcd(64, 24) = gcd(24, 16) for 64 = 2 * 24 + 16 = gcd(16, 8) for 24 = 1*16 + 8 = gcd(8, 0) for 16 = 2 * 8 + 0 = 8 for 8 = 0 * 0 + 8. Note that gcd(24, 64) = gcd(64, 24)

  13. Given two integers a and b with a > b 0, The Euclidean Algorithm computes GCD(a, b) based on two facts: GCD(a, b) = GCD(b, a mod b), where a = b * q + r , 0 r < b, and r = a mod b. 2. GCD(a, 0) = a.

  14. Algorithm Euclid (m, n) //Compute gcd(m, n) by Euclidean algorithm Input: two non-negative m and n, not both zero integers Output: the greatest common divisor of m and n while n ≠ 0 do { if (n = = 0) r ← m mod n; then return m m ← n; else Euclid(n, m mod n) n ← r;} return m.

  15. Let us rewrite the iterative Euclid algorithm as follows: • Input: A, B [integers with A > B 0] • Algorithm: • a := A; b := B; r := B; • while (b 0) { • r := a mod b; • a := b; • b := r; • } //end while; • gcd := a; • Output: gcd. a := A; b := B; r := B; b 0 r := a mod b; a := b; b := r; gcd := a;

  16. Recall: • Several characteristics of Algorithms: • … • [Different ways for specifying an algorithm] The same algorithm can be represented in several different ways. • Euclidean algorithm can be defined recursively or non-recursively. • Another example is nth Fibonacci Term which can be computed recursive and iterative.

  17. Example 0.32: Find the greatest common divisor of two integers, 7,276,500 and 3,185,325. gcd(7,276,500, 3,185,325) = gcd(3,185,325, 905,850) = gcd(905,850, 467,775) = gcd(467,775, 438,075) = gcd(438,075, 29,700) = gcd(29,700, 22,275) = gcd(22,275, 7,425) = gcd(7,425, 0) = 7,425 It requires 7 recursive calls to get the solution 7,425. Let n = 3,185,325 log n = 21 For the worse case, the number of recursive calls is about 21 times.

  18. Input: A, B [integers with A > B 0] • Algorithm: • m := A; • n := B; • r := B; • while (n 0) { • r := m mod n; • m := n; • n := r; • } //end while; • gcd := m; • Output: gcd. • Definition: An algorithm is said to be correct if, for every input instance, it halts with the correct output. • The algorithm will eventuallyhalt: • The new value of n on the next iteration is m mod n, which is always smaller than n (since 0 ≤ r < n, for r = m mod n). The value of the second number of the pair (m, n), (which is gcd(m ← n, n ← r), r = m mod n) eventually becomes 0 and it cannot become negative. The algorithm stops.

  19. Algorithm Euclid (m, n) //Compute gcd(m, n) by Euclid’s algorithm Input: two non-negative m and n, not both zero integers Output: the greatest common divisor of m and n Pre-condition: {m > n 0} while n ≠ 0 do { if (n = = 0) r ← m mod n; then return m m ← n; else Euclid(n, m mod n) n ← r;} return m.

  20. Correctness if the Euclidean Theorem • Input: A, B [integers with A > B 0] • Algorithm: • [pre-cond: A and B are integers with A > B 0, • a = A, b = B, r = B] • a := A; b = B; r = B; • [I(n): gcd(a, b) = gcd(A, B), 0 b < a] • while (b 0) { • r := a mod b; • a := b; • b := r; • } //end while; • [post-cond: a = gcd(A, B)] • gcd = a; • Output: gcd. a := A; b := B; r := B; b 0 r := a mod b; a := b; b := r; gcd := a;

  21. {I and G} S {I} {I} while G do S end {Q} Correctness if the Euclidean Theorem Proof: To prove the correction of the loop, let the invariant be I(n): gcd(a, b) = gcd(A, B), 0 b < a. The guard of the while loop is G: b 0. Basic Property: [I(0) is true before the first iteration of the loop] Need to show: I(0) is gcd(A, B) = gcd(a, b) and 0 b < a. According to the pre-condition, a = A, b = B, r = B, and 0 B < A. Hence gcd(A, B) = gcd(a, b). Since 0 B < A, b = B and a = A, then 0 b < a. Hence I(0) is true. Inductive Property: [if G ˄ I(k) is true before an iteration of the loop (where k 0), then I(k+1) is true after iteration of the loop]. … a = A, b = B, r = B, and 0 B < A. a := A; b := B; r := B; I(0): gcd(A, B) = gcd(a, b), 0 b < a. {P} {I}

  22. To prove the correction of the loop, let the invariant be I(n): gcd(a, b) = gcd(A, B), 0 b < a. The guard of the while loop is G: b 0. Inductive Property: [if G ˄ I(k) is true before an iteration of the loop (where k 0), then I(k+1) is true after iteration of the loop]. Suppose k is a nonnegative integer such that G ˄ I(k) is true before an iteration of the loop. [We must show that I(k+1) is true after iteration of the loop.] Since G is true, bold 0 and the loop is entered. And since I(k) is true, immediately before statement “r := a mod b;” is executed. gcd(aold, bold) = gcd(A, B), 0 bold < aold. ………………… (E.01) After execution of statement “r := a mod b;” , rnew := aold mod bold . Thus, by the quotient-remainder theorem, aold = bold * q + rnew for some integer q {I and G} S {I} • [I(n): gcd(a, b) = gcd(A, B), 0 b < a] • while (b 0) { • r := a mod b; • a := b; • b := r; • } //end while;

  23. and rnewhas the property that • 0 rnew < bold. …………………..(E.02) • By Lemma 0.1.2, • gcd(aold, bold) = gcd(bold, rnew). • So by the equation of (E.01) gcd(aold, bold) = gcd(A, B), 0 bold < aold. , • gcd(bold, rnew) = gcd(A, B), ..………………….(E.03) • When statements “a := b; b := r;” are executed, • anew = bold and bnew = rnew . ..………………….(E.04) • Substituting equations (E.04) into equation (E.03) and inequality (E.02) yields • gcd(anew, bnew ) = gcd(A, B), ..………………….(E.05) • and 0 bnew < anew ..…………………..(E.06) • respectively. Hence, after the iteration of the loop, by equations (E.05) and (E.06), • gcd(a, b) = gcd(A, B), 0 b < a, • which is I(k+1). [This is what we need to show.] • [I(n): gcd(a, b) = gcd(A, B), 0 b < a] • while (b 0) { • r := a mod b; • a := b; • b := r; • } //end while;

  24. III. Eventual Falsity of the Guard: [after a finite number of iterations of the loop, G becomes false.] Each value of obtained by repeated iteration of the loop is nonnegative and less than the previous value of b. Thus, by the well-ordering principle, there is a least value bmin. The fact is that bmin = 0. [If bmin is not 0, then the guard is true, and so the loop is iterated another time. In this iteration a value of r is calculated that is less than the previous value of b, bmin. Then the value of b is changed to r, which is less than bmin. This contradicts the fact that bminis the least value of b obtained by repeated iteration of the loop. Hence bmin= 0.] Since bmin= 0, the guard is false immediately following the loop iteration in which bminis calculated. The loop terminates

  25. IV. Correctness of the Post-Condition. [If N is the least number of iterations after which G is false and I(N) : gcd(a, b) = gcd(A, B), 0 b < a] is true, then the values of the algorithm variables will be as specified in the post-condition.] Suppose that for some nonnegative integer N, G is false and I(N) is true. [We must show the truth of the post-condition: a = gcd(A, B).] Since G is false, b = 0, and since I(N) is true, gcd(a, b) = gcd(A, B). …………(E.07) Substituting b = 0 into equation (E.07) give gcd(a, 0) = gcd(A, B). But by Lemma 0.1.1, gcd(a, 0) = a. Hence, a = gcd(A, B) [as was to be shown.] {I and G} {Q}

  26. Algorithm Euclid(m, n) { if (n = = 0) then return m else Euclid(n, m mod n) } Rationalize: Euclid(n, m mod n), wherem mod n < m/2. Let say, m mod n = m/2. Euclid(n, m mod n) reduces m by one bit through right shift. Euclid(m mod n, n mod (m mod n) ) where n mod (m mod n) < n/2. This reduces n by one bit through right shift. This means,after two consecutive rounds, both arguments, m and n, are at the very least halved in value. This means: Assume that both m and n are of n-bit integers. Then the base case will be reached with 2n recursive calls. i.e., gcd(a, b) = gcd(b, a mod b) = gcd(a mod b, b mod (a mod b)). Then 2n recursive calls are needed. Each requires O(n2 ). That is, the total running time is 2n * O(n2 ) = O(n3 ) .

  27. The overall running time of Euclid is proportional to the number of recursive calls it makes. • Our analysis makes use of the Fibonacci numbers Fk, defined by the following recurrence: • Fi = Fi-1 + Fi-2 for i ≥ 2, • F0 = 0, F1 = 1. • Note that, in the following sequel, • F(k) = F(k-1) + F(k-2), k ≥ 2; F(0) = 0 and F(1) = 1 • is equivalent to write • Fk = Fk-1+ Fk-2 k ≥ 2 and F0 = 0 and F1 = 1.

  28. Lemma 0.1: If m > n ≥ 1 and the invocation Euclid(m, n) performs k ≥ 1 recursive calls, then m ≥ Fk+2 and n ≥ Fk+1, where Fkis the kth number in the Fibonacci sequence. Algorithm Euclid(m, n) { if (n = = 0) then return m else Euclid(n, m mod n) } Example: Fibonacci sequence is: 0 1 1 2 3 5 8 13 21 34 55 89 … where k is: 0 1 2 3 4 5 6 7 8 9 10 11 … Euclid(13, 89) = Euclid(89, 13%89) = Euclid(13, 89%13) = Euclid(11, 13%11) = Euclid(2, 11%2) = Euclid(1, 0) = 1. It requires 5 recursive calls. n = 13 = ≥ F6+1 where k = 6

  29. Proof: The proof is by induction. Induction base: Suppose the call gcd(m, n) results in 1 recursive call. That means, n ≠ 0 and k = 1. Let k = 1. Since n ≥ 1 n ≥ F1+1 = F2 = 1, because F2 = F1 + F0 = 1 + 0 = 1. Since m > n, we have m ≥ 2, which means m ≥ F1+2 = F3 = 2, because F3 = F2 + F1 = 1 + 1 = 2. Since n > (m mod n), in each recursive call the first argument is strictly larger than the second; the assumption that m > n therefore holds for each recursive call.

  30. Induction hypothesis: Assume the lemma is true if k – 1 recursive calls are made. Induction step: We need to show the lemma is true if k ≥ 2 recursive calls are made. That means we need to show m ≥ Fk+2 and n ≥ Fk+1. The first recursive call is gcd(n, m mod n). Since there are k recursive calls in all, this call must require k – 1 recursive calls. Since k ≥ 2, there is at least one more recursive call, which means m mod n ≥ 1. Therefore, since n > m mod n, the conditions of the induction hypothesis are satisfied, which means n ≥ Fk+1 and m mod n ≥ Fk. (e0.1)

  31. So we have arrived at least one of the inequalities we need to show. Towards showing the other, we have m = q*n + m mod n, (e0.2) where q is the quotient of dividing m by n. since m > n, q ≥ 1. Inequality (e0.1) and Equality (e0.2) therefore imply m ≥ n + m mod n ≥ Fk+1+ Fk = Fk+2 . This completes the proof. QED

  32. Note that the induction step also can be written as follows: Assume that the lemma is true if k – 1 recursive calls are made. We shall then prove that it is true for k recursive calls. Since k > 0, we have n > 0, and Euclid(m, n) calls Euclid(n, m mod n) recursively, which in turn makes k -1 recursive calls. The inductive hypothesis then implies that n ≥ Fk+1 (thus proving part of the lemma), and (m mod n) ≥Fk. We have n + (m mod n) = n + (m – └ m / n ┘ * n) ≤ m. Since m > n > 0 implies └ m / n ┘ ≥ 1. Thus, m ≥ n + (m mod n) ≥ Fk+1+ Fk = Fk+2

  33. The following theorem is an immediate corollary of Lemma 0.1. Theorem 0.5 (Lame’s Theorem): For any integer k ≥ 1, if m > n ≥ 1 and n < Fk+1 then the call Euclid(m, n) makes fewer than k recursive calls. Example: Fibonacci sequence is: 0 1 1 2 3 5 8 13 21 34 55 89 … where k is: 0 1 2 3 4 5 6 7 8 9 10 11 … Let n = 13. 13 < F7+1 where k = 7 E(21, 13) = E(13, 21mod13 = 8) = E(8, 13mod8 = 5) = E(5, 8mod5 = 3) = E(3, 5mod3 = 2) = E(2, 3mod2 = 1) = E(1, 2mod1 = 0) = 0. It needs 6 recursive calls, fewer than k = 7 where 13 < F7+1 = 21.

  34. Theorem 0.5 (Lame’s Theorem): For any integer k ≥ 1, if m > n ≥ 1 and n < Fk+1 then the call Euclid(m, n) makes fewer than k recursive calls. Proof: when k = 1, we do not have F2 > F1 for F(2) = F(1) = 1. Therefore we begin k = 2. For the base, k = 2, the call Euclid(F3, F2) = Euclid(2, 1) = Euclid(1, 2 mod 1) makes exactly one recursive call to Euclid(1, 0). By induction, we can show, for k > 2, we have Fk > Fk-1 > 0 and Fk+1 = Fk + Fk-1 and therefore we have Fk+1 mod Fk = Fk-1 . Thus we have Euclid(Fk+1, Fk) = Euclid(Fk, Fk+1 mod Fk) = Euclid(Fk, Fk-1) Therefore the call Euclid(Fk+1, Fk) recurses one time more than the call Euclid(Fk, Fk-1), or exactly k – 1 times, assuming that Euclid(Fk, Fk-1) makes exactly k - 2 recursive calls. QED

  35. Complexity of Euclid’s algorithm: • By Lame’s theorem you find a first Fibonacci number larger than n. If it is Fk+1 there are fewer than k recursive calls. Thus the number of the recursive calls in Euclid is O(log n), assuming n = 2k. • If m > n 0, we can show Euclid(m, n) makes at most 1 + logn recursive calls. Since Fk, where is the golden ratio , the number of recursive calls in Euclid is O(log n). • Thus, if we call Euclid on two -bit numbers, then it performs O() arithmetic operations and O(3) bit operations (assuming that multiplication and division of -bit numbers take O(2) bit operations). • Anyway the worst-case number of recursive calls for input size m, n • isO((log m)(log n)) bit operations.

  36. Consecutive integer checking algorithm for computing gcd(m,n) Principle: Let t = min{m, n} to be the possible common divisor for the given numbers m and n. Test whether t | m. If not reduce t by one, then test the new t = t – 1 | m. Repeat this process until the new t | m. (The new t = t – 1). Then we test whether the new t | n. If yes, then t is the common divisor of m and n. Otherwise, repeat the same process for the smaller t (at least one less than the current t) as the possible common divisor for the given number m and n.

  37. Consecutive integer checking algorithm for computing gcd(m,n) t = min{5, 0}; r = 5 mod 0 implies that r = 5 – 0 – 0 - … - 0 operates infinitely. Step 1: t ← min{m, n}. Step 2: remainder = m mod t. If the remainder is 0, go to Step 3; Otherwise, go to Step 4. Step 3: remainder = n mod t. If the remainder of n mod t is 0, return t as the answer and stop; otherwise, proceed to Step 4. Step 4: Decrease the value of t by 1. Go to Step 2.

  38. This algorithm does not work correctly when one of the inputs numbers is zero (why? On Step 2: r = (3, 0) = 3, then go to Step 4. t = 0 – 1 = -1. This fails to work!). • This example illustrates why it is so important to specify the range of an algorithm’s input explicitly and carefully. • Definition: An algorithm is said to be correct if, for every input instance, it halts with the correct output.

  39. Several characteristics of Algorithms: • … • The range of inputs for which an algorithm works has to be specified carefully. [Well-specified inputs’ range] Consecutive integer checking algorithm for computing gcd(m, n) does not work correctly when one of the input numbers is zero.

  40. Example 0.33: Find gcd(11, 3). • Let m = 11 and n = 3. • Step 1: t = min{ 11, 3} = 3. • Step 2: r = 11 mod 3 = 2 ≠ 0. • Go to Step 4 • Step 4: t = t -1; that is, t = 3 – 1 = 2. • Go to Step 2. • Step 2: r = 11 mod 2 = 1 ≠ 0. • Go to Step 4. • Step 4: t = t -1; that is, t = 2 – 1 = 1. • Go to Step 2. • Step 2: r = 11 mod 1 = 0. • Then go to step 3 • Step 3: r = n mod t; that is r = 3 mod 1 = 0; • then return t = 1 as the answer and stop.

  41. Example 0.35: Another example for finding gcd(16, 12) • Let m = 16 and n = 12 • Step 1: t = min{16, 12}. • Step 2: r = 16 mod 12 = 4 ≠ 0. Go to Step 4. • Step 4: t = t - 1; that is, t = 12 – 1 = 11. • Go to Step 2. • Step 2: r = 16 mod 11 = 5 ≠ 0. Go to Step 4 • Step 4: t = t - 1; that is, t = 11 – 1 = 10. • Go to Step 2. • Step 2: r = 16 mod 10 = 6 ≠ 0. Go to Step 4. • Step 4: t = t - 1; that is, t = 10 – 1 = 9. • Go to Step 2. • Step 2: r = 16 mod 9 = 7 ≠ 0. Go to Step 4 • Step 4: t = t - 1; that is, t = 9 - 1 = 8. • Go to Step 2.

  42. Step 2: r = 16 mod 8 = 0. Then go to step 3 Step 3: r = n mod t; that is r = 12 mod 8 = 4 ≠ 0. Then, Go to Step 4 {So far, what we find 16 is divisible by 8, but 12 is not divisible by 8. Therefore 8 is not a common divisor. It must be a number which is smaller than 8. So what is the smaller number such that 16 is divisible by the smaller number.} Step 4: t = t - 1; that is, t = 8 - 1 = 7. Go to Step 2.

  43. {Second iteration} Step 2: r = 16 mod 7 = 2 ≠ 0. Go to Step 4. Step 4: t = t - 1; that is, t = 7 – 1 = 6. Go to Step 2. Step 2: r = 16 mod 6 = 4 ≠ 0. Go to Step 4 Step 4: t = t - 1; that is, t = 6 – 1 = 5. Go to Step 2. Step 2: r = 16 mod 5 = 1 ≠ 0. Go to Step 4. Step 4: t = t - 1; that is, t = 5 – 1 = 4. Go to Step 2. Step 2: r = 16 mod 4 = 0. Go to Step 3 Step 3: r = n mod t; that is r = 12 mod 4 = 0; then return t = 4 as the answer and stop.

  44. Middle-school procedure for computing gcd(m, n) Step 1 Find the prime factors of m (Use Sieve of Eratosthenes). Step 2 Find the prime factors of n (Use Sieve of Eratosthenes). Step 3 Identify all the common factors in the two prime expansion found in Step 1 and Step 2. (If p is a common factor occurring pm and pn times in m and n, respectively, it(the gcd) should be repeated min{pm, pn} times.) Step 4 Compute the product of all the common factors and return it as the greatest common divisor of the numbers given.

  45. Example 0.36:For the numbers 60, and 24, we get • m = 60 = 2.2.3.5 • n = 24 = 2.2.2.3 • GCD(60, 24) = 2.2.3 = 12. • This procedure is much more complex and slower than Euclid’s algorithm (inferior efficiency). • The middle-school procedure does not qualify as a legitimate algorithm, because the prime factorization steps are not defined unambiguously. p = 2 pm = 2; pn = 3; min{pm, pn} = min{2, 3} = 2; p = 3 pm = 1; pn = 1; min{pm, pn} = min{1, 1} = 1; p = 5 pm = 1; pn = 0; min{pm, pn} = min{1, 0} = 0;

  46. Several characteristics of Algorithms: • The non-ambiguity requirement for each step of an algorithm cannot be compromised. [non-ambiguity] Prime Factorization in Middle School Procedure for computing gcd(m, n) is defined ambiguously • …

  47. Sieve of Eratosthenes (Ancient Greece, 200B.C.) Problem: A simple algorithm for generating consecutive primes not exceeding any given integer n. Step 1: Initialize a list of prime candidates with consecutive integers from 2 to n. Step 2: Continue the step for eliminating from the list all multiples of 2, then 3, then 5, until no more numbers can be eliminated from the list. The remaining integers of the list are the primes needed.

  48. Example 0.37:Let n = 25. [p = 2, 3, 5] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 3 5 7 9 11 13 15 17 19 21 23 25 2 3 5 7 11 13 17 19 23 25 2 3 5 7 11 13 17 19 23 How far do you have to go for eliminating the non-prime numbers, for a given n? What is the largest number p whose multiples can still remain on the list? For example, for this case p = 5 .

  49. If p is a number whose multiples are being eliminated on the current pass, then the first multiple we should consider is p.p, because all its smaller multiples 2p, 3p, …, (p-1)p have been eliminated on earlier passes through the list. For example, if p = 5, we only consider 5*5, 6*5, 7*5, … because 2*5, 3*5, and 4*5 have been eliminated when we consider 2, 3, and 4 respectively. This observation helps to avoid eliminating the same number more than once.

More Related