/**********************************************************************/
/*  Jeff Balsley      Sun Jan 28 20:22:46 PST 2001                    */
/*  This code will calculate prime numbers                            */
/*  gcc -lm find_primes.c -o find_primes                              */
/**********************************************************************/

#include <stdio.h>

#define START 3       /* START must be an odd number  */
#define END 10000     /* Find all the primes up to this integer  */
#define TRUE 1
#define FALSE 0

int main(void)
{
    int i,j;
    int prime;
    FILE *outp;

    /* we will be starting on an odd number and only testing odd     */
    /* numbers to see if they are prime.  Since 2 is the only even   */
    /* prime we should just print it first                           */

    outp = fopen("primes.dat", "w");
    fprintf(outp, "2\n");
    fclose(outp);

    for (i=START; i <= END; i+=2){  /* test only odd numbers */
        prime = TRUE;
        j = 3;
        while ( prime == TRUE && (j < (i+1)/2.0) ){
            if( ( i % j ) == 0 ){
                prime = FALSE;
            }
            j+=2;
        }
        /*  print prime number to file  */
        if (prime == TRUE){
            outp = fopen("primes.dat","a");
            fprintf(outp, "%d\n", i);
            fclose(outp);
        }
    }
    /* fclose(outp); */
    return(0);
}
