I wanted to ask this question: Convert to and from the factorial number system but I'm a couple of years too late!
So, instead you must convert to and from the lairotcaf backwards-factorial number base! The way it works is that the first digit is always 0
, the maximum value of the next digit is 1
, then 2
and so on. If a number has n digits, then each place value is given by n!/i! where i is the digit position starting at 1
on the left. The highest digit position is on the right. This means that each length uses different place values!
There is a unique shortest way to represent each number, which is what you must output for a decimal number input. If an input number starts with a 0
, convert it to decimal. If it starts with any other digit, convert it to the backwards-factorial number base.
0
is both valid and the same number in both bases. In the test data no input value will use, or produce a number with a digit higher than 9
. Note that the shortest representation of some numbers starts with multiple 0
s.
Digit position: 1 2 3 4 5 6
Place value: 720 360 120 30 6 1
0 0 2 2 3 3
0 0 240+ 60+ 18+ 3=321
This is the shortest way to represent 321
because removing a digit makes the highest number possible only 119
(5! - 1).
Test data:
Input: Output:
0 0
1 01
002 2
3 010
011 4
5 012
0012 6
21 0121
0130 24
01000 60
01234 119
001000 120
321 002233
011235 563
0010330 987
4321 0120252
54321 000325536
654321 0010135621
0112351713 2838883
7654321 00102603574
This is code-golf so the fewest bytes wins!