Anonymous Monk has asked for the
wisdom of the Perl Monks concerning the following question:
hi all i wrote this function to check if the number is prime or not but i just want to confirm if i'm doing this correctly
:
sub isPrime($) {
$num = shift;
my ($i,$c);
for ($i = 1; $i < $num / 2; $i++)
{
if ($num % $i == 0){$c += 1;}
}
if ($c == 1){return 1;}
return 0;
}
Re: is it prime? by JavaFan (Canon) on May 06, 2012 at 05:39 UTC 
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.  [reply] 

i did test it, you are correct it is incorrect i was hoping for a fix (hence why i posted) thanks for the help
 [reply] 

also you are right again i dont need a loop hell knows why i was using one sinse im only sending 1 arg (digit) at a time to the sub routine :/
 [reply] 

Re: is it prime? by tobyink (Abbot) on May 06, 2012 at 06:19 UTC 
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 nonnatural numbers (i.e. nonintegers, 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 =~ /^[19][09]*$/;
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'
 [reply] [d/l] [select] 
Re: is it prime? by 2teez (Priest) on May 06, 2012 at 07:25 UTC 
ofcourse there are several ways of checking or testing primality of integers, I used Trial division method. There are other better method I believe. Check below:
use warnings;
use strict;
is_prime( $ARGV[0] );
sub is_prime {
my ($val) = @_;
my $num_sqrt = int( sqrt($val) );
my @factor;
for ( 2 .. $num_sqrt ) {
if ( ( $val % $_ ) == 0 ) {
push @factor, $_;
}
}
my $count = () = @factor;
print $val, " is a Prime Number" if $count == 0;
}
And 2 was used here because 1 is not a prime number by defination, which state that a prime number must have two factors: 1 and itself, but 1 has only itself! too bad for 1 though.
Also look into good advice given by tobyink under other suggestion on this post
 [reply] [d/l] 
Re: is it prime? by johngg (Abbot) on May 06, 2012 at 12:15 UTC 
use strict;
use warnings;
use 5.010;
say
qq{$_ is },
isPrime( $_ ) ? q{} : q{not },
q{prime}
for 75, 169, 229, 367, 369, 8794, 9227;
sub isPrime
{
my $toTest = shift;
my $sqrtLimit = sqrt $toTest;
my $sieve = q{};
vec( $sieve, 0 , 1 ) = 1;
vec( $sieve, 1 , 1 ) = 1;
vec( $sieve, $toTest, 1 ) = 0;
my $marker = 1;
while ( $marker < $sqrtLimit )
{
my $possPrime = $marker + 1;
$possPrime ++ while vec( $sieve, $possPrime, 1 );
my $fill = 2 * $possPrime;
while ( $fill <= $toTest )
{
vec( $sieve, $fill, 1 ) = 1;
$fill += $possPrime;
}
last if vec( $sieve, $toTest, 1 );
$marker = $possPrime;
}
return not vec( $sieve, $toTest, 1 );
}
The output.
75 is not prime
169 is not prime
229 is prime
367 is prime
369 is not prime
8794 is not prime
9227 is prime
I quickly adapted this from a much older program I had lying around that lists all the primes up to a limit.
I hope this is of interest.
Update: Not so speedy, apparently! If only I had a computer science background and understood all the O(log n) gubbins :D Also updated the broken link.
 [reply] [d/l] [select] 

One speedy way is to use the Sieve of Eratosthenes.
It's speedy if you want all primes below a certain number, or if you need to determine primality of "a lot of" numbers, but it isn't for checking a single number.
Checking all the possible divisors can be done in O(√N) (throw in a factor of O(log n) to do the division if you have really big numbers). However, just initializing the sieve will take you Ω(N) time. And then you still have to do the work: Ω(N/p_{i}) for the i^{th} prime.
The fastest way is probably just to use bigprimes.net, which has the first 1.4 billion primes on file.
 [reply] 

NUMBER : TOBYINK JOHNGG
NUMBER : RESULT TIME RESULT TIME
75 : NO 0.000065 NO 0.000215
169 : NO 0.000057 NO 0.000649
229 : YES 0.000070 YES 0.000888
367 : YES 0.000073 YES 0.001437
369 : NO 0.000052 NO 0.000805
8794 : NO 0.000051 NO 0.008682
9227 : YES 0.000113 YES 0.031170
10807 : NO 0.000131 NO 0.047135
11939 : YES 0.000121 YES 0.045032
14803 : NO 0.000122 NO 0.054764
19937 : YES 0.000143 YES 0.070692
19938 : NO 0.000040 NO 0.020510
39783 : NO 0.000057 NO 0.065555
47083 : NO 0.000230 NO 0.170125
199933 : YES 0.000346 YES 0.778786
For some of those higher numbers, mine is about 2000 times faster than yours. The sieve is efficient if you need to generate a list of all prime numbers below x, but very slow as a mechanism for determining whether a given number is prime.
Benchmark script:
perl E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]>[3])=~s{::}{ }and$monkey}}"Monkey say">Monkey::do'
 [reply] [d/l] [select] 

I'd only ever used Eratosthenes' Sieve to find a list of all primes up to a limit and, stupidly, assumed that it would be equally good at testing whether a single number was prime. Silly me!
You have shown your method to be much faster but there's still room for improvement. You reject all even numbers the first time through your loop so there's no point in using even numbers again as divisor. Instead, take the evens test out of the loop then employ a Cstyle loop to test only odd divisors.
The code. I've tweaked things to return 0 if not prime in order to make testing easier.
The benchmark results.
Testing with value 2 which is a prime
ok 1  eratosthenes
ok 2  tobyink
ok 3  tobyinkPlus
Testing with value 8357 which is not a prime
ok 4  eratosthenes
ok 5  tobyink
ok 6  tobyinkPlus
Testing with value 9293 which is a prime
ok 7  eratosthenes
ok 8  tobyink
ok 9  tobyinkPlus
Rate eratosthenes tobyink tobyinkPlus
eratosthenes 0.154/s  100% 100%
tobyink 1291/s 838266%  35%
tobyinkPlus 1984/s 1288047% 54% 
1..9
I hope this is of interest.
 [reply] [d/l] [select] 

Re: is it prime? by brx (Pilgrim) on May 06, 2012 at 14:21 UTC 
sub isPrime($) {
$n=pop;
1^(1x$n)=~/^(1{2,$n})\1+$/;
}
With n>2
Just for fun as a contrib to https://github.com/fxn/mathwithregexps/blob/master/oneliners.sh (fxn).
Update, thanks to ambrus's reply: Re^2: is it prime?
sub isPrime($) {
(1x pop)!~/^(11+)\1+$/
}
 [reply] [d/l] [select] 

 [reply] 
Re: is it prime? by Anonymous Monk on May 06, 2012 at 21:57 UTC 
hi all i wrote this function to check if the number is prime or not but i just want to confirm if i'm doing this correctly : Didn't your teacher provide a list of test values for your homework?
 [reply] 

