美文网首页
素数打表

素数打表

作者: simple乐 | 来源:发表于2018-11-04 07:09 被阅读8次
    #include<iostream>
    #include<string.h>
    #define KEY 10000 
    using namespace std;
    int count = 0;
    int prime[KEY];//保存 素数的数组 
    int check[KEY];//是素数为1  不是素数为0 
    //下面函数 的工作 将输入数据的素数存入数组内 
    void fun(int max) ;
    int main()
    {
        memset(check,0,sizeof(check)); 
        int max;
        cin >> max;
        fun(max); 
        for(int i = 0; i< count;i++)
        cout << prime[i] << endl;
    }
    
    void fun(int max)
    {
        
        for(int i = 2;i < max;i++)//表示要数的当前数字 
        {
            if(!check[i])        //当前数字为质素放入质素数组钟 
            prime[count++] = i;   //0是质素 去掉所有的质素的倍数  为1 
            for(int j = 2;j*prime[count-1]< max;j++)    //将质数数组中的  shu 
            {
                check[j*prime[count-1]] = 1;
            }
        }
        
     } 
    

    相关文章

      网友评论

          本文标题:素数打表

          本文链接:https://www.haomeiwen.com/subject/vwxrxqtx.html