Why is the number 6174 so interesting? As defined by Wikipedia
- Take any four-digit number, using at least two different digits. (Leading zeros are allowed.)
- Arrange the digits in ascending and then in descending order to get two four-digit numbers, adding leading zeros if necessary.
- Subtract the smaller number from the bigger number.
- Go back to step 2.
The above process, known as Kaprekar's routine, will always reach 6174 in at most 7 iterations. Once 6174 is reached, the process will continue yielding it.
Write a program which runs the Kaprekar's routine against a given four-digit number (see definition above) printing out each step of the routine.
Rules:
- Submissions must be complete programs.
- Input must be read from standard input. Piping from echo is OK.
- Input should be in numeric form.
- Printing out leading zeros is required. (See examples below.)
- Last line should say how many iterations were needed. Punctuation is required.
Examples:
> 2607
7620 - 0267 = 7353
7533 - 3357 = 4176
7641 - 1467 = 6174
Iterations: 3.
> 1211
2111 - 1112 = 0999
9990 - 0999 = 8991
9981 - 1899 = 8082
8820 - 0288 = 8532
8532 - 2358 = 6174
Iterations: 5.
> 6174
7641 - 1467 = 6174
Iterations: 1.
Any programming language is welcome. Extra points for esoteric ones + a small bounty.
Update 1: There is already a similar question.
Update 2: Added example for 6174 as input. Thanks to Peter Taylor for the notice.