Prime number is a very useful number. So far, no one has found the distribution of prime numbers, and no one can use a formula to calculate all prime numbers. However, there is a way to judge and create prime numbers.

Common Method 1: The optimized enumeration method (efficiency O (N * SQRT (N) ranges from 2-enumeration to SQRT (n) according to the definition of prime numbers ). The method is easy to understand. This method can be used for judgment, but it takes too long to create it.

int isprime(int n){ for (int i=2;i<=sqrt(n);i++) { if (n%i==0) return 0; } return 1;}

Common Method 2: screening method. (Efficiency O (n ))

The screening method follows the principle that the multiples of prime I must not be prime.

(Reprinted below)

A simple process of screening prime numbers: n = 30.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30

After step 2, step 2, 4... 28, 30, the 15 units are marked as false, and the remaining are true.

Start Step 1:

I = 3; because Prime [3] = true, put prime [6], [9], [12], [15], [18], [21], [24], [27], [30], marked as false.

I = 4; because Prime [4] = false, the screening process is not continued.

I = 5; because Prime [5] = true, prime [10], [15], [20], [25], and [30] are marked as false.

I = 6> SQRT (30) algorithm ends.

In step 2, output the subscript with the prime [] value true:

For (I = 2; I <= 30; I ++)

If (prime [I]) printf ("% d", I );

The result is 2 3 5 7 11 13 17 19 23 29.

The screening method can also be optimized, so method 3 is available: the screening method is optimized.

Remove all the even numbers to further reduce the time complexity.

Template:

int prime(int a[],int n){ int i,j,k,x,num,*b; n++; n/=2; b=(int *)malloc(sizeof(int)*(n+1)*2); a[0]=2; a[1]=3; num=2; for (i=1;i<=2*n;i++) b[i]=0; for (i=3;i<=n;i+=3) for (j=0;j<2;j++) { x=2*(i+j)-1; while (b[x]==0) { a[num++]=x; for (k=x;k<=2*n;k+=x) b[k]=1; } } return num;}

There are also some interesting properties about prime numbers:

1. goldebach's conjecture that any even number greater than 4 in the famous prime number problem can be split into two prime numbers.

2. twin prime number problem. The prime number with a difference of 2 has an infinite pair.

3. In the first counterproof method, Euclidean proved that there is no largest prime number in the world, and this is not the largest but the largest.

4. Currently, humans (computers ?) The calculated maximum prime number is 2 ^ 43112609-1, a number of 723 W + digits .....