小编Mah*_*bry的帖子

Eratosthenes筛选的上限用于查找素数的算法使程序停止工作

我已经使用了Eratosthenes算法的Sieve来查找在一定限度下的素数之和,并且它已经正常工作直到200万的限制,但是当我尝试了300万时,程序在执行时停止了.这是代码:

int main(){

    bool x[3000000];
    unsigned long long sum = 0;

    for(unsigned long long i=0; i< 3000000; i++)
        x[i] = true;

    x[0] = x[1] = false;

    for(unsigned long long i = 2; i < 3000000; i++){
        if(x[i]){
            for (unsigned long long j = 2; i * j < 3000000; j++) {
                x[j*i] = false;
            }
            sum += i;
        }
    }

    printf("%ld", sum);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c loops runtime-error sieve-of-eratosthenes

3
推荐指数
1
解决办法
192
查看次数

标签 统计

c ×1

loops ×1

runtime-error ×1

sieve-of-eratosthenes ×1