码迷,mamicode.com
首页 > 其他好文 > 详细

素数打表

时间:2016-08-11 11:19:11      阅读:123      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!