c - 埃拉托色尼筛法

标签 c sieve-of-eratosthenes sieve

我正在尝试弄清楚如何使用埃拉托色尼筛法找出 1-300 之间的素数。我无法弄清楚,所以任何帮助都会很好! 顺便说一句,我是编程新手,所以如果你能保持简单,那将是最好的 以下是我的代码(到目前为止)

    #include <stdio.h>
    #include <simpio.h>
    #include <genlib.h>
    #include <math.h>

    #define max 301

    main()
    {
         bool is_prime[max];
         int i, int1, j, n;
         int1=sqrt(max);

  for(n=0; n<=max; n++);
  {
           is_prime[n]=TRUE; //set everything to prime
  }

  is_prime[0]=FALSE; //false = NOT prime
  is_prime[1]=FALSE;
  for(i=2; i<int1; i++); //multiply starting from 2 end at 17
  {
           for(j=i; j<=(max/i); j++); //number being multiplied by
           {
                    n=(j*i);
                    is_prime[n]==FALSE; //all multiples of i are false
           }
  }
  if (is_prime[n]=TRUE); //print all prime numbers
  {
                        printf("%d", n);
  }
  getchar();
  }

最佳答案

您可以在此处查看实现。

筛选实现:

bool arr[1000001];
int main()
{
    arr[0]=arr[1]=1;
    for(int i=4;i<1000001;i+=2)
        arr[i]=1;
    for(int i=3;i<1000001;i+=2)
    {
        if(!arr[i])
            for(int j=2;i*j<1000001;j++)
            {
                arr[i*j]=1;
            }
    }
    return 0;
}

还有一篇关于素数的博客link .

关于c - 埃拉托色尼筛法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891524/

相关文章:

ruby - 为什么我的埃拉托色尼筛法算法失败了?

c - scanf() 在文件处理中不起作用?

c - 当使用具有不同参数的函数时,我应该在函数原型(prototype)中放置什么?

python - 使用编译使我的程序更快

r - 有没有办法将筛图上的标签向上移动以使图表看起来更干净?

python - 除 Sieve 之外的快速素数生成器

python - 埃拉托色尼递归筛不返回任何内容

c++ - 是否可以在不重新编译的情况下更改 exe 的图标?

c - socket bind() 返回错误 EINVAL

c - Spoj PRIME1 使用埃拉托色尼筛(在 C 中)