33
4
Task:
Given an integer number in decimal number system, reduce it to a single decimal digit as follows:
- Convert the number to a list of decimal digits.
- Find the largest digit, D
- Remove D from the list. If there is more than one occurrence of D, choose the first from the left (at the most significant position), all others should remain intact.
- Convert the resulting list to a decimal number and multiply it by D.
- If the number is bigger than 9 (has more than 1 decimal digit), repeat the whole procedure, feeding the result into it. Stop when you get a single-digit result.
- Display the result.
Example:
26364 ->
1. 2 6 3 6 4
2. The largest digit is 6, so D=6
3. There are two occurrences or 6: at positions 1 and 3 (0-based). We remove the left one,
at position 1 and get the list 2 3 6 4
4. we convert the list 2 3 6 4 to 2364 and multiply it by D:
2364 * 6 = 14184
5. 14184 is greater than 9 so we repeat the procedure, feeding 14184 into it.
We continue by repeating the procedure for 14184 and so on and we go through the following intermediate results, finally reaching 8:
11312
3336
1998
1782
1376
952
468
368
288
224
88
64
24
8
So the result for 26364 is 8.
Input: An integer / a string representing an integer
Output: A single digit, the result of the reduction applied to the number.
Test cases:
9 -> 9
27 -> 4
757 -> 5
1234 -> 8
26364 -> 8
432969 -> 0
1234584 -> 8
91273716 -> 6
This is code-golf, so the shortest answers in bytes in each language win.
3Which is it If the number is bigger than 10 or has more than 1 decimal digit. The number 10 has more than 1 decimal digit, but it isn't bigger than ten. – Adám – 2017-11-18T22:00:43.327
@Adám By coding logics, should then
10 -> 10
? – Ian H. – 2017-11-19T00:16:29.8571@Adám You are right, I should have written "bigger than 9". I'm going to edit the description. Thanks! – Galen Ivanov – 2017-11-19T07:37:56.470
Has someone examined the histogram of this function for sufficiently large regions? It seems to have a lot of zeroes; I also got many 8s while composing the test cases. – Galen Ivanov – 2017-11-19T09:54:07.730
Being divisible by 2 or 5 is preserved once it happens, so it's no wonder you easily get 0. That might also increase the chance of 8s. – Ørjan Johansen – 2017-11-19T17:54:48.030
2Also, a random number divisible by 4 has 3/5 probability of the product of the last two digits being divisible by 8. – Ørjan Johansen – 2017-11-21T18:36:15.067