Think about Loose Coupling PerlMonks

### Comment on

 Need Help??
We haven't had a golf in a while, so here it goes:

The problem is to write a perl golf (a solution in the fewest characters) to solve Kaprekar's Process. Kaprekar's Process basically states: "Take any number and arrange its digits in descending order and in ascending order and subtract. Repeat with the result. Ad Infinitum." Eventually, all 3 digit numbers (except when all digits are the same, like 333) will end up as 495. This is called Kaprekar's Constant. The program will return the number of steps it takes to get to 495.

Here is an example series:

```Input: 213
1: 321 - 123 = 198
2: 981 - 189 = 792
3: 972 - 279 = 693
4: 963 - 369 = 594
5: 954 - 459 = 495
```

Input:

Your subrutine will recieve a 3 digit number; you may assume it is positive and that all the digits are not the same (i.e. you do not have to worry about error handling).

Output:

The return value will merely be the number of steps it took to reach 495; in the example above, it was 5. Here are a few sample calls: (assume sub name is a, it really doesn't matter what you name yours though)

```print a(213) #prints 5
print a(258) #prints 2
```

Here is my attempt, weighing in at a bloated 60 chars:

```sub a {
return\$i if\$_[0]==495;
\$a=join'',sort{\$a<=>\$b}split//,pop;
\$i++;
a((reverse\$a)-\$a)
}
[download]```

P.S. If it is still unclear to you, visit this website and look for Kaprekar's Process on it.

The 15 year old, freshman programmer,
Stephen Rawls

In reply to (Golf) Kaprekar's Process by srawls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.
• Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
 [perldigious]: Quick question, is undef %hash a recommended/good way to delete an entire hash structure from memory? Or sould I be doing it some other way? [1nickt]: The best way is to scope it tightly so it just goes away when you are finished with it. [perldigious]: but... but... perldigious is the unvirtuous kind of lazy in this case. :-) [perldigious]: Just kidding. Thanks 1nickt, I'll go ahead and do it the right way. An extra set of brackets and a little extra indentation isn't too much to ask.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-07-21 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (335 votes). Check out past polls.