Perl Monk, Perl Meditation PerlMonks

### Re^4: Prime Number Finder

 on Sep 17, 2013 at 08:46 UTC ( #1054405=note: print w/replies, xml ) Need Help??

in reply to Re^3: Prime Number Finder

This code is quite fast, since it skips a lot of unnecessary operations
```#!/usr/bin/perl

use strict;
use warnings;
use POSIX;

my (\$i,\$j,\$h,\$sentinel) = (0,0,0,0);

# i>=3
for(\$i=1000000000; \$i<=1000000500; \$i++){
# if \$i is an even number, it can't be a prime
if(\$i%2==0){}
else{
\$h=POSIX::floor(sqrt(\$i));
\$sentinel=0;
# since \$i can't be even -> only divide by odd numbers
for(\$j=3; \$j<=\$h; \$j+=2){
if(\$i%\$j==0){
\$sentinel++;
# \$i is not a prime, we can get out of the loop
\$j=\$h;
}
}
if(\$sentinel==0){
print "\$i \n";
}
}
}

Create A New User
Node Status?
node history
Node Type: note [id://1054405]
help
Chatterbox?
 [Lady_Aleena]: Why does grep(/.*\$in.*/, @my_modules) work but grep { \$_ =~ /.*\$in.*/; \$_ } @my_modules not work? What did I do wrong in the second? [tobyink]: Why are you returning \$_ n your grep block? You should return a boolean. [tobyink]: *in your grep block

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-05-27 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.