Chapter 182
Pirates, Monkeys, and Coconuts
Riddler Express
If are all unique digits, what values would work with the following equation?
The Riddler, FiveThirtyEight, May 4, 2018(original post)
Solution
Pin . Both sides are six-digit numbers, so (no leading zero) and the product is at most , forcing , hence . The rightmost digit of equals the rightmost digit of , which is . Since is even and , is an even positive digit at most . Therefore .
Pin . The right-hand side starts with and equals . The first digit of is either or , so . The ones-digit constraint is , which forces . The intersection is .
Pin . With and , the ones place of the left-hand side gives : is the ones of the right, and carries into the tens place. The leftmost two digits of the right-hand side are and the leftmost of the left-hand side multiplied by must produce them. In particular in the ten-thousands place; the carry in is at most and is a single digit, so , giving . Since , . Looking at the ten-thousands place: (because the right-hand side’s ten-thousands digit is ; actually it is , so has in the tens place). Walking the multiplication right to left, the second digit from the right of the product is determined by (carry from ). This must equal modulo . If , then , so , so ; is taken by , so . If , then , so , impossible since is even. Hence and .
Pin . With the remaining unknown is . The number multiplied by must equal . The hundreds digit on the right is , generated from , i.e. carry . So , i.e. , i.e. . Hence .
Check. The candidate is : All five digits are distinct, and the equation holds. So
The computation
Sweep every assignment of five distinct digits to , evaluate both sides of the equation, and report those that satisfy it.
Loop over and and (both leading digits non-zero).
Skip assignments where any two letters coincide.
Test and print every solution.
solutions = []
for a in range(1, 10):
for b in range(10):
if b == a: continue
for c in range(10):
if c in (a, b): continue
for d in range(10):
if d in (a, b, c): continue
for e in range(1, 10):
if e in (a, b, c, d): continue
L = a*100000 + b*10000 + c*1100 + d*10 + e
R = e*100000 + d*10000 + c*1100 + b*10 + a
if 4 * L == R:
solutions.append((a, b, c, d, e, L, R))
for s in solutions:
print(s)
The sweep prints exactly one tuple, , matching the boxed assignment.
Riddler Classic
Seven pirates wash ashore on a deserted island and gather coconuts into a single pile. During the night each pirate, one after another, wakes up, tosses one coconut to a watching monkey, divides the remaining pile into seven equal heaps, hides one heap for themselves, and pushes the other six heaps back into a single pile before going back to sleep. In the morning the seven pirates divide the remaining pile into seven equal heaps (the monkey gets nothing this time). Coconuts are indivisible. What is the smallest for which this is possible?
The Riddler, FiveThirtyEight, May 4, 2018(original post)
Solution
One pirate’s effect on the pile. If a pirate finds coconuts in the pile, tosses one to the monkey, and hides one-seventh of the remainder, the pile shrinks to For this to be a whole number, must be divisible by , i.e. .
Seven pirates and a morning split. Starting with , after the seven night visits the pile has size . The morning split into seven equal heaps requires
Unwind the recurrence. Write with . The recurrence is , i.e. , i.e. . So the sequence is geometric with ratio , giving Setting and solving for , The denominator factors as , and the numerator’s leading term has coprime to , so is an integer iff .
Solve the congruence. Reduce: . The inverse of modulo is (since ), and by lifting modulo and separately the inverse of modulo is some specific residue , giving . The smallest non-negative solution is . Plugging back, So
Sanity check on the sequence. Apply seven times to : Each step subtracts one for the monkey, divides by seven exactly, and keeps six sevenths, and the final pile splits cleanly into seven morning shares of coconuts each.
The computation
Search the smallest that supports seven successive integer-divisible night visits and a morning seven-way split.
For from small upward, simulate seven times, abandoning early if any intermediate fails to be divisible by or any intermediate goes non-positive.
Accept the first whose final pile is positive and divisible by .
def smallest_N():
for N in range(7, 5_000_000):
M = N; ok = True
for _ in range(7):
if (M - 1) % 7 != 0:
ok = False; break
M = 6 * (M - 1) // 7
if M <= 0:
ok = False; break
if ok and M > 0 and M % 7 == 0:
return N, M, M // 7
return None
N, final, share = smallest_N()
print(f"smallest N = {N}, final pile = {final}, morning share each = {share}")
The search prints , final pile , morning share per pirate, matching the boxed answer.