素数打表

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

 

void Isprime()
{
    int i, j;

    for(i=2; i<N; i++)
    {
        if(v[i]==0)
        {
            a[k++]=i;

            for(j=i+i; j<N; j+=i)
                v[j]=1;
        }
    }
}

int Judge(long long n)
{
    int i;

    if(n==0 || n==1)return 0;

    for(i=0; a[i]*a[i]<=n; i++)
        if(n%a[i]==0)
            return 0;
    return 1;
}

 

posted @ 2016-08-11 10:54  爱记录一切美好的微笑  阅读(138)  评论(0编辑  收藏  举报