戊二烯筛 [英] Sieve of Erastothenes

查看:110
本文介绍了戊二烯筛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用eratosthenes筛子查找1-300中的质数的方法.我在解决问题时遇到了麻烦,所以任何帮助都很好! 顺便说一句,我是编程新手,所以如果您能保持简单,那将是最好的 下面是我的代码(到目前为止)

I'm trying to figure out how to use the sieve of eratosthenes to find the prime numbers from 1-300. I'm having trouble figuring it out, so any help would be nice! Btw, im new to programming so if you could keep it simple that would be best Below is my code (so far)

    #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();
  }

推荐答案

您可以在此处查看实现.

You can have a look at the implementation here.

筛网实施:

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;
}

还有一个写在Prime Numbers 链接上的博客.

And there is a blog written on Prime Numbers link.

这篇关于戊二烯筛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