There's more than one way to do things PerlMonks

### Comment on

 Need Help??
gam3,
• Cache is now persistent
• Primeness is determined in C
• The 2 4 trick is used in checking near numbers also
• An iterator is used in lieu of a stack
```#!/usr/bin/perl
use strict;
use warnings;
use DBM::Deep;
use Inline C =>;

my \$prime = DBM::Deep->new( 'primes.db' );
#\$prime->{is}{\$_} = 1 for (2, 3, 5, 7, 11, 13, 17, 19, 23); # 1st run
+only
my \$next = find_near_primes(10000 => 100);

while ( my \$p = \$next->() ) {
print "\$p\n";
}

sub find_near_primes {
my (\$num, \$tot) = @_;
my (\$below, \$above, \$up, \$down);
my \$by_2_4 = sub { my \$n = shift; return sub { return \$n = \$n == 2
+ ? 4 : 2 } };
my \$dir = -1;

return sub {
return () if ! \$tot--;
\$dir *= -1;
if ( \$num ) {
\$num = \$num - \$num % 3;
\$num += not (\$num % 2) ? -1 : 2;
(\$below, \$above, \$num) = (\$num, \$num, undef);
(\$up, \$down) = (\$by_2_4->( 4 ), \$by_2_4->( 2 ));
\$below -= \$down->();
}
if ( \$dir == 1 || \$below < 5 ) {
if ( ! \$prime->{is}{\$above} ) {
\$above += \$up->() while \$prime->{not}{\$above};
while ( ! is_prime( \$above ) ) {
\$prime->{not}{\$above} = 1;
\$above += \$up->() while \$prime->{not}{\$above};
last if \$prime->{is}{\$above};
}
\$prime->{is}{\$above} = 1;
}
my \$p = \$above;
\$above += \$up->() and return \$p;
}
else {
if ( ! \$prime->{is}{\$below} ) {
\$below -= \$down->() while \$prime->{not}{\$below};
while ( ! is_prime( \$below ) ) {
\$prime->{not}{\$below} = 1;
\$below -= \$down->() while \$prime->{not}{\$below};
last if \$prime->{is}{\$below};
}
\$prime->{is}{\$below} = 1;
}
my \$p = \$below;
\$below -= \$down->() and return \$p;
}
};
}
__END__
__C__
#include <math.h>

short is_prime (unsigned long num) {
unsigned long i = 5;
unsigned long j = sqrt( num );

while ( i <= j ) {
if ( num % i == 0 ) return 0;
i += 2;
if ( num % i == 0 ) return 0;
i += 4;
}
return 1;
}
Tag - you're it!

Cheers - L~R

In reply to Re^3: a close prime number by Limbic~Region
in thread a close prime number 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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-06-20 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (116 votes). Check out past polls.

Notices?