go ahead... be a heretic PerlMonks

### Re^3: Prime Number Finder

by chith (Initiate)
 on Jan 20, 2013 at 19:45 UTC ( #1014325=note: print w/replies, xml ) Need Help??

in reply to Re^2: Prime Number Finder

this is the actual code which works for windows

```#!usr/bin/perl -w

use strict;
use warnings;

my \$o = 2;
print "enter upto what number you wish to generate the primes: ";
my \$e = <STDIN>;
my (\$i,\$j,\$p);
my @prime_=();
print "prime numbers are:\n";

for(\$i=\$o; \$i<=\$e; \$i++)
{
\$p=0;
for(\$j=1; \$j<=\$i; \$j++)
{
if(\$i % \$j== 0)
{
\$prime_[\$p] = "\$j";
\$p++;
}
if (\$prime_[1] == \$i)
{
print "\$i\t";
}
}
}
print"\n";

Replies are listed 'Best First'.
Re^4: Prime Number Finder
by Anonymous Monk on Sep 17, 2013 at 08:46 UTC
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://1014325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2021-05-11 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (115 votes). Check out past polls.

Notices?