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'

In reply to Re: is it prime? by tobyink
in thread is it prime? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.