[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Fun With Golf



perlgolf hole - Digital Root

Write a subroutine to compute the digital root of any whole number argument.

Winner: sub hole {$_=pop;1while s/(\d)(\d)/$1+$2/ge;$_}


Can't this function can be triviality optimized of three whole characters
as

	sub hole {$_=pop;1while s/(.)(.)/$1+$2/e;$_}

?

(we while the s///, so we can away with the g, and the statement 
says the argument is a number, do we can switch \d by .)

A slightly longer solution, but so ugly I couldn't resist:

	sub hole {$"='+'while($_=pop||eval"@{[/./g]}")>9;$_}

And one that is slightly shorter:

	sub hole {pop=~/./;$'?hole($&+$'):$&}

Joy,
Yanick

--
($_,$y)=("Yhre lo  .kePnarhtretcae\n",   '(.) (.)'  );
$y=~s/\(/(./gwhile s/$y/$2$1/xg;print;       @      !; 
                                     "     `---'    ";

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe