试编写一个程序,找出前N个质数.尽可能用最快的方法.
--------------------------------------------------------------------------------
#include <stdio.h> #include <time.h> #include <math.h>
#define PRIMEMAX 200
int main(void) { double s,t; int prime[PRIMEMAX]; int N = PRIMEMAX; int count = 0; int i = 2; int j = 2; s = clock(); while(count<N) { if(i==2 || i==3 || i==5 || i==7) { printf("%d...",i); prime[count] = i; count++; i++; continue; } if(i%2==0){i++;continue;} if(i%3==0){i++;continue;} if(i%6==0){i++;continue;} if(i%5==0){i++;continue;} if(i%7==0){i++;continue;} for(j=0; j<count; j++) { if(i%prime[j] == 0) //用质数去除来判断 { i++; continue; } } printf("%d...",i); prime[count] = i; i++; count++; } t = clock(); printf("\n-------time:%fms\n",t-s);
return 0; }
|
阅读(1071) | 评论(0) | 转发(0) |