我尝试使用一种稍微优化的算法来寻找素数来解决项目欧拉的第 10 个问题
#include<stdio.h>
#include<math.h>
long long int main() {
int flag =1;
long int lim = 199999; //upto which the prime is to be generated
long int purposedPrime = 2;
long int PrimeSet[200000] = {0}, count=0;
long int i;
float sRoot_PP;
long long int Sum = 0 ; //This is for prj_euler :)
PrimeSet[count] = 2; count++; purposedPrime++;
PrimeSet[count] = purposedPrime ; count++;
Sum = 5 ;
while(purposedPrime < lim){
flag = 1;
purposedPrime += 2;
sRoot_PP = sqrt(purposedPrime);
for(i=0; i<count; i++){
if(PrimeSet[i]>sRoot_PP) break;
if(purposedPrime % PrimeSet[i] == 0){ flag = 0; break;}
}
if(flag == 1){
PrimeSet[count] = purposedPrime ;
count++;
Sum += purposedPrime;
}
}
printf("Total Count: %d", count);
printf("\n");
printf("Sum : %ld", Sum);
return Sum;
}
我在本地机器上运行它:Codeblocks with MinGW in Win8
. 输出
Total Count: 17984
Sum : 1709600813
Process returned 7096800813 (0x65E6702D) execution time : 0.063 s
Press any key to continue
我在codepad中运行它。输出
Total Count: 17984
Sum : 1709600813
Exited: ExitFailure 45
在这两种情况下,输出都不正确。我个人认为这与显示的错误有关。错误指向什么?