欧拉第十题 求2000000以内的素数之和
#include <stdio.h>
#include <stdlib.h>
long long getSum(int n)
{
long long sum = 0;
char *array=(char*) malloc(n+1);
int i;
for(i=0; i<n+1; i++)
{
array[i]='r'; //'r' : remain
}
array[0]='d'; //'d' : delete
array[1]='d';
int p=2;
while(p*p<=n)
{
if(array[p]=='r')
{
int j;
for(j=2; j*p<=n; j++)
{
array[j*p]='d';
}
}
p++;
}
for(p=2; p<=n; p++)
{
if(array[p]=='r')
{
sum += p;
}
}
free(array);
return sum;
}
int main()
{
int max=2000000;
long long sum = getSum(max);
printf("sum of the prime numbers under %d is %lld\n", max, sum);
return 0;
}