Re: a close prime number

by Limbic~Region (Chancellor)
 on Feb 11, 2005 at 22:29 UTC ( #430327=note: print w/replies, xml ) Need Help??

in reply to a close prime number

Anonymous Monk,
Ok - this is an adaptation of Challenge: Nearest Palindromic Number. Since the mathematical lesson has already been explained, I decided to spruce up the Perl so that there might be a lesson in it.
```#!/usr/bin/perl
use strict;
use warnings;

my \$nearest_prime = nearest();

for ( map { int( rand 9998 ) + 2 } 1 .. 50 ) {
print "\$_ : ", \$nearest_prime->( \$_ ), "\n";
}

sub nearest {
my \$prime = is_prime();
return sub {
my \$n = shift;
return \$n if \$prime->( \$n );
my \$pos = \$n;
++\$pos while ! \$prime->( \$pos );
\$pos = \$pos - \$n;
my \$neg = \$n;
--\$neg while ! \$prime->( \$neg );
\$neg = \$n - \$neg;
return \$pos > \$neg ? \$n - \$neg : \$n + \$pos;
}
}

sub is_prime {
my %prime = map { \$_ => 1 } (2, 3, 5, 7);
my %not_prime;
return sub {
my \$n = shift;
return 1 if \$prime{ \$n };
return 0 if \$n % 2 == 0 || exists \$not_prime{ \$n };
for ( 3 .. sqrt \$n ) {
return \$not_prime{ \$n } = 0 if \$n % \$_ == 0;
}
return \$prime{ \$n } = 1;
};
}
I will leave converting the code from the nearest prime to the nearest N primes as an exercise for the reader.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: a close prime number
by gam3 (Curate) on Feb 14, 2005 at 18:44 UTC
The speed of the is_prime function can be increase by about 140% by using the 2 4 trick used below.
```sub is_prime {
my %prime = map { \$_ => 1 } (2, 3, 5, 7);
my %not_prime;
return sub {
my \$n = shift;
return 1 if \$prime{ \$n };
return 0 if \$n % 2 == 0 || \$n % 3 == 0
|| exists \$not_prime{ \$n };
my \$last = int sqrt \$n;
for ( my \$x = 5; \$x <= \$last ;) {
return \$not_prime{ \$n } = 0 if \$n % \$x == 0;
\$x += 2;
return \$not_prime{ \$n } = 0 if \$n % \$x == 0;
\$x += 4;
}
return \$prime{ \$n } = 1;
};
}
This version skips all of the numbers that are divisable by 2 and 3. Not checking numbers divisable by 2 is obvious, but not checking numbers divisable by 3 is less so, but reduces the search space by 25%.
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
I wonder if it's quicker to add the digits of the number when you're checking to see if it's divisible by three and seeing if that resultant sum is divisible by three.
It's not.
```use Benchmark qw( cmpthese );

my \$large_number = 3 * 1234647389;

cmpthese( -1, {
modulo => sub { \$large_number % 3 == 0 },
divide => sub { my \$v; \$v += \$_ for split //, \$large_number; \$v %
+3 == 0},
});

--------

Rate divide modulo
divide   30919/s     --   -99%
modulo 2120579/s  6759%     --

By a factor of 67 times faster. Or so ...

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Create A New User
Node Status?
node history
Node Type: note [id://430327]
help
Chatterbox?
 [shmem]: LanX: now I have to find a succinct transformation FOOL => MONK [LanX]: yeah but Marto already proposed a new "Lex Sun-D" ... [karlgoethebier]: the word really exists: http://www. urbandictionary. com/define.php? term=fool [shmem]: ...possibly involving RTFM [karlgoethebier]: big surprise! [Eily]: LanX I try to avoid answering, but I did feel that this one post was going into much detail to prove a false claim (that SHA-1 is secure, I was just wrong about how insecure it is) [LanX]: yeah whatever ... I'm in the favorable condition to already autohiding him ... how can I judge the poor FOOLs who still see his contributions xD [Eily]: he's been pretty saavy about threads that were implictly about him in the past. I think he might have deliberatly avoiding mentioning it for some reason (he got tired?) [Eily]: he did mention one of his propositions: basically make it possible to ignore downvotes, by separating them more from the upvotes

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-07-24 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (356 votes). Check out past polls.