首页 > 代码库 > Summation of primes

Summation of primes

problem 10:Summation of primes

题意:求不大于200w的素数和

代码如下:

 1 #ifndef PRO10_H_INCLUDED
 2 #define PRO10_H_INCLUDED
 3 
 4 #include "prime.h"
 5 #include <cstring>
 6 
 7 int p[2000000];
 8 bool vis[10000005];
 9 long long solve(){
10     memset(vis,0,sizeof(vis));
11     long long sum=0;
12     int k=getPrime(10000000,p,vis);
13     for(int i=0;i<k&&p[i]<=2000000;++i)
14         sum+=p[i];
15     return sum;
16 }
17 
18 #endif // PRO10_H_INCLUDED

其中,int getPrime(int n,int *p,bool *vis)修改为:

技术分享
 1 int getPrime(int n,int *p,bool *vis){
 2     int k=0;
 3     for(int i=2;i<=n;++i){
 4         if(!vis[i])p[k++]=i;
 5         for(int j=0;j<k&&i*p[j]<=n;++j){
 6             vis[i*p[j]]=1;
 7             if(!i%p[j])break;
 8         }
 9     }
10     return k;
11 }
View Code

 

Summation of primes