24
2
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.
This is the exact same question I had to do in the NCSS challenge. If you're doing this to get other people to give you the answer in the NCSS challenge, then that's pretty bad and it defeats the purpose of a challenge. If you're not, then I apolagize. – None – 2012-10-17T04:19:27.970
This question was posted in the middle of last year - I very much doubt anyone was trying to cheat for some test or challenge. – Gareth – 2012-10-20T08:38:26.210
This is news for me. Someone call a moderator... – None – 2011-06-06T21:44:02.463
Uh... isn't there a "migrate" button? – HostileFork says dont trust SE – 2011-06-06T22:20:24.207
I've flagged this for a moderator to migrate. May I suggest altering the rules on input output to agree with the earlier 3-digit version? And linking to the earlier version in the body of the question. – dmckee --- ex-moderator kitten – 2011-06-07T01:12:44.077
@dmckee I didn't know about this site and could not know that there is already a similar question (on stackoverflow there was none). However I would hesitate altering the rules to agree with the 3-digit version and thus making the two questions even more similar. It is pointless to post a duplicate or a slight variation of an existing question. Even when done unintentional. – lunohodov – 2011-06-08T08:47:09.807
@lunohodov: Well, it's your question. I think that despite the surface similarities the questions are different in an interesting way because the particular trick used by several posters in the earlier one (independence of the series from the middle sized digit) does not apply here. – dmckee --- ex-moderator kitten – 2011-06-08T12:47:20.520
3Please add 6174 as an example so we can see how the output should be formatted. – Peter Taylor – 2011-06-10T06:26:26.827
I took the liberty of fixing your test cases. Given that I always copy the test cases from the original question to test against (Yes, I write a script for that nearly every time, unless the author provides one), it isn't very nice of you to (a) have a wrong digit in the first one and (b) using U+2013 instead of U+002D as the minus. I just sat there wondering why my program fails, yet yields apparently correct output. – Joey – 2011-06-10T09:43:55.993
@Joey Bad me. Thank you. – lunohodov – 2011-06-10T14:34:04.657
@lunohodov If you already have 6174 as input, do you really need one iteration to reach 6174? I only ask because I'll have to write a special case in my program if you do. :-( – Gareth – 2011-06-10T15:31:21.043
@Gareth Yes, as the task is, as defined above, to run the Kaprekar's routine against a given four-digit number and 6174 should be treated as such. – lunohodov – 2011-06-12T10:15:18.803