You can run this with or without a numeric argument. I wouldn't recommend numeric arguments past 15; it becomes pretty processor bound.

$p=Math::BigFloat;$n=$ARGV[0]||10;$p->accuracy(400);$w=do{$w=$p->new; $w+=1/$p->new($_)->bfac for 0..125;$w->bstr};for(0..length($w)-$n){$z =substr$w,$_,$n;use Math::BigFloat;if(ip($z)){print"$z \@ $_$/";last} }sub ip{($y)=@_;($s,$d)=(sqrt$y,2);{return if$y%$d==0;last if$d++>$s; redo}1}

Dave

Replies are listed 'Best First'.
Re: A natural occurrence
by japhy (Canon) on Mar 01, 2006 at 20:35 UTC
    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

      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

      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
Re: A natural occurrence
by stubblyhead (Initiate) on Mar 03, 2006 at 22:49 UTC
    a nice program, although i think i've found a bug. it'll tack on leading zeros instead of continuing through the series to find something that doesn't begin with one or more zeros. it happens at 5 and 17; probably elsewhere.
Re: A natural occurrence
by benizi (Hermit) on Mar 03, 2006 at 16:49 UTC

    Fun stuff, davido. I like it a little better using bignum, and it bugged me that the 'answer' for an input of 5 started with a zero. (Plus I had fun golfing it a bit.) Modified:

    use bignum qw/a 400/;$n=shift||10;$w+=1/($_/1)->bfac for 0..125;$w ="$w";sub P{$a=$_;($a%$_)||return for 2,3..sqrt;1}for$o(2..length( $w)-$n){$_=substr $w,$o,$n;next if/^0/||!P;print"$_ \@ $o\n";last}