Pathologically Eclectic Rubbish Lister PerlMonks

### Re: is it prime?

by tobyink (Prior)
 on May 06, 2012 at 06:19 UTC ( #969117=note: print w/ replies, xml ) Need Help??

in reply to is it prime?

As JavaFan said, the simplest solution is to just start your loop at 2:

```sub isPrime(\$) {
\$num = shift;
my (\$i,\$c);
for (\$i = 2; \$i < \$num / 2; \$i++)
{
if (\$num % \$i == 0){\$c += 1;}
}
if (\$c == 1){return 1;}
return 0;
}

The above does however produce a false negative for isPrime(2). It produces a negative for isPrime(1) which is probably correct, but could be considered to be a false depending on your stance on the much debated primality of 1.

Better though is:

```use 5.010;
use strict;

sub isPrime (_)
{
my \$num = shift;
for my \$div (2 .. sqrt \$num)     # start at 2, end at sqrt(\$n)
{
return if \$num % \$div == 0;  # return false if \$n evenly divid
+es
}
return 1;  # return true if we got through the entire loop
}

for (1 .. 30)
{
if (isPrime)
{
say "\$_ is prime";
}
else
{
say;
}
}

I've folded in JavaFan's other suggestions, plus changed your prototype from (\$) to (_). Prototypes are rarely helpful - you're best off leaving them out usually. In this case though, the (_) prototype is quite cool, because what it does is, if isPrime is called with no argument at all, then it checks \$_.

Other suggestions: consider what should happen for isPrime(1), isPrime(0), isPrime(-3) and isPrime("chimpanzee"). Add checks for those special cases before the for my \$div loop. The above implementation claims all the above are prime, except -3 which dies.

In mathematical circles, 1 is (these days) not usually regarded as prime, and asking the question of non-natural numbers (i.e. non-integers, and integers lower than 1) does not make sense. Bearing that in mind...

```use Carp qw/croak/;
sub isPrime (_)
{
my \$num = shift;

return if \$num == 1;  # 1 is not prime

croak "usage: isPrime(NATURAL NUMBER)"
unless \$num =~ /^[1-9][0-9]*\$/;

for my \$div (2 .. sqrt \$num)
{
return if \$num % \$div == 0;
}
return 1;
}
perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'

Comment on Re: is it prime?

Create A New User
Node Status?
node history
Node Type: note [id://969117]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2013-05-22 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?