## frogboy77 73

``````bool is_prime(int a)
{
if(a==1){return false;}
if(a<4){return true;}
if(a%2==0){return false;}
if(a<9){return true;}
if(a%3==0){return false;}
int i;
for(i=5;(i*i)<=a;i+=6)
{
if(a%i==0){return false;}
if(a%(i+2)==0){return false;}
}
return true;
}``````

critique welcome.

## MosaicFuneral 812

You could simplify 3-7: `if(a || a<4 || !(a&1) || a<9 || !(a%3)) return(false);`

## frogboy77 73

would that be better?

## frogboy77 73

really! you find that easier to read?

## MosaicFuneral 812

If find it easier than going through each line and staring at a solid block of text.

## frogboy77 73

ok but i find following one statement after another easier than trying to evaluate 3 or 4 in one go. thanks for comments and i will keep them in mind.

## MosaicFuneral 812

You can also remove a<4 because you already have a<9.

## frogboy77 73

no then line 5 would return false if a=2

## frogboy77 73

i realise there is no error checking for a<1 but as the title says "simple"

## MosaicFuneral 812

To?

a<9 excludes all values below 9, so...

## frogboy77 73

yes but 2 would be evaluated before that

## ziggystarman 12

``````/***********************************************************
if a valid prime number return true or false
***********************************************************/
int is_prime(int _iVal)
{
char bPrime;
bPrime = true;
for (int ii(2); ii < _iVal / 2; ii++)
{
if (!(_iVal % ii)){
bPrime = false;
break;
}
}
return bPrime;
}``````

Would be better

## frogboy77 73

still pretty new at this but i don't get this

``````char bPrime;
bPrime = true;``````

## MosaicFuneral 812

bPrime is a bool not a char.

The return being a false or truth also means that it should be `bool is_prime(...)` not `int is_prime(...)`

## ziggystarman 12

``````is_prime(5);//returns true
is_prime(8);//returns false``````

char bPrime is the returned value true or false

## frogboy77 73

so you're returning a char value from an int function with a bool value?

## ziggystarman 12

``````/***********************************************************
Check to see if a valid prime number
***********************************************************/
bool is_prime(int _iVal)
{
bool bPrime;
bPrime = true;
for (int ii(2); ii < _iVal / 2; ii++)
{
if (!(_iVal % ii)){
bPrime = false;
break;
}
}

return bPrime;
}``````

Should be thus & returns 1 or 0
Point taken I didn't write the function got off the forum sometime ago. Did not spot that nice one.

## frogboy77 73

maybe just me being a noob but that looks like shit!

## MosaicFuneral 812

maybe just me being a noob but that looks like shit!

How so? It's very straightforward in fashion.

It could use a little cleaning up, but it looks fine for the most part.

## frogboy77 73

ok find all the primes from 10000000-20000000 with each one and time it.

## frogboy77 73

why go as far as i_Val/2?

## MosaicFuneral 812

ok find all the primes from 10000000-20000000 with each one and time it.

Why?

## ziggystarman 12

``Title "simple prime checker" ?``

## frogboy77 73

square root is far enough

## frogboy77 73

Why?

well how else you going test it?

## frogboy77 73

still waiting for a clarification on this

You can also remove a<4 because you already have a<9.