pad.mattdiesel.co.uk

Snippet - Primes test code

Primes test code (C)

For SO question
Created 2014-04-11 16:40:32.772140 by Matt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

#define numt 20

int main(int argc, char const *argv[])
{

    int print = argc % 2;
    int primes[numt] = { 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 };


    int numprimes = 0;
    if(print) {
        for(int i = 0; i < numt; i++) {
            if(primes[i]) {
                printf("%d\n", i);
                numprimes++;
            }
        }
    //fast
    } else {
        for(int i = 0; i < numt; i++) {
            if(primes[i]) {
                numprimes++;
            }
        }
    }

    return 0;
}