Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: e

by I0 (Priest)
on Jul 17, 2004 at 06:04 UTC ( #375201=note: print w/ replies, xml ) Need Help??


in reply to e

sub e{ use integer; my @e=(1) x shift; for(1..@e*log(@e)/3){ for(reverse(1..$#e)){ $e[$_-1]+=$e[$_]/$_; $e[$_]%=$_; } printf"%01d",$e[0]; $e[0]=0; $_*=10 for @e; } }


Comment on Re: e
Download Code
Re^2: e
by tachyon (Chancellor) on Jul 17, 2004 at 07:28 UTC

    Cool. Lovely piece of code.

    Map gets it down to 105 chars - yes void maps are good for golf only!

    sub e{ #234567890123456789012345678901234567890123456789012 @e=(1)x pop;map{map{$e[$_-1]+=$e[$_]/$_;$e[$_]=10*($ e[$_]%$_)}reverse 1..$#e;print int$e[0];$e[0]=0}1..@e } # or if you prefer whitespace and no void maps sub e{ @e=(1)x pop; for(1..@e){ for(reverse(1..$#e)){ $e[$_-1]+=$e[$_]/$_; $e[$_]=10*($e[$_]%$_) } print int $e[0]; $e[0]=0 } }

    cheers

    tachyon

    Re^3: e
    by mtve (Chaplain) on Jul 17, 2004 at 09:36 UTC

      Simple optimizations down to 86:

      sub e{ #234567890123456789012345678901234567890123456789012 map{$e[$q-1]+=$e[$q]/$q,($e[$q]%=$q--).=0for 1..($q=@e);$e[0]=!print"@e"|0}@e=(1)x pop } e(1000);

      Update from tybalt89 - 80 chars:

      $=[!print$=]=!map{$==$=[$#z]+=$=[@z]/@z,($=[@z]%=1+$#z--).=0}@z=@=for@ +==(1)x pop

        There was a bug in the code I posted. AFAIK it does not effect the result per se just the efficiency. By trying to scrape a char by using @e instead of $#e (as used in IOs code) within the inner loop we are accessing a non existent element one past the end of the array. As a result @e is growing by one with each inner loop.

        cheers

        tachyon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (16)
As of 2015-07-02 21:32 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 (45 votes), past polls