My algorithm ideas and implementation methods are in the code and comments, this way does make the algorithm complexity of a grade, very good ah.
#include <stdio.h> #include <time.h>/** * Use an array to find the first n prime numbers * To determine whether a number m is a prime, which can be determined by the divisible nature of the obtained prime numbers m * *//If you do not know the characteristics of prime numbers and the way to optimize ideasvoidGetnprimes_normal ();//Optimize the method aftervoidGetnprimes_optimize ();intMainvoid) {clock_t start,end; start = Clock ();//Start, get start time. //Usual practice of running time, input of n=10000 //getnprimes_normal (); //Optimized run timeGetnprimes_optimize (); end = Clock ();//end, get end timeprintf"Run time:%lf S",(Double) (End-start)/clocks_per_sec);return 0;}//common use of ideasvoidGetnprimes_normal () {/** * Number of saved prime numbers * @brief count * / intCount printf"Please the Count of Prime number:\n"); scanf"%d", &count);//Use an array to save the calculated prime number intPrimes[count];/** * First, the first known prime number is 2, * The calculation should start at 3 */primes[0] =2;intPC =1;intm =3;//Starting from the number 3 while(PC < count) {intK =0;//If you can't find a prime number here, you'll always be in this loop. while(K < PC) {if(m% primes[k] = =0) {m + =1; K =0; }Else{k++; } }//After finding the prime number, jump out of the loop above //This execution is performed first primes[pc] = m; //To perform pc++;Primes[pc++] = m; m+=1; }/** * Number of outputs operation * */ for(PC =0;p C < count;pc++) {printf ("%4d\t", primes[pc]); }}//Optimize the method aftervoidGetnprimes_optimize () {/** * Number of saved prime numbers * @brief count * / intCount printf"Please the Count of Prime number:\n"); scanf"%d", &count);//Use an array to save the calculated prime number intPrimes[count];/** * First, the first known prime number is 2, * The calculation should start at 3 */primes[0] =2;intPC =1;intm =3;//Starting from the number 3 while(PC < count) {/** * The first thing to solve is how to tell if a number is a prime numbers * 1: Except for the number 2, all other prime numbers are odd * 2: Suppose a number is k, as long as the K can be determined by K In addition, if divisible, K is * composite, if not divisible, K is prime number * * But, in order to reduce the complexity of the algorithm, we imagine that * p*q=k * is sure P and q: * p*p <=k words, q*q >= k *, only ask K can be K square root before the number is divisible. * * Based on this idea, our implementation is as follows: */ intK =0;//If you can't find a prime number here, you'll always be in this loop. while(Primes[k] * primes[k] <= m) {if(m% primes[k] = =0) {m + =2;//Except for the number 2, all other prime numbers are oddK =1;//Do not use the number 2 to test}Else{k++; } }//After finding the prime number, jump out of the loop above //This execution is performed first primes[pc] = m; //To perform pc++;Primes[pc++] = m; m+=2; }/** * Number of outputs operation * */ for(PC =0;p C < count;pc++) {printf ("%4d\t", primes[pc]); }}
Here is the result of my operation, the first is no optimization results, the second is the result of the algorithm optimization, the effect is still very obvious.
This is the result of no optimization:
This is the result of the optimization:
Using arrays to find the first n prime numbers