p~c₃o.k×~t
Try it online!
Far too slow to run in a reasonable length of time (the Brachylog interpreter spends a long time doing multiplications on empty strings, 4-digit numbers, negative numbers etc. using a very slow constraint solver). The TIO link uses an input with only 3 digits (this program can handle input with any number of digits). This is a function whose input is a number containing all the digits required (e.g. 234567
) – the lack of duplicates in the input means that you can always just put any 0
at the end to avoid a leading zero – and whose output is a list in the order [b, a, c]
(e.g. [6, 57, 342]
).
Explanation
p~c₃o.k×~t
p Permute the digits of the input
~c₃ Split them into three groups
o Sort the three groups
. to produce the output, which must have the following property:
k all but the last group
× when multiplied together
~t produces the last group
So where did the requirement on the groups to be 2, 1, and 3 digits go? Well, we know there are 6 digits in the input, and the groups are in sorted orders. The only possible sizes they can have, therefore, are [1, 1, 4], [1, 2, 3], or [2, 2, 2]. The first case is impossible (you can't multiply two 1-digit numbers to produce a 4-digit number, as 9×9 is only 81), as is the last case (you can't multiply two 2-digit numbers to produce a 2-digit number, as even 10×10 produces 100). Thus, the return values [b, a, c]
must be 1, 2, and 3 digits long in that order, so a
is 2 digits, b
is 1 digit, and c
is 3 digits, as requested.
0,1,2,3,4,5
result in13,4,052
; no solution; or is any behaviour OK? \$\endgroup\$