primes/primes.alt.c

44 lines
806 B
C

// C version of a Prime Number Finder
// Alternate Algorithm: Instead of using sqrt function,
// track it with two variables
// haplo@mindstab.net
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned int i, x, max=2, isP, sqr=4;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesaltc <MAX NUM>\n");
return -1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return -1;
}
for(i=3; i<=MAX_NUM; i+=2)
{
isP=1;
for(x = 3; x<=max; x+=2)
{
if( ! (i%x)){
isP=0;
break;
}
}
if(isP) printf("%d\n", i,max, sqr);
if(i>=sqr) {
max++;
sqr = max*max -2;
}
}
}