首页 > 其他 > 详细

素数打表

时间:2016-08-11 11:18:04      阅读:170      评论:0      收藏:0      [点我收藏+]
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=1000007;
const int INF=0x3f3f3f3f;
const int mod=1000003;

LL a[maxn], vis[maxn], k;

///素数打表数组a里面存的就是2到MAXN之间的素数
void IsPrime()
{
    k=0;
    for(int i=2; i<maxn; i++)
    {
        ///用数组vis做标记数组
        if(!vis[i])///数组在没有初始化时,默认为都为0,不用初始化数组的原因
        {
            a[k++]=i;
            
            ///i倍数不会是素数标记为1
            for(int j=i+i; j<maxn; j+=i)
                vis[j]=1;
        }
    }
}

///主函数把素数输出看的更明显
int main()
{
    IsPrime();
    int n;
    while(~scanf("%d", &n))
    {
        for(int i=0; i<n; i++)
            printf("%lld ", a[i]);
        printf("\n");
    }
    return 0;
}

 

素数打表

原文:http://www.cnblogs.com/w-y-1/p/5760136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!