Korean, Edit

Prime numbers up to n in the C language, Eratosthenes’ sieve

Higher category : [C Language] C Language Table of Contents


a. GitHub



#include <stdio.h>
#include <stdlib.h>
/* This source is for finding all the primes between 1 and n with Erathosthenes' sieve*/

int main(int argc, char *argv[]) {
    int n, m;
    scanf("%d", &n);
    char c[n+1];
    int i, j;
    int count = 0;
    for(i=1; i <= n; i++){
        c[i] = 0; // clean the matrix
    }
    printf("[Types of prime numbers]\n");
    for(i = 2; i <= n; i++){
        if(c[i] == 0){ // "=" if i is one of primes,
            printf("%d ", i);
            m = n / i;
            for(j = 1; j <= m; j++) c[j*i] = 1; // mark i's multiples as composite number
            count ++;
        }
    }
    printf("\n\n[Number of prime numbers]\n%d개", count);
    return 0;
}


Input: 2016.02.09 20:52

results matching ""

    No results matching ""