1

The purpose of this is to obtain a genuine critique of my source code (based on a fairly simple concept). I realize I may not have the cleanest implementation for testing for prime, due to the fact I am not an aspiring mathematician. I am interested on critique of the code itself, the use of recursion, pointers, etc. Please post "better" implementations of sections, along with WHY it is better. This will allow me to learn, and possibly others, at the same time stimulating academic conversation. Thanks in advance.

```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*========================================================
/ GLOBAL VALUES
/ ========================================================*/
#define FALSE 0
#define TRUE (!FALSE)
#define TEST_EVEN 2.0 /* any integer evenly divisible by 2 is an even integer */
#define ZERO_REMAINDER 0 /* an even number has a zero remainder when divided by 2 */
/*========================================================
/ FUNCTION PROTOTYPE(s)
/ ========================================================*/
void findPrime(unsigned int *isPrime, const unsigned int *num, unsigned int *div);
/*========================================================
/ MAIN FUNCTION
/ ========================================================*/
int main(void)
{
unsigned int baseDiv = 2; /* Starting Divisor */
unsigned int isPrime = TRUE; /* a number is considered prime, until proven !prime */
unsigned int limit = 0; /* max [possible] chosen prime */
/* find primes from "limit" down to 2 */
do
{
printf("Enter a value for \"limit\" to find primes from 2 to \"limit\"): ");
scanf("%u", &limit);
}
while(limit <=2);
/* Recursive semi-exhaustive search to determine if number is prime */
while(limit > 1) /* 1 is not a prime number, no need to test it */
{
findPrime(&isPrime, &limit, &baseDiv);
if(isPrime == TRUE)
printf("\n%u is a prime number.", limit);
else
isPrime = TRUE; /* Number is !Prime. Reset isPrime for next iteration */
limit = limit - 1; /* Test the next consecutive integer to see if its prime */
baseDiv = 2; /* Reset */
}
return 0;
}
/*========================================================
/ FUNCTION DEFINITION(s)
/ ========================================================*/
/* Function to determine if an integer is a prime number */
void findPrime(unsigned int *isPrime, const unsigned int *num, unsigned int *div)
{
int rem = 0;
float rootMod = fmod( sqrt(*num), TEST_EVEN);
/* if the sqrt of num is an even number (remainder == 0), then it is not prime */
if(rootMod > ZERO_REMAINDER)
{ /* Sqrt mod 2 not equal to zero, so num "may" be prime */
/* Dont need to divide by 1, as any number !=0 is divisible by 1 and
dont need to keep testing once non-prime is determined. Start at dividing by
two, as any even number [other than 2] is not prime, and will be eliminated immediately */
if( (*div < *num) && (*isPrime == TRUE) )
{
rem = *num % *div; /* Calculate the remainder */
if(rem == 0) /* If the remainder is 0, then num is not prime */
*isPrime = FALSE;
else /* else, we need to check the next subsequent number -1 */
{
*div = (*div + 1); /* increment the base divisor */
findPrime(isPrime, num, div);
}
}
}
else /* number is not prime */
*isPrime = FALSE;
}
```