/* Primes finder (primes.c) by Cobi */ /* * We need this library (printf?). */ #include /* * We need this library for atoi(). */ #include int main (int argc, char **argv) { /* Our main program. */ /* Make sure we have the correct arguments. */ if (argc < 2) { /* We don't, complain. */ fprintf (stderr,"This binary requires the "); fprintf (stderr,"stop value as an argument"); fprintf (stderr,".\n"); fprintf (stderr,"Example: %s [verbose] \n",argv[0]); return 1; } /* We initialize our outer loop variables. */ unsigned long long bigcounter = 2; unsigned long long upperbound; /* Then we initialize our inner loop variables. */ unsigned long long to = 0; unsigned long long counter = 0; unsigned long long halfto = 0; /* We initialize our verbose flag. */ unsigned short verbose; /* We initialize our primes array. */ unsigned long long *primes = (unsigned long long*)malloc(0); unsigned long long nprimes = 0; /* * And then we initialize our variable to * hold the status of the current check. */ unsigned short found = 0; /* Do we have 2 arguments? */ if (argc == 3) { /* * Yes, initialize our variables * on the assumption that we have * verbose as the first arguement. */ upperbound = atoi(argv[2]); if (strcmp(argv[1],"verbose") == 0) { verbose = 1; } else { verbose = 0; } } else if (argc == 2) { /* * No, we have only 1 argument. * The first arguement is the start * and the second is the end. */ upperbound = atoi(argv[1]); verbose = 0; } else { /* * We have more than 2 arguments, * we don't know what to do with this. */ fprintf (stderr,"Uhh, we only need at most 2 arguments.\n"); return 1; } /* * We make a big loop through each integer * between argv[1] and argv[2]. */ while (bigcounter <= upperbound) { /* Reset any variables for the inner loop. */ counter = 0; to = bigcounter; found = 0; /* * We make an inner loop to loop through * each integer element of the primes array * between 0 and the number of array elements * - 1 integer we are checking for primality. */ while (counter < nprimes) { /* Is verbose on? */ if (verbose == 1) { /* Print verbose data to standard error output. */ fprintf (stderr,"counter: %d bigcounter: %d upperbound: %d\n",counter,bigcounter,upperbound); } /* * We check to see if the counter devides * evenly into the integer we are checking * for primality. */ if (to % primes[counter] == 0) { /* It is not prime, set found = 1 */ found = 1; /* and set counter so the loop exits. */ counter = to; } /* Increment the counter. */ counter++; } /* * Check to see if the loop exited normally * as opposed to prematurely. */ if (found == 0) { /* * If it exited normally, we found * ourselves a prime! Store it in * array, and print it out so the * owner can see. */ nprimes++; if( (primes = (unsigned long long*) realloc (primes, sizeof(unsigned long long) * nprimes)) == NULL) { fprintf (stderr,"Insufficient memory"); return 1; } primes[nprimes - 1] = to; printf("%d\n",to); } /* Increment the counter. */ if (bigcounter != 2) { bigcounter++; } bigcounter++; } /* Exit successfully. */ return 0; }