简体   繁体   English

C中存在高迭代循环的程序没有给我输出..为什么呢?

[英]program in which there is high iterations loops in C doesn't give me an output .. why?

I made a program which print the sum of prime numbers which is below 4 million, it doesn't give me an output. 我制作了一个程序,输出小于400万的质数之和,但没有输出。 When I count prime numbers below 1000 for instance it works well. 例如,当我算素数低于1000时,它就可以很好地工作。 So what's the problem with high iterations? 那么高迭代有什么问题呢? Here is the function calculating the sum of the primes under a certain number. 这是计算一定数量下素数之和的函数。

long long is_prime(){

long long i,j,c=0,sum=0;

for(i=2;i<=4000000;i++){

    c=0;

    for(j=1;j<i;j++){

         if(i%j==0) c++;

    }

    if(c==1) sum+=i ;
}

return sum;
}

Can anyone help me? 谁能帮我?

It doesn't make sense 'doesn't give me output', whether it produces error or it just executing for long time. 不管是产生错误还是长时间执行,“不给我输出”都是没有道理的。 Your code's complexity is in O(n*n) that will take time for 4 million* 4 million iterations in worst case, this could be reason, wait for result. 您的代码的复杂度以O(n * n)表示,在最坏的情况下将花费400万* 400万次迭代的时间,这可能是原因,等待结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM