Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

e with continued fractions

by jdalbec (Deacon)
on Jul 17, 2004 at 20:13 UTC ( [id://375278]=obfuscated: print w/replies, xml ) Need Help??

$|--; my ($a, $b, $c, $d, $m, $n) = (0,1,1,0,0,0); while() { if($b && $d && (my $q=int($a/$b))==int($c/$d)) { $a = 10*($a-$b*$q); $c = 10*($c-$d*$q); print $q; } else { $q = $a; $a = $c; $c = $c * (($m % 3) % 2 ? $n * 2 : 1) + $q; $q = $b; $b = $d; $d = $d * (($m++ % 3) % 2 ? $n++ * 2 : 1) + $q; } } # Hit control-C when it stops printing # due to numeric overflow

Replies are listed 'Best First'.
Re: e with continued fractions
by tachyon (Chancellor) on Jul 18, 2004 at 05:05 UTC

    Diabolical. You can make the ASCII quite pretty too:

    ;;$|++;use=> bigint;my($a,$b,$c,$d ,$m,$n) =(0,1,1 ,0,0,0) ;while (){if( $b&&$d &&(my$ e=int( $a/$b))==int($c/$d)){$a=10*($a-$b*$ e);$c=10*($c-$d*$e);print$e;}else{$ e=$a;$ a=$c;$ c=$c*( ($m%3) %2?$n* 2:1) +$e;$e=$ b;$b=$ d;$d=$d*(($m++%3)%2?$n++ *2:1)+$e;}};#e

    cheers

    tachyon

      On Perl 5.8.1 (Mac OS X 10.3.4) this doesn't seem to use bigint (as you apparently intended) unless I remove the =>.

      Update: OK, you fooled me. Good obfuscation, then!

        I use 5.6.x so don't actually have bigint. This is called obfuscation after all :-) The reason it does not work is that => is just a synonym for , in Perl so it reads use, bigint Or it should. Actually that , syntax parses as an error, whereas => syntax is OK GOK?

        C:\>type test.pl use, strict C:\>perl -MO=Deparse test.pl syntax error at test.pl line 1, near "use," test.pl had compilation errors. C:\>type test.pl use => strict C:\>perl -MO=Deparse test.pl '???', '???'; test.pl syntax OK C:\>

        cheers

        tachyon

Re: e with continued fractions
by bageler (Hermit) on Jul 19, 2004 at 20:15 UTC
    if you use bigint pragma then you won't get a numeric overflow and it will continue running for much longer :)
Good to about 45 places; spigot algorithm
by tmoertel (Chaplain) on Jul 21, 2004 at 18:02 UTC
    The output of the obfuscated code seems to be good for about 45 places (at least on my box running Perl v5.8.3 built for i386-linux-thread-multi) before it differs from the reference values from the On-Line Encyclopedia of Integer Sequences.

    If you want more precision, you might be interested in Unbounded Spigot Algorithms for the Digits of π by Jeremy Gibbons. It's a fun read, and you can adapt the methods presented in his paper to computing e digit by digit.

      The inaccuracy appears to be the result of roundoff error; use bigint; or use Math::BigInt ':constant'; (as suggested by tachyon and bageler) fixes that also.

      Also, my algorithm is streaming in Gibbons' sense. It represents the continued fraction as a composition of linear fractional transformations.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://375278]
Approved by PERLscienceman
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found