Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
The important word in my assertion isn't mathematical, it's predict. You cannot determine the closest prime p to any number n without a primality test. Now, brute force algorithms are mathematically describable; I'm not saying they're not.

Let me give you another example. Can you tell me the closest power of 2 to a given number N? Now, can you do it without breaking down a number into its composite primes? I can, by generating the sequence of powers of 2 and looking at them. That's predictive.

Here's pseudocode for both algorithms, so you can see the difference.

Brute-force algorithm: sub is_power { # Some algorithm to determine if a number is a power of two. } sub closest_power_of_two_brute { my $m = my $n = shift; return $n if is_power( $n, 2 ); while (1) { return $n if is_power( $++n, 2 ); return $m if is_power( $--m, 2 ); } }
Predictive algorithm: sub iterate_over { my $v = shift; return sub { $v *= $v; }; } sub closest_power_of_two_predictive { my $n = shift; my $it = iterate_over( 2 ); my $l = $it->(); while ( my $v = $it->() ) { last if $v > $n; $l = $v; } abs( $n - $l ) > abs( $n - $v ) ? $v : $l; }

Limbic~Region's example is much better than mine.

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.


In reply to Re^7: a close prime number by dragonchild
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2014-11-21 04:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (105 votes), past polls