C Programming - Is that a Prime Number?
Interviewers favourite :)The problem is to find out whether a number is prime or not?
This is the one best solution that I could find out!!!
Solution:
/* returns 1, if number is prime else 0 */
int prime(int number)
{
int iter,bound;
if (!(number % 2)) return (number == 2);
if (!(number % 3)) return (number == 3);
if (!(number % 5)) return (number == 5);
for(iter = 7; (iter * iter)<= number; iter += 2)
if ( number % iter == 0 )
return 0;
return 1;
}
Labels: C
About this entry
You’re currently reading “
- Published:
- 9:57 pm
- by -
1 Comments (Post a Comment)