我尝试使用一种稍微优化的算法来寻找素数#
来解决项目euler的第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
在这两种情况下输出都不正确。我个人认为必须对显示的错误做些什么。指向的错误是什么?
答案 0 :(得分:1)
错误指向您从主函数返回非零值。要解决该错误,只需将return Sum
替换为return 0
即可。但是,它对你的结果没有帮助。同样,我将main
的返回值更改为int
,但这实际上也没有任何区别。