Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
There is no way of predicting what the next prime number is, given any other number. If there was, then current cryptography methods wouldn't work. All prime number generators are, for the most part, variations on brute force. The method you're looking at is probably as good as any.

Huh?!? You're joking, aren't you? The same fact that you say "all prime number generators are, for the most part, variations on brute force" is in contradiction with the claim that "there is no way of predicting what the next prime number is, given any other number".

More precisely if I have any isprime() sub, e.g.

sub isprime { my $n=shift; return undef if abs $n == 1; return isprime -$n if $n<0; return 0 if $n == 0; $n%$_ or return 0 for (2..sqrt $n); 1; }
(I'm not posting a better one for others already have, and even better ones are available elsewhere.) or the witty
sub isprime { (1 x shift) !~ /^1?$|^(11+?)\1+$/; }
(this is Abigail's - who else?) then a sub like this:
sub nearest_prime { my $n=my $m=shift; return $n if isprime $n; while (1) { return $n if isprime ++$n; return $m if isprime --$m; } }
would answer the OP's question.

Important: not only primality testing algorithms do exist, but fast ones exist too. What in some sense there is "no way" to do is to factorize a composite number which is a different problem. But really even for the latter problem there are perfectly deterministic algorithms. "Only" it is believed to be computationally "hard" (in a mathematically precise sense), whereas the former is not. It is on this last conjecture (not proved yet!) that many cryptography methods rely.

Since the OP did not specify the size of a problem, but gave an example with small numbers, an effective answer in terms of some efficient sieving algorithm can be given.

In reply to Re^4: a close prime number by blazar
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
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2018-06-23 03:39 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.