求素数

求2~2000的所有素数.有足够的内存,要求尽量快

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <assert.h>
 4 
 5 int array[2000] = {2};
 6 static int find = 1;
 7 bool adjust(int value)
 8 {
 9     assert(value>=2);
10     if(2 == value)
11         return true;
12     for (int i=0;i<find;++i)
13     {
14         if (0 == value%array[i])
15         {
16             return false;
17         }
18     }
19     return true;
20 }
21 
22 void GetPrime()
23 {
24     for (int n=3;n<=2000;++n)
25     {
26         if (adjust(n))
27         {
28             array[find++] = n;
29         }
30 
31     }
32 }
33 int _tmain(int argc, _TCHAR* argv[])
34 {
35     GetPrime();
36     system("pause");
37 
38     return 0;
39 }

posted @ 2014-09-03 14:58  kira2will  阅读(132)  评论(0编辑  收藏  举报