dc, 40 bytes
Sad to see dc
so underrepresented here.
This is essentially my first code golf post, so if I've misjudged how bytes should be counted, go easy on me! :)
?[1+]sc[10~rdZ1<a]dsax1[rd0=c*z1<b]dsbxp
You can run this in the terminal like so:
$ echo 361218402 | dc -e '?[1+]sc[10~rdZ1<a]dsax1[rd0=c*z1<b]dsbxp'
2304
Or you can try it online!
Explanation (probably too verbose):
? # Read and execute a line from standard input
# (A number just goes on the stack)
[ # Begin a literal string (ended by a matched ']')
1 # Push 1 onto the stack (when this string is executed as a macro)
+ # Pop top two numbers, add them, push result
] # End literal string
s # Pop top of stack (the string) and store it in...
c # ...register c
[
10 # Push 10 (duh)
~ # Pop top two numbers, divide second popped by first popped, push quotient and then remainder
r # Reverse top two numbers of stack
d # Duplicate top of stack (pop and then push it twice)
Z # Pop top of stack and push its length in digits
1
< # Pop two numbers and compare; if first popped is less than second then...
a # ...run macro stored in register a (a recursive call, here)
] # End string
d # Duplicate top of stack
s # Store in...
a # ...register a
x # Execute the top of stack!
# At this point the stack will consist of one digit entries, the digits of input
1
[
r # Reverse
d # Duplicate
0
= # If top two numbers are equal then...
c # ...run macro in register c
* # Pop two, multiply, push product
z # Push "height" of stack
1
< # If height of stack is greater than 1 then...
b # ...run macro b
]
d # Duplicate the string on the stack
sb # Store it as macro b
x # Execute!
p # Print top of stack
100000000000
I suggest the test case99999999999 => 31381059609
, since it doesn't fit in a default 32-bit integer. Perhaps better would be to lower the maximum output to a 32-bit maximum (2147483647). \$\endgroup\$