求<=n的所有素数

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc != 2) {
        printf("Usage: ./a.out <num>\n");
        exit(EXIT_FAILURE);
    }

    int n = atoi(argv[1]);
    int arr[n + 1];

    // init
    int i;
    for (i = 0; i <= n; i++)
        arr[i] = i;

    int p;
    for (p = 2; p < n; p++)
        for ( i = p + 1; i <= n; i++)
            if (i % p == 0)
                arr[i] = 0;

    // display all prime number
    for (i = 0; i <= n; i++)
        if (arr[i] != 0)
            printf("%d\n", arr[i]);

    exit(EXIT_SUCCESS);
}

你可能感兴趣的:(C++,c,算法,C#,J#)