Download presentation

Presentation is loading. Please wait.

Published byDylan Kidd Modified over 2 years ago

1
A Solution to POJ1811 Prime Test STYC

2
Problem Description Given an integer N which satisfies the relation 2 < N < 2 54, determine whether or not it is a prime number. If N is not a prime number, find out its smallest prime factor.

3
Key Concepts Prime numbers: A prime number is a positive integer p > 1 that has no positive integer divisors other than 1 and p itself.

4
Framework of the Solution Determine whether the given N is prime or not. If N is prime, print Prime and exit. Factorize N for its smallest prime factor.

5
The Brute-force Way Trial Division If N is even, then 2 is its smallest prime factor. Try dividing N by every odd number k between 2 and N 1/2. The smallest k by which N is divisible is the smallest prime factor of N. If such k does not exist, then N is prime. Complexity: O(N 1/2 ) for time, O(1) for space

6
Modified Brute-force Construct a table that stores all prime numbers not greater than N 1/2. Try dividing N only by prime numbers. Complexity: O(N 1/2 logN) for time, O(N 1/2 ) for space using Sieve of Eratosthenes Estimation of space consumption: 2 26 bits = 2 23 bytes = 8,192 kilobytes Much time is used in the process of sieving

7
Modified Brute-force 2 Embed a table of prime numbers smaller than N max 1/4 into the source. Extend the table to N 1/2 by runtime calculation. Complexity: O(N 3/4 /logN) for time, O(N 1/2 /logN) for space Estimation of time consumption: Finding all 7,603,553 primes smaller than 2 27 takes approx x divisions or 73 minutes on a Pentium 1.5 GHz.

8
Brute-force with Trick Start from N 1/2 rather than 2. Do factorization recursively once a factor is found. Efficient in handling N = pq where p and q relatively close to each other. POJ accepts this! - westevers solution

9
Brute-force with Trick 2 Wheel Factorization Test whether N is a multiple of 2, 3 or 5. If it is, the problem has been solved. If not, do trial division using only integers which are not multiples of 2, 3, and 5. Saves 7/15 of work.

10
Key Concepts (cont.) Prime factorization algorithms: Algorithms devised for determining the prime factors of a given number Primality tests: Tests to determine whether or not a given number is prime, as opposed to actually decomposing the number into its constituent prime factors

11
Primality Tests Deterministic: Adleman-Pomerance- Rumely Primality Test, Elliptic Curve Primality Proving… Probabilistic: Rabin-Miller Strong Pseudoprime Test…

12
Rabin-Miller Strong Pseudoprime Test Given an odd integer N, let N = 2 r s + 1 with s odd. Then choose a random integer a between 1 and N - 1. If a s = 1 (mod N) or a 2^j s = -1 (mod N) for some j between 0 and r - 1, then N passes the test. A prime will pass the test for all a.

13
Rabin-Miller Strong Pseudoprime Test Requires no more than (1 + o(1))logN multiplications (mod N). A number which passes the test is not necessarily prime. But a composite number passes the test for at most 1/4 of the possible bases a. If n multiple independent tests are performed on a composite number, the probability that it passes each test is 1/4 n or less.

14
Rabin-Miller Strong Pseudoprime Test Smallest composite numbers passing the RMSPT using the first k primes as bases: 2,047; 1,373,653; 25,326,001; 3,215,031,751; 2,152,302,898,747; 3,474,749,660,383; 341,550,071,728,321, 341,550,071,728,321; at most 41,234,316,135,705,689,041… 341,550,071,728,321 = …, 41,234,316,135,705,689,041 = … Tests show that randomized bases may fail sometimes.

