[Number Theory Series] prime articles

This chapter describes some of number theory about prime numbers, given the limited space does not prove that friends need to prove themselves relevant evidence.

What is a prime number? Divisible only by themselves and 1 number is prime.

With this it is easy to obtain the following properties of prime number determination method.

bool isPrime(int x){
    if(x==1)return 0;
    for(int i=2;i*i<=x;i++)
        if(x%i==0)return 0;
    return 1;
}

Here to explain why i * i <= x, i * i <x transformed what was i <= sqrt (x).

Think about it, if we enumerate to sqrt (x) can not find a number divisible by it, there can be no back number can be divisible by it, because a> sqrt (x) if the number is an integer x in addition, its business is certainly <sqrt (x), and we've been enumeration <number sqrt (x) of the =, which did not, so> number about sqrt (x) also can not.

This method of determining the time complexity is O (sqrt (n)), the space complexity is O (1) is.

Then gives a determination method, time complexity of this method is about O (sqrt (n) / 3), the space complexity is O (1).

bool isPrime(int x){
    if(x==1)return false;
    if(x==2||x==3)return true;
    if(x%6!=1&&x%6!=5)return false;
    for(int i=5;i*i<=x;i+=6)
        if(x%i==0||x%(i+2)==0)return false;
    return true;
}

I ate was proved.

Then there are several primes the sieve method. We require [discussed below] is rounded down.

The first is the well-known Egyptian sieve, Eratosthenes sieve method.

Egypt screen based on the idea: For a number x, which is a multiple of 2x, 3x, 4x, 5x ... is not a prime number.

So, we start from two small to large number of scanning each x, it multiples 2x, 3x, 4x, ..., [N / x] * x is marked as a composite number .

When scanning a number, if that number is not marked as closing, then it can not be divisible by any of 2 ~ x-1 is, it is a prime number.

However, we find that Egypt will screen a number of duplicate tag. 8 for example, it will be labeled 2, 4 will be marked, so affecting the efficiency.

So we consider optimization, for each operand, beginning from the 2 x ^ x ^ 2, (x + 1 ) * x, ..., [N / x] * x is marked as a composite number.

Given the code:

 

int CoMP [MAXN], Prime [MAXN];
 // composite number flag (composite), prime labeled 
void Eratothenes ( int n-) { // screen where for ( int I = 2 ; I <= n-; I ++ ) {
         IF (CoMP [I]) Continue ;
        prime[i]=1;//i是质数
        for(int j=i;j<=n/i;j++)comp[i*j]=1;
    }
}

 

Egypt screen time complexity of the optimization is O (nloglogn), nearly linear, and well written, it is recommended to memorize.

That there is no linear sieve it, yes, but personally believe that Egypt would screen is enough, no need to write to the card often linear.

I want to know can own Baidu.

Then give some theorems, still the same, not evidence.

1. The only decomposition theorem

If the integer x> = 2, then x can be expressed as the product of a certain number of prime numbers in a unique form.

It can be written as: x = p1 ^ c1 * p2 ^ c2 * p3 ^ c3 * ... * pm ^ cm.

Which, ci are positive integers and prime numbers pi are satisfied p1 <p2 <... <pm

2. Wilson's Theorem

Futile theorem, if p is a prime, then (p-1)! ≡ 1

Inverse Theorem 3. Theorem Wilson

If for some positive integers p, there are (p-1)! ≡ -1 (mod p), p is a prime number.

3. Fermat Theorem

If p is a prime number, x is a positive integer, and x and p are relatively prime, then x ^ (p-1) ≡ 1 (mod p).

4. Fermat's Little Theorem

If p is prime, then x ^ p ≡ x (mod p).

 

There are some things :( ﹁﹁) ~ → to be filled pit  

 

Guess you like

Origin www.cnblogs.com/light-house/p/11788057.html