MDGSF Software Engineer

[数学] prime generate

2016-03-11
Art

生成10000以内的所有素数

#include <stdio.h>

#define N 10000

int main()
{
    int i;
    int j;
    int a[N];

    for (i = 2; i < N; i++) a[i] = 1;

    for (i = 2; i < N; i++)
    {
        if(a[i])
        {
            for (j = i; i*j < N; j++)
                a[i*j] = 0;
        }
    }

    for (i = 2; i < N; i++)
        if(a[i])
            printf("%4d ", i);
    printf("\n");
    return 0;
}

weixingongzhonghao

Similar Posts

上一篇 [Qt] 打开网页

Comments