简体   繁体   English

Eratosthenes 的筛子不工作 C++

[英]Sieve of eratosthenes not working C++

So I am trying to make a C++ program to generate all prime numbers till a certain point but for some reason, it prints all numbers as non-primes after 2.所以我试图制作一个 C++ 程序来生成所有素数,直到某个点,但由于某种原因,它在 2 之后将所有数字打印为非素数。

int A[1000000];
void sieve(int till)
{
    for(int i = 2; i < till; i++)
    {
        if(A[i] == 0)
        {
            for(int j = i*i; j < till; j+=i)
            {
                A[j - 1] = 1;
                //printf("%i is NOT prime\n", ij);
            }
        }
    }
}

and in the main i do:主要是我做的:

int N;
scanf("%i", &N);
sieve(N);

but when I try to debug it says:但是当我尝试调试时,它说:

NOT PRIME 0
NOT PRIME 1
NOT PRIME 2
PRIME 3
NOT PRIME 4
PRIME 5
NOT PRIME 6
PRIME 7
NOT PRIME 8
PRIME 9
NOT PRIME 10
PRIME 11
NOT PRIME 12
NOT PRIME 13

Can anybody figure what am I doing wrong?有人能弄清楚我做错了什么吗?

EDIT: The A int table is automatically intialized to 0s as it is outside the main class.编辑:A int 表自动初始化为 0,因为它在主类之外。

Very easy.好简单。 We have a few trivial case:我们有几个小案例:

  1. 0 more than two divisors, so it's not prime 0 超过两个除数,所以它不是质数
  2. 1 has only one divisor, so it's not prime 1 只有一个因数,所以它不是质数
  3. 2 is the only even number which is prime 2 是唯一的偶数是素数

So, basically we should not calculate the even numbers.所以,基本上我们不应该计算偶数。 We are interested in only odd numbers.我们只对奇数感兴趣。

We will mark all non prime numbers as 1.我们将所有非质数标记为 1。

int A[1000000];
void sieve(int till)
{
    // mark all even numbers (except 2) as not prime
    for(int i = 4; i < till; i += 2)
    {
        A[i] = 1;
    }

    // now calculate not primes for only odd numbers
    for(int i = 3; i < till; i += 2 )
    {
        if(A[i] == 0)
        {
            for(int j = i*i; j < till; j+=(i+i))
            {
                A[j] = 1;
            }
        }
    }
}

int main()
{
    int N;
    scanf("%i", &N);
    sieve(N);

    for(int i = 2; i < N; i++)
    {
        if(A[i] == 1) printf("%d is not prime\n", i);
        else printf("%d is prime\n", i);
    }

    return 0;
}

There are many issues with your code.你的代码有很多问题。

  1. int A[1000000];

    You haven't initialized the array.你还没有初始化数组。 you should initialize A[0]=1 , A[1]=1 and all other indexes to 0 .您应该将A[0]=1A[1]=1和所有其他索引初始化为0

  2. for(int j = i*i; j < till; j+=i)

    i*i may lead to integer overflow. i*i可能会导致整数溢出。 you should probably use j=i+i to decrease your chances of overflow.您可能应该使用j=i+i来减少溢出的机会。

  3. A[j - 1] = 1;

    Not sure you are using proper array indexes.不确定您使用的是正确的数组索引。

This line:这一行:

A[j - 1] = 1;

Should be:应该:

A[j] = 1;

So, now should works well:所以,现在应该运行良好:

#include<stdio.h>

int A[1000000];
void sieve(int till)
{
    for(int i = 2; i < till; i++)
    {
        if(A[i] == 0)
        {
            for(int j = i*i; j < till; j+=i)
            {
                A[j] = 1;
            }
        }
    }
}

int main()
{
    int N,i;

    scanf("%i", &N);

    for (i = 0;i < N;i++)
        A[i] = 0;

    sieve(N);

    for (i = 2;i < N;i++)
        if (A[i])
            printf("%i is not prime\n",i);
        else
            printf("%i is prime\n",i);
}

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

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