# Reduce a number by its largest digit

Given an integer number in decimal number system, reduce it to a single decimal digit as follows:

1. Convert the number to a list of decimal digits.
2. Find the largest digit, D
3. 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.
4. Convert the resulting list to a decimal number and multiply it by D.
5. 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.
6. 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 , so the shortest answers in bytes in each language win.

• Which 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.
Nov 18, 2017 at 22:00
• @Adám By coding logics, should then 10 -> 10? Nov 19, 2017 at 0:16
• @Adám You are right, I should have written "bigger than 9". I'm going to edit the description. Thanks! Nov 19, 2017 at 7:37
• 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. Nov 19, 2017 at 9:54
• Also, a random number divisible by 4 has 3/5 probability of the product of the last two digits being divisible by 8. Nov 21, 2017 at 18:36

# dc, 98 85 bytes

?dsj[0dsosclj[soIlc^sr0]sn[I~dlo!>nrlc1+scd0<i]dsixljdlr%rlrI*/lr*+lo*dsj9<T]sT9<Tljp


Many thanks to this answer for the idea of utilizing ~ in the extraction of digits from a number, resulting in two saved bytes over the original version of the code.

This was a rather though one to complete in dc with its nonexistent string manipulation capabilities.

Try it online!

# Bash, 80 bytes

Uses packages Core Utilities (for sort and tail) and grep.

while((n>9));do m=$(grep -o .<<<$n|sort|tail -n1);n=$((${n/$m/}*m));done;echo$n


How does it work?

while (( n > 9 )); do  # C-style loop conditional
grep -o .          # Separate the string into one char per line
<<< $n # Use the content of variable n as its stdin | sort # Pipe to sort, which sorts strings by line | tail -n 1 # Take the last line m=$(                 ) # Assign the output of the command chain to m
n=$(( )) # Assign the result of the evaluation to n${n/$m/} # Replace the first occurrence of$m with empty
*m        # ... and multiply it by the value of m
done; echo $n # After loop, print the value of n  • Use $[${n/$m/}*m] instead of $((${n/$m/}*m)) for -2 bytes – Tino Sep 8, 2021 at 15:36 • Code fails on 91273716. Fix: Use $[10#${n/$m/}*m], adds +3 bytes to 81.
– Tino
Sep 8, 2021 at 15:55
• And if you replace \$(..) with backticks (I do not know how to present a backtick here in comments, sorry), that brings us back to 80 bytes again ;)
– Tino
Sep 8, 2021 at 17:08

# APL NARS 4240 36 chars

{⍵≤9:⍵⋄∇h⌷v×10⊥v[(⍳⍴v)∼h←1⌷⍒v←⍎¨⍕⍵]}


some test, I copied the trick ⍎¨⍕ from Adam solution

g←{⍵≤9:⍵⋄∇h⌷v×10⊥v[(⍳⍴v)∼h←1⌷⍒v←⍎¨⍕⍵]}

g¨9 27 757 1234 26364 432969 1234584 91273716
9 4 5 8 8 0 8 6

{⍵≤9:⍵   if ⍵≤9 return ⍵
∇h⌷v×10⊥v[(⍳⍴v)∼h←1⌷⍒v←⍎¨⍕⍵]
v←⍎¨⍕⍵] convert ⍵ in the list of integer digits (not chars) in v
h←1⌷⍒v       puts in h the index of the first element max that appear in v
v[(⍳⍴v)∼h           gets from v only the indices for digits different from h
10⊥                   convert that modify v[]sub array to a number in base 10
∇h⌷v×            multiply it with v[h], and recall the same function for this number
}


# Kotlin, 109 bytes

fun f(n:Int):Int{return if(n>9){val m=(""+n).max()!!;f((""+n).replaceFirst(""+m,"").toInt()*(m-'0'))}else n}