first, the definition of prime number
prime numbers are also called primes. Refers to the number of natural numbers greater than 1, except 1 and the integer itself, which cannot be divisible by other natural numbers (excluding 0). Because composite is obtained by multiplying several prime numbers, there is no composite without prime numbers, which shows that prime numbers have a very important place in number theory.
For example: 2,3,5,7,9 ..... are prime numbers.
Second, construct prime number algorithm
before writing the algorithm, the following is the first thing to say:
for any one composite n, if it has two mass factor X, Y, obviously n = x*y,
So, by the nature of inequality, x <= sqrt (n), i.e. x <= n^ (1/2).
Promote, for any one composite, if it has K-factor x1,x2,x3, ...., xk, obviously n = x1 * x2 * x3 * .... * XK,
Therefore, by the nature of inequalities, X <= n^ (1/k), that is, each mass factor must be less than or equal to n open k, where x belongs to the set { x1,x2,x3, ...., XK }.
Define a global variable and a global array, if the array is allocated on the stack, there is a capacity limit, so we define a global.
const unsigned int N = 50000; BOOL Flag[n+1];
1, algorithm one: by definition, you can quickly write a simple algorithm of constructing prime numbers
void Primecreatecommon ()//By definition {for (unsigned int i = 2; I <= N; ++i) {//By the front cushion, it is not difficult to understand. Because I do not know that there are several qualitative factors in this number,//I assume it only has two, the range is larger, the number of all the quality factor is less than or//equal to sqrt ((double) i) + 1. The other approximations are necessarily multiples of these factors//just so there is no need to go on. BOOL flag = true;for (unsigned int j = 2; J <= sqrt (double) i) +1; ++j) { if (i% j = 0) { flag = f Alse;}} if (flag)//{//cout << i << ""; }}cout << Endl;}
Analysis: The time complexity of the algorithm is O (N*SQRT (n)), when n relatively large (such as n = 50000), my machine ran for 20 seconds, also called algorithm, hehe, so next we look at another algorithm, that isThe Ancient Greek mathematician Erato a relatively labor-saving algorithm------ Erato Sieve method .
2, algorithm two: Screening method
First, list the number starting from 2. Then, put 2 on the list of prime numbers, and then draw all multiples of 2. By definition, the smallest number remaining-in this case, the 3--must be prime. Put this number on the list of prime numbers, and then draw all its multiples, so that there are some numbers left, whichever is the smallest, so repeated. The last thing left is a prime number.
Comparison of the running time between the three and two algorithms
Generates a number of prime time comparisons within 500,000
Four , Space-optimized
Slightly
< Span style= "margin:0px; padding:0px ">
Hill son
Reprint Please indicate the source, thank you. Original address:http://blog.csdn.net/s634772208/article/details/46327281
Optimization of prime number algorithm