/* Primes finder (primes.c) by Cobi *//* * We need this library (printf?). */#include <stdio.h>/* * We need this library for atoi(). */#include <stdlib.h>int main (int argc, char **argv) { /* Our main program. */ /* Make sure we have the correct arguementes. */ if (argc < 3) { /* We don't, complain. */ fprintf (stderr,"This binary requires the start "); fprintf (stderr,"and stop values as arguements 1 "); fprintf (stderr,"and 2.\n"); fprintf (stderr,"Example: %s [verbose] <start> <stop>\n",argv[0]); return 1; } /* We initialize our outer loop variables. */ unsigned long long bigcounter; 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; /* * And then we initialize our variable to * hold the status of the current check. */ unsigned short found = 0; /* Do we have 3 arguements? */ if (argc == 4) { /* * Yes, initialize our variables * on the assumption that we have * verbose as the first arguement. */ bigcounter = atoi(argv[2]); upperbound = atoi(argv[3]); if (strcmp(argv[1],"verbose") == 0) { verbose = 1; } else { verbose = 0; } } else if (argc == 3) { /* * No, we have only 2 arguements. * The first arguement is the start * and the second is the end. */ bigcounter = atoi(argv[1]); upperbound = atoi(argv[2]); verbose = 0; } else { /* * We have more than 3 arguements, * we don't know what to do with this. */ fprintf (stderr,"Uhh, we only need at most 3 arguements.\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 = 2; to = bigcounter; found = 0; halfto = to / 2; /* * We make an inner loop to loop through * each integer between 2 and half of the * integer we are checking for primality. */ while (counter <= halfto) { /* 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 % 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! Print it out * so the owner can see. */ printf("%d\n",to); } /* Increment the counter. */ bigcounter++; } /* Exit successfully. */ return 0;}