Library · The Fiddler: Solutions · Chapter 141
Can You Figure the Factorial Numbers?
In the factorial number system, the th digit from the right has base : it can be anything from to , and its place value is . So the rightmost digit is always , the next is or , the next , or , and so on. For example, in this system is What is the smallest number that is divisible by every whole number from to and whose factorial-system digits include each of (repeats allowed)?
The Fiddler, Zach Wissner-Gross, October 6, 2023(original post)
The official solution appeared in the post of October 13, 2023 and gives (that is, ) for the main puzzle and for the Extra Credit. Both agree with the answers below.
Solution
Two facts about this number system do all the work. First, every number has exactly one representation, just as in ordinary bases: the digits are found by dividing by in turn and keeping the remainders. Second, the rightmost digits on their own can be worth anything from to (the largest is ), and every digit further left has a place value that is a multiple of . So the rightmost digits are exactly the remainder of the number on division by .
Divisible by every number from to means divisible by their least common multiple, . Since divides , divisibility by depends only on the remainder on division by , which is the last five digits. That remainder must be a multiple of below , so it is or , and the last five digits are
The number must also show the digits , and with the ending it already shows and . Those four other digits need four more places, so the number has at least nine digits. With the ending it would need five more places for , making ten digits. A ten-digit number is at least , larger than every nine-digit number, so the ending is .
With nine digits, the four leading places, of bases , hold in some order, and every digit is allowed in each of them. The number is smallest with the smallest digit in front, so the order is , and its value is :
The computation
The check is blunt: count upwards from , convert each number to the factorial system, and stop at the first that is divisible by to and shows all of the digits to .
from itertools import permutations
from math import factorial
def fact_digits(n):
"""Digits of n in the factorial number system, most
significant first (the place of base b holds 0..b-1)."""
digits, base = [], 1
while True:
n, d = divmod(n, base)
digits.append(d)
base += 1
if n == 0:
return digits[::-1]
def show(n):
return "".join(map(str, fact_digits(n)))
n = 1
while not (all(n % k == 0 for k in range(1, 6))
and set(range(6)) <= set(fact_digits(n))):
n += 1
print("main:", show(n), "=", n)
It prints
main: 134522000 = 58980
Extra Credit
What is the smallest number, written in the factorial system, that contains each of the digits to exactly once (and any number of zeros) and is divisible by every whole number from to ?
Solution
The same idea, one size up. The least common multiple of is , which divides . So divisibility depends only on the last seven digits, which must be worth or . In the factorial system , so the last seven digits are The second repeats the digit , which is not allowed. So the last seven digits are zeros, and the nine digits to all sit in places of base or more.
That needs at least nine such places, the places of bases to , making sixteen digits. A longer number would be larger, so the answer fills exactly these nine places, with each digit used once. The leading place matters most, so make it as small as possible, then the next, and so on, while keeping the rest possible. The only obstacles are the top digits: needs a place of base at least , and a place of base at least .
The places of bases down to take : each is the smallest digit left, and the digits still to come, , still have room in the bases .
The place of base is the last that can hold , so it must take .
The place of base could take or , but the base- place below it can hold at most . So base takes and base takes .
It is about trillion in ordinary numbers.
The computation
Counting upwards would take too long here, so the program splits the search along the place values, using the conversion function from The computation above. It first confirms that is divisible by every number from to , so that only the last seven digits matter, and lists every possible last-seven-digit block, from to , that is itself divisible by all of them. Then it tries all ways to put the digits to into the places of bases to , keeps those that respect each place’s limit, and takes the smallest.
# Places from 7! upward are multiples of 1..9, so divisibility
# depends only on the last seven digits (worth 0 to 7!-1).
print("7! divisible by 1..9:",
all(factorial(7) % k == 0 for k in range(1, 10)))
ends = [b for b in range(factorial(7))
if all(b % k == 0 for k in range(1, 10))]
print("possible last seven digits:",
[show(b).zfill(7) for b in ends])
# 3300000 repeats a 3, so the last seven are zeros and 1..9
# fill the nine places of bases 8 to 16 (fewer cannot hold
# them; more makes a longer, larger number).
best = min(sum(d * factorial(b - 1) for d, b in zip(p, bases))
for bases in [range(16, 7, -1)]
for p in permutations(range(1, 10))
if all(d < b for d, b in zip(p, bases)))
print("extra credit:", show(best))
It prints
7! divisible by 1..9: True
possible last seven digits: ['0000000', '3300000']
extra credit: 1234569870000000