Generating primes fast is very important in some problems.
Let's cut to the chase and introduce Eratosthenes's Sieve.
The main idea is the following. Suppose we want to find all primes between 2 and 50.
Iterate from 2 to 50. We start with 2. Since it is not checked, it is a prime number. Now check all numbers that are multiple of except 2. Now we move on, to number 3. It's not checked, so it is a prime number.
Now check all numbers that are multiple of , except 3. Now move on to 4. We see that this is checked - this is a multiple of 2! So 4 is not a prime. We continue doing this.
Here's the implementation.
#include <stdio.h> int primechk[21000]; void preprocess(void) { int i, j; for(i=2 ; i<=20000 ; i++) { primechk[i]=1; } for(i=2 ; i<=20000 ; i++) { if(primechk[i]==1) { for(j=2 ; i*j<=20000 ; j++) { primechk[i*j]=0; } } } } int main(void) { preprocess(); int i, cnt=0; for(i=2 ; i<=20000 ; i++) { if(primechk[i]==1) { cnt++; printf("%d is the %dth prime!\n",i,cnt); } } }
Okay, so what is the time complexity? To get all primes in the interval , the TC of this algorithm is
Very very fast. To prove this, notice that the number of iterations are something like where is a prime.
Well, Merten's Second Theorem states that (natural logarithm, by the way) so this prove the TC.
Now we know how to generate prime numbers fast. How about primality testing?
Naive solutions first. Given an integer , we can check numbers up to to find if a number divides . If there is such a number, is composite. If not, is a prime. This gives the solution in .
Here's a "solution" in using the fast exponentiation we will talk about in the next section. It's called Miller-Rabin Primality Test.
I'll introduce the deterministic version. We probably (hopefully) won't see stuff like in contests.
Here's the sketch of the algorithm. Choose some set of . We will run the algorithm with different s, and the more s we run this algorithm with, the more accurate this primality test is going to be.
Decompose as . Then check if the following holds.
If there is an that satisfies this, is composite. If not, is a prime.
For , we can just check for and be sure about it.
For , we can check for and be confident.
Now let us look at prime factorization of numbers.
Assume that we generated prime numbers using the Eratosthenes's Sieve.
If is in the "prime-generated" range, we can actually do prime factorization in .
Make another array. While we are doing the Sieve, for composite numbers, put "the first prime that verified that this number is composite" and for prime numbers, put itself. This is easy to implement.
Then we can start with , and continue to divide the prime numbers in the array.
#include <stdio.h> int primechk[21000]; int fprime[21000]; void preprocess(void) { int i, j; for(i=2 ; i<=20000 ; i++) { primechk[i]=1; } for(i=2 ; i<=20000 ; i++) { if(primechk[i]==1) { fprime[i]=i; for(j=2 ; i*j<=20000 ; j++) { primechk[i*j]=0; if(fprime[i*j]==0) { fprime[i*j]=i; } } } } } int main(void) { preprocess(); int n; scanf("%d",&n); while(n!=1) { printf("%d\n",fprime[n]); n=n/fprime[n]; } }
If not, we can just check through all primes less than and divide by those primes until we can't.
If all the primes multiply up to , we are done. If not, there is exactly one prime more than that divides .
Another algorithm involving prime factorization is Pollard's rho algorithm - since the pseudocode is simple, I'll leave you the wikipedia link. https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm#Algorithm
Comments
Post a Comment