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

Re: (Golf) Kaprekar's Process

by ZZamboni (Curate)
on Jun 16, 2001 at 23:08 UTC ( #89083=note: print w/ replies, xml ) Need Help??


in reply to (Golf) Kaprekar's Process

Here's my take, a non-recursive version, at 66 characters:

sub a { $_=shift;do{$_=join'',sort/./g;$i++}while($_=(reverse)-$_)!=495;$i #23456789012345678901234567890123456789012345678901234567890123456 }
Update #1: Ah, by borrowing an idea from MeowChow's solution, I can shave off one character :-)
sub a { $_=shift;do{$_=join'',sort/./g;$_=(reverse)-$_;$i++}until/495/;$i #2345678901234567890123456789012345678901234567890123456789012345 }
Update #2: Changed shift to pop, another 2 characters off. Thanks srawls!
sub a { $_=pop;do{$_=join'',sort/./g;$_=(reverse)-$_;$i++}until/495/;$i #23456789012345678901234567890123456789012345678901234567890123 }

--ZZamboni


Comment on Re: (Golf) Kaprekar's Process
Select or Download Code
Re: Re: (Golf) Kaprekar's Process
by srawls (Friar) on Jun 16, 2001 at 23:26 UTC
    ZZamboni: pop is shorter than shift; you can take two chars off of that.

    The 15 year old, freshman programmer,
    Stephen Rawls

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (15)
As of 2014-07-10 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (215 votes), past polls