Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Re: (Golf) Kaprekar's Process

by MeowChow (Vicar)
on Jun 16, 2001 at 23:33 UTC ( [id://89089] : note . print w/replies, xml ) Need Help??

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

... it handles 0s in numbers correctly
There are many such cases, though I misstated the problem somewhat. It isn't so much zeroed digits, as intermediate two-digit results. So far, all the solutions in this thread (except my own :) fail on many three-digit numbers.

Also, in Golf, it may be ok to violate strict, but your subroutine should be re-runnable (ie. $i should be reset somewhere).

               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Kaprekar's Process
by srawls (Friar) on Jun 16, 2001 at 23:41 UTC
    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

      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.
                     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