more useful options | |
PerlMonks |
Re: is it prime?by JavaFan (Canon) |
on May 06, 2012 at 05:39 UTC ( [id://969111]=note: print w/replies, xml ) | Need Help?? |
It cannot be correct. Have you actually tried this? Considering that any number is divisible by 1, isPrime will return 1 for each number. A fix is easy, have $i start at 2.
But then you are still far from being efficient. There's no need to loop to $num/2, you can stop at sqrt($num). There's no need to test even numbers (except 2). And there's no need to continue once you've found a divisor.
In Section
Seekers of Perl Wisdom
|
|