Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Deriving pi and e

by liz (Monsignor)
on Oct 25, 2003 at 10:21 UTC ( [id://302046]=note: print w/replies, xml ) Need Help??


in reply to Deriving pi and e

Apart from pg's and Abigail-II's comments, I couldn't resist golfing these down to:
1while($x||=3)-($x-=sin$x/cos$x);print"$x\n"
and:
1while($x||=2)-($x-=((log($x)-1)*$x));print"$x\n"

I particularly like the fact that it only uses 1 variable ;-)

Liz

Update:
Golfed some more by losing the seperate initial assignement of $x

Replies are listed 'Best First'.
Re^2: Deriving pi and e
by jdalbec (Deacon) on Jun 26, 2004 at 04:38 UTC
    I think you've landed in a sand trap. When I run these in Perl 5.8.1 they perform one iteration and stop. The problem is that you're changing the value of $x in the while condition and the left-hand side of the subtraction can return either the new value or the old value. In my case it's returning the new value which causes the loop to terminate early.

    Update: These work for me. I've skipped printing the newline to lower the golf score by 6. I wasn't able to skip the initial assignment but that only saves 1 character anyway.

    $;=3;$_=$;while($;-=sin$;/cos$;)-$_;print $;=2;$_=$;while($;+=(1-log$;)*$;)-$_;print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://302046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-09-07 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.