Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

one-liner pi

by zeno (Friar)
on Feb 15, 2001 at 00:34 UTC ( #58445=obfuscated: print w/ replies, xml ) Need Help??

I always love implementing this (++ if you can tell where the algorithm comes from!)
for($|++,$_++,$a++;;$,+=$a/$_,$_++,$a*=-1,$_++){printf"%.16f\n",$,*4}
This version will run (incessantly) as a one-liner as well. -zeno

Update: As MrNobo1024 has answered correctly, this algorithm was discovered by Gregory and Leibniz.

Comment on one-liner pi
Download Code
Replies are listed 'Best First'.
Re: one-liner pi
by japhy (Canon) on Feb 15, 2001 at 00:43 UTC
      Hi japhy,
      Correct! I'm still looking for a little something more, though (I did ++ you). This algorithm historically is attributed to someone. Do you know who that someone is? (Hint: He's dead)
      -zeno
        Gregory and Leibniz both discovered that formula.
Re: one-liner pi
by life_monger (Hermit) on Feb 17, 2001 at 00:29 UTC

    TIMTOWTDI...

    I think I preserved the "spirit" of the algorithm :-)

    #!/usr/bin/perl -w use strict; {(redo)while(printf"%.16f\n",$,+=((-1/++$_/(($a||=1)*=($_-$_++)*-1)))* +4)}

Re: one-liner pi
by MrNobo1024 (Hermit) on Feb 15, 2001 at 03:24 UTC
    You should do a $|++ before the program starts, or it won't display anything until you exit :(
      Hi MrNobo1024,

      It didn't happen that way for me... I thought that print did line buffering if it was printing to the terminal, and block buffering when printing to a filehandle.

      However... it nicely obfuscates the code a little more if I do put it in! So I did. Thanks for the tip! ;)
      -zeno

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://58445]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2015-07-07 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls