in reply to A natural occurrence

Very nifty. Deconstructed thus:
use Math::BigFloat; use strict; use warnings; Math::BigFloat->accuracy(400); # the length of the prime to find in e my $prime_length = $ARGV[0] || 10; # build e (1/0! + 1/1! + 1/2! + 1/3! + ... + 1/125!) my $e = do { my $t = Math::BigFloat->new; $t += 1/Math::BigFloat->new($_)->bfac for 0..125; $t->bstr }; # grab substrings for (0 .. length($e) - $prime_length) { my $potential = substr $e, $_, $prime_length; print "found prime $potential \@ position $_\n" and last if is_prime($potential); } sub is_prime { my ($p) = @_; $p % $_ == 0 and return for 2, 3 .. sqrt($p); # 2, 3 .. sqrt($p) is + my trick return 1; }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: A natural occurrence
by davido (Cardinal) on Mar 01, 2006 at 20:41 UTC

    Very good japhy. Your reconstruction looks almost identical (including variable names) to my original pre-obfuscated version, with the exception of your 'trick'. :) I think I did put the generation of e into its own subroutine in my original code just for modularity, but that's about the only real difference.

    I read about the Google billboard and thought the concept was kind of fun. It gave me a new thought to toy with, and this was the outcome.


    Dave

Re^2: A natural occurrence
by jdalbec (Deacon) on Mar 04, 2006 at 16:03 UTC
    How is 2, 3 .. sqrt($p) different from 2 .. sqrt($p)?

    Update: I guess that applies to .718281 as well. Not to mention any all-0 string. In benizi's golf below I think 2..sqrt would work just as well because he starts at position 2 (skipping the decimal point) and explicitly excludes any string starting with a 0.

      In the particular case of 2.71828, its square root is less than 2, so 2 .. sqrt($p) is no good for us.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart