primes/primes.c

42 lines
701 B
C

// C version of a Prime Number Finder
// haplo@mindstab.net
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char ** argv)
{
unsigned int counter, max_test, tester;
char isPrime;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesc <MAX NUM>\n");
return 1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return 1;
}
for(counter =3; counter<= MAX_NUM; counter +=2)
{
max_test = (int) sqrt((double) counter);
isPrime = 1;
for(tester =3; tester <= max_test; tester += 2)
{
if(! (counter % tester)) {
isPrime = 0;
break;
}
}
if(isPrime) printf("%d\n", counter);
}
return 0;
}