CMPT 706 Algorithms for Big Data Testing Primality
CMPT 706 - Algorithms for Big Data Testing Primality January 28, 2020 Testing Primality 1 -1
• Today office hours starting 3: 00. • Quiz 1 - January 30 – first 20 minutes of the class • Practice problems on piazza • Assignment 1 • Due to January 30 • To be submitted to the assignment box in CSIL (in class is also ok) Testing Primality 1 -2
Testing Primality 1 -3
Prime Numbers • An integer n>1 is prime if its only divisors are 1 and n. • Otherwise n>1 is said to be composite. • Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41… Theorem: There are infinitely many prime numbers Proof: Suppose not. Then we can enumerate all primes as p 1, p 2 … pk. Consider the number N = p 1*p 2 *…* pk+1. It is not divisible by any of the pi’s. Therefore, N is another prime. Theorem: The number of primes up to N is denoted by π(N), and it behaves like π(N) = Θ(N/ ln(N)). Proof is out of scope for this course. Testing Primality 1 -4
Primality Testing Question: Given a number how can we check if it is prime? A naïve idea: Input: N What is the runtime of this Naïve algorithm for j = 2…N 0. 5 if j | N output COMPOSITE output PRIME algorithm? Can we do better? Testing Primality 1 -5
Primality Testing A less naïve idea: Fermat’s Little Theorem: If p is a prime and a≠ 0 mod p, then ap-1 = 1 mod p. Proof: In the book. note that if ap-1=1 mod p, then ap=ap-1+1=ap-1*a 1=1*a =a (mod p) Algorithm: on input N For all a < N What is the runtime of this algorithm? N-1 if a ≠ 1 mod N output COMPOSITE Can we do better? output PRIME Testing Primality 1 -6
Primality Testing A Randomized Algorithm: on input N Choose a 1, …ak < N at random (think of k=100) if (ai)N-1 ≠ 1 mod N output COMPOSITE output PRIME Runtime: k iterations, in each iteration the runtime is O(log 3(N)). Therefore, the total runtime is O(k* log 3(N)). log 3(N) comes from the exponentiation algorithm: We compute repeated squares log(N) times In each iteration we pay log 2(N) for multiplying numbers mod N Testing Primality 1 -7
Primality Testing Example: N = 47. Choose a = 5. Compute a 46=546, and check if it is 1 (mod 47) 46 = 32+8+4+2 52=25 54=252=625 = 14 (mod 47) 58=(54)2=142=196 = 8 (mod 47) 516=(58)2=82=64 = 17 (mod 47) 532=(516)2=172=289 = 7 (mod 47) 546= 532 *58 *54 *52= 25*14*8*7 = 1 (mod 47) Testing Primality 1 -8
Primality Testing A Randomized Algorithm: on input N What about correctness? Choose a 1, …ak < N if (ai)N-1 ≠ 1 mod N for some ai output COMPOSITE output PRIME • If N is a prime, the by Little Fermat’s theorem a. N-1 = 1 mod N Therefore, the algorithm always outputs PRIME. • If N is composite… and there exists a such that a. N-1 ≠ 1 mod N, then |{a : a. N-1 ≠ 1 mod N }| >= (N-1)/2. That is, if we draw a random a between 1 and N-1, then with probability >= 1/2 it is a witness that N is composite. Therefore, the algorithm will say COMPOSITE with prob > 1 -1/2 k • But… there are composite N’s such that a. N-1 = 1 mod N for all a. Testing Primality 1 -9
Primality Testing A composite number N is a Carmichael number if a. N-1 = 1 mod N for all a. Examples: 561 = 3 * 11 * 17 is a Carmichael number. 1105 = 5 * 13 * 17 is a Carmichael number. Theorem: There are infinitely many Carmichael numbers. Bad news: That is, there are infinitely many composite numbers on which our primality test always outputs PRIME. Good news: The density of Carmichael numbers is really small Testing Primality 1 -10
Improved Primality Testing Idea: Consider the equation x 2=1. How many solutions does it have? A: x=1, x=-1 Q: Does it have more solutions over the reals? A: No, only +1, -1 Q: What about modulo 15? A: 42=1 (mod 15), 112= 121 = 1 (mod 15) Theorem: Let N be an odd integer such that N is not a prime power. If N is prime, then x 2=1 has only the trivial solutions (i. e. , +1, -1) N is composite, then if x 2=1 has a non-trivial solution. Testing Primality 1 -11
Improved Primality Testing Theorem: Let N be an odd integer that is not a prime power. If N is prime, then x 2=1 (mod N) has only the trivial solutions (i. e. , +1, -1) N is composite, then if x 2=1 (mod N) has a non-trivial solution. Idea: • If N is odd and a. N-1 ≠ 1 (mod N) output COMPOSITE Otherwise, a. N-1 = 1 (mod N). In the case “we check the square root”- a(N-1)/2. • • If a(N-1)/2 ≠ +1, -1 output COMPOSITE. If a(N-1)/2 = -1, output PRIME. // pass the test If a(N-1)/2 = 1 and (N-1)/2 is odd, output PRIME. If a(N-1)/2 = 1 and (N-1)/2 is even, continue to a(N-1)/4 Testing Primality 1 -12
The Miller-Rabin Test A Miller-Rabin test: on input N>2 If N is even output COMPOSITE Write N-1 = q*2 d, where q is odd. Choose a random a < N-1. if a. N-1 ≠ 1 mod N output COMPOSITE otherwise for i=d… 0 if aq*2 i ≠ -1 mod N output PRIME output COMPOSITE • If N is a prime, then the algorithm always outputs PRIME. • If N is composite then outputs COMPOSITE with probability at least 1/4. Therefore, if we repeat the test k times (k=100), then we will be correct with probability > 1 -1/4 k > 0. 999. Testing Primality 1 -13
The Miller-Rabin Test Example: N = 561. Then N-1 = 560 = 35*16 = 35*24. Choose a = 5. If a 560 ≠ 1 (mod 561) output COMPOSITE a 560= 1 (mod 561) --- We continue a 280= 67 (mod 561) We found a non-trivial “square root of 1”. Therefore, 561 is not a prime. Testing Primality 1 -14
The Miller-Rabin Test Example: N = 569. Then N-1 = 568 = 71*23. Choose a = 7. If a 568 ≠ 1 (mod 569) output COMPOSITE a 569= 1 (mod 569) --- We continue • If a 284 ≠ +1, -1 (mod 569) output COMPOSITE a 284= 1 (mod 569) --- We continue • If a 142 ≠ +1, -1 (mod 569) output COMPOSITE a 142= 1 (mod 2569) --- We continue • If a 71 ≠ +1, -1 (mod 569) output COMPOSITE a 71= 1 (mod 2569) --- We continue • 71 is odd, so we can’t continue anymore… Output: 569 looks like PRIME Testing Primality 1 -15
Homework and Reading for next time Exercises from the Book: 1. 33, 1. 34, 1. 36, 1. 37, 1. 38 Reading Chapters 1. 5, 2. 3, 2. 4 Testing Primality 1 -16
- Slides: 16