There's more than one way to do things PerlMonks

### Re: Re: Re: (Golf) Kaprekar's Process

by srawls (Friar)
 on Jun 16, 2001 at 23:41 UTC ( #89090=note: print w/replies, xml ) Need Help??

in reply to Re: Re: (Golf) Kaprekar's Process

Oh, I see: 100 - 001 is 99; 99 - 99 is zero. Now we've got an infinite loop. Just out of curiosity, how would you solve that? Your solution gives 6 (which is most likely right), I just don't see how.

The 15 year old, freshman programmer,
Stephen Rawls

Replies are listed 'Best First'.
Re: Re: Re: Re: (Golf) Kaprekar's Process
by MeowChow (Vicar) on Jun 17, 2001 at 00:11 UTC
That's what the sprintf is there for. It formats a two-digit result into a three-digit string, eg. 099. After sorting and reversal, you get 990 - 099.
```   MeowChow
s aamecha.s a..a\u\$&owag.print```
Ok, I get it. I can modify yours than, and improve it by two chars:
```sub a {
\$_=pop;s/^..\$/0\$&/;/495/?0:1+a(-(\$_=join'',sort/./g)+reverse)
}

The 15 year old, freshman programmer,
Stephen Rawls

Create A New User
Node Status?
node history
Node Type: note [id://89090]
help
Chatterbox?
 [shmem]: moin Corion, karlgoethebier. I stumbled over <<>> occasionally on the p5p mailing list [Discipulus]: not noticed [karlgoethebier]: sometimes rereading manages is helpful ;-) at least for me... [Eily]: hello people \o/

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-07-21 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (319 votes). Check out past polls.