``````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.

Edited by frogboy77: n/a

3
Contributors
27
Replies
28
Views
7 Years
Discussion Span
Last Post by frogboy77

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

Edited by MosaicFuneral: n/a

would that be better?

really! you find that easier to read?

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

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.

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

no then line 5 would return false if a=2

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

To?

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

yes but 2 would be evaluated before that

``````/***********************************************************
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

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

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

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(...)`

Edited by MosaicFuneral: n/a

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

char bPrime is the returned value true or false

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

Edited by frogboy77: n/a

``````/***********************************************************
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.

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

Edited by frogboy77: n/a

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.

Edited by MosaicFuneral: n/a

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

why go as far as i_Val/2?

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

Why?

``Title "simple prime checker" ?``

square root is far enough

Why?

well how else you going test it?

still waiting for a clarification on this

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.