View Single Post
  #3 (permalink)  
Old 09-11-2007, 04:24 PM
kkelly's Avatar   
kkelly kkelly is offline
Learning Programmer
 
Join Date: Sep 2007
Posts: 50
Rep Power: 5
kkelly is on a distinguished road
Default

I definitely wouldn't use floats, and I wouldn't even use ints. Use unsigned ints. Understanding how numbers are handled by the processor is very important if not paramount. There are more than a few standards, and I'm sorry I don't have any links. You might want to google "Floating Point representation", or "numeric representation". Given your ouput, I'm guessing you problems are stemming from truncation, and/or floating point inprecision.
I didn't look through your code, but unless you are implementing an advanced algorithm, searching for primes is a simple matter.
Code:
unsigned int uintNum;			//	The number to test
unsigned int uintMod;			//	The modulus.

bool blIsPrime;				//       Prime? Yes or No
blIsPrime = true;				//	We'll assume it yes, and try to prove it otherwise

uintMod = 2;				//	We'll start at modulo 2 since it is our first potential factor

//	If our modulus is more than half our number we can stop testing.
//	So, while the number may be prime and the modulus is less than half
while ((blIsPrime) && (uintMod <= (uintNum/2)))
{
	//	if the number is in the modulus set it can't be prime
	if (uintNum%uintMod == 0)
	{
		blIsPrime = false;
	}
	else
	{
		//	increment modulus
		uintMod++;
	}
}
Reply With Quote