http://www.perlmonks.org?node_id=430620


in reply to Re^6: a close prime number
in thread a close prime number

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.