Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: a close prime number by Limbic~Region
in thread a close prime number by Anonymous Monk

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.
  • Please read these before you post! —
  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [shmem]: but that would mean, also, to drop the "use" not allowed in expression abomination, and make use actually return something useful (as does require at the first call, accidentally)

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (10)
    As of 2017-11-21 17:30 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (306 votes). Check out past polls.