Do you know where your variables are? PerlMonks

### Re: Prime Number Finder

by jbl_bomin (Acolyte)
 on Oct 10, 2011 at 19:26 UTC ( #930696=note: print w/replies, xml ) Need Help??

in reply to Prime Number Finder

Old discussion, but I figure I'll throw in my attempt at calculating primes without modules, and using only recursive functions (no loops)
```#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

\$DB::deep = 500;
\$DB::deep = \$DB::deep; # Avoids silly 'used only once' warning

no warnings "recursion";

# Identify primes between ARG0 and ARG1

my (\$x, \$y, \$re_int, \$result);
my (\$prime, \$is_int);

\$x = \$ARGV[0];
\$y = \$ARGV[1];

\$is_int = sub {
my \$re_int = qr(^-?\d+\z);
my (\$x) = @_;
\$x =~ \$re_int
? 1
: 0;
};

\$prime = sub {
my ( \$x, \$y ) = @_;
if ( \$y > 1 ) {
given (\$x) {
when ( \$is_int->( \$x / \$y ) ) {
return 0;
}
default {
return \$prime->( \$x, \$y - 1 );
}
}
}
else { return 1; }
};

\$result = sub {
my ( \$x, \$y ) = @_;
if ( \$x <= \$y ) {
if ( \$prime->(\$x, \$x-1) ) {
say \$x;
}
\$result->( ( \$x + 1 ), \$y );
}
};

\$result->(\$x, \$y);
Originally posted the above to my blog.

Replies are listed 'Best First'.
Re^2: Prime Number Finder
by Anonymous Monk on Jan 20, 2013 at 19:36 UTC
this is the actual code that works on windows and generates prime numbers upto the number you wish, check below code: #!usr/bin/perl -w use strict; use warnings; my \$o = 2; print "enter upto what no.you wish 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";

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";
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://930696]
help
Chatterbox?
 [Corion]: Hmm. I think overnight I decided on simplifying some code. I have plugin classes that do data import (.csv, .yml, .json) and for that create objects on which then ->load() is called. But YAML::XS doesn't have an object, so I wrote my own wrapper. [Corion]: This evening, I'll kill that wrapper again, and just call LoadFile() in the plugin class directly instead of creating a go-between object for no real gain. [Corion]: Writing these import plugins was really nice though - in about 2 hours, I had imports for CSV, YAML and JSON, and adding XLS(X), SQLite (or DBI) data sources is also trivial. I'm idly wondering about separating the plugin into transport+parser, so ... [Corion]: ... http:// URLs could be retrieved and then parsed, but I think that that would be overkill for a toy static site generator ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-05-22 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (163 votes). Check out past polls.

Notices?