15
Pseudocode of RMSPT (Sprache) function powermod(a, s, n) { p := 1 b := a while s > 0 { if s & 1 == 1 then p := p * b % n b := b * b % n s := s >> 1 }

16
Pseudocode of RMSPT (cont.) function rabin-miller(n) { if n > 2 AND powermod(2, n - 1, n) != 1 then return FALSE if n > 3 AND powermod(3, n - 1, n) != 1 then return FALSE if n > 5 AND powermod(5, n - 1, n) != 1 then return FALSE if n > 7 AND powermod(7, n - 1, n) != 1 then return FALSE if n > 11 AND powermod(11, n - 1, n) != 1 then return FALSE if n > 13 AND powermod(13, n - 1, n) != 1 then return FALSE if n > 17 AND powermod(17, n - 1, n) != 1 then return FALSE if n > 19 AND powermod(19, n - 1, n) != 1 then return FALSE if n > 23 AND powermod(23, n - 1, n) != 1 then return FALSE return TRUE }

17
Prime Factorization Algorithms Continued Fraction Algorithm, Lenstra Elliptic Curve Method, Number Field Sieve, Pollard Rho Method, Quadratic Sieve, Trial Division…

18
Pollard Rho Factorization Method Also known as Pollard Monte Carlo factorization method. Runs at O(p 1/2 ) where p is the largest prime factor of the number to be factored. Two aspects to this method: iteration and cycle detection.

19
Pollard Rho Factorization Method Iteration: Iterate the formula x n+1 = x n 2 + a (mod N). Almost any polynomial formula (two exceptions being x n 2 and x n 2 - 2) for any initial value x 0 will produce a sequence of numbers that eventually falls into a cycle.

20
Pollard Rho Factorization Method Cycle detection: Keep one running copy of x i. If i is power of 2, let y = x i, and at each step, compute GCD(|x i - y|, N). If the result is neither 1 nor N, then a cycle is detected and GCD(|x i - y|, N) is a factor (not necessarily prime) of N. If the result is N, the method fails, choose another a and redo iteration.

21
Pseudocode of RRFM (Sprache) function pollard-rho(n) { do { a := random() } while a == 0 OR a == -2 y := x k := 2 i := 1

22
Pseudocode of RRFM (cont.) while TRUE { i := i + 1 x := (x * x + a) % n e := abs(x - y) d := GCD(e, n) if d != 1 AND d != n then return d else if i == k then { y := x k := k << 1 }

23
Overall Efficiency Analysis Time complexity: O(logN) for any prime; O(N 1/4 ) for most composites under average conditions, principally decided by the factorization process Space complexity: O(1), space demand is always independent of N

24
Notes on Implementation Multiplication (mod N) in RMSPT: Requires calculation of 64bit * 64bit % 64bit. Should be computed as binary numbers using divide and conquer method. Use floating-point unit for (mod N) operation. Can be optimized by coding in assembly.

25
Notes on Implementation (cont.) GCD(a, b) (a > b) in PRFM: Following properties of GCD helps avoiding divisions: If a = b, then GCD(a, b) = a. GCD(a, b) = 2 * GCD(a/2, b/2) with both a and b even. GCD(a, b) = GCD(a/2, b) with a even but b odd. GCD(a, b) = GCD(a - b, b) with both a and b odd. Time complexity: O(logb)

26
Notes on Implementation (cont.) Combination with brute-force algorithms: Embed a prime table. Do brute-force trial division for small divisors. Minor optimizations: Use 32-bit integer division instead of the 64-bit version when possible…

27
Actual Timing Performance Platform: Windows XP SP1 on a Pentium M 1.5 GHz Algorithms tested: Adapted versions of westevers, TNs, lhs and zsuyrls solutions and my later fixed version Timing method: Process user time returned by the GetProcessTimes Windows API

28
Actual Timing Performance Test data: Original data set on POJ, two pseudorandom data sets generated by Mathematica and one hand-made data set (all given in ascending order), new data set on POJ Verification: Done with Mathematica

29
Actual Timing Performance Data0 (20) Data1 (10) POJ Data2 (24) Data3 (20) Data5 (23) westever 7, ,93724,46215,93531,375 TN 3,54560W/A12,4507,74415,935 Wheel 2,143402,4536,9324,1828,929 lh (-1) zsuyrl STYC

30
Conclusions Brute-force ways work too slow with integers that have large factors. But they are good compliments to complex methods like Pollard Rho. Original test data on POJ are too weak to have slow algorithms fail and to prove wrong solutions incorrect. New data have been much better but not yet perfect.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google