Skip to content
Vamshi Jandhyala

Books · The Riddler

Chapter 182

Pirates, Monkeys, and Coconuts

Riddler Express

If A,B,C,D,EA, B, C, D, E are all unique digits, what values would work with the following equation? ABC,CDE×4  =  EDC,CBA.\overline{A B C, C D E} \times 4 \;=\; \overline{E D C, C B A}.

The Riddler, FiveThirtyEight, May 4, 2018(original post)

Solution

Pin AA. Both sides are six-digit numbers, so A1A \ge 1 (no leading zero) and the product is at most 999,999999{,}999, forcing ABCCDE249,999\overline{ABCCDE} \le 249{,}999, hence A2A \le 2. The rightmost digit of 4ABCCDE4 \cdot \overline{ABCCDE} equals the rightmost digit of 4E4 E, which is AA. Since 4E4 E is even and A0A \ne 0, AA is an even positive digit at most 22. Therefore A=2A = 2.

Pin EE. The right-hand side starts with EE and equals 42BCCDE4 \cdot \overline{2BCCDE}. The first digit of 42BCCDE4 \cdot \overline{2BCCDE} is either 88 or 99, so E{8,9}E \in \{8, 9\}. The ones-digit constraint is 4E2(mod10)4 E \equiv 2 \pmod{10}, which forces E{3,8}E \in \{3, 8\}. The intersection is E=8E = 8.

Pin BB. With A=2A = 2 and E=8E = 8, the ones place of the left-hand side gives 48=324 \cdot 8 = 32: 22 is the ones of the right, and 33 carries into the tens place. The leftmost two digits of the right-hand side are 8D8 D and the leftmost of the left-hand side multiplied by 44 must produce them. In particular 4B+(carry)=D4 B + \text{(carry)} = D in the ten-thousands place; the carry in is at most 33 and DD is a single digit, so 4B94 B \le 9, giving B{0,1,2}B \in \{0, 1, 2\}. Since A=2A = 2, B{0,1}B \in \{0, 1\}. Looking at the ten-thousands place: 4B+(carry from C4)1(mod10)4 B + (\text{carry from } C \cdot 4) \equiv 1 \pmod{10} (because the right-hand side’s ten-thousands digit is B=?B = ?; actually it is BB, so EDC,CBA\overline{ED C, C B A} has BB in the tens place). Walking the multiplication right to left, the second digit from the right of the product is determined by 4D+34 D + 3 (carry from 4E=324 E = 32). This must equal BB modulo 1010. If B=1B = 1, then 4D+31(mod10)4 D + 3 \equiv 1 \pmod{10}, so 4D84 D \equiv 8, so D{2,7}D \in \{2, 7\}; D=2D = 2 is taken by AA, so D=7D = 7. If B=0B = 0, then 4D+30(mod10)4 D + 3 \equiv 0 \pmod{10}, so 4D7(mod10)4 D \equiv 7 \pmod{10}, impossible since 4D4 D is even. Hence B=1B = 1 and D=7D = 7.

Pin CC. With (A,B,D,E)=(2,1,7,8)(A, B, D, E) = (2, 1, 7, 8) the remaining unknown is CC. The number 21C,C78\overline{2 1 C, C 7 8} multiplied by 44 must equal 87C,C12\overline{8 7 C, C 1 2}. The hundreds digit on the right is CC, generated from 4C+(carry from 47+3)=4C+31/104 C + (\text{carry from } 4 \cdot 7 + 3) = 4 C + 31 / 10, i.e. carry 33. So 4C+3C(mod10)4 C + 3 \equiv C \pmod{10}, i.e. 3C3(mod10)3 C \equiv -3 \pmod{10}, i.e. C9(mod10)C \equiv 9 \pmod{10}. Hence C=9C = 9.

Check. The candidate is (A,B,C,D,E)=(2,1,9,7,8)(A, B, C, D, E) = (2, 1, 9, 7, 8): 219,978×4  =  879,912.219{,}978 \times 4 \;=\; 879{,}912. \quad\checkmark All five digits are distinct, and the equation holds. So A=2, B=1, C=9, D=7, E=8,and 219,978×4=879,912.\boxed{\,\begin{aligned} & A = 2,\ B = 1,\ C = 9,\ D = 7,\ E = 8, \\ & \text{and}\ 219{,}978 \times 4 = 879{,}912. \end{aligned}\,}

The computation

Sweep every assignment of five distinct digits to (A,B,C,D,E)(A, B, C, D, E), evaluate both sides of the equation, and report those that satisfy it.

  1. Loop over A{1,,9}A \in \{1, \ldots, 9\} and B,C,D{0,,9}B, C, D \in \{0, \ldots, 9\} and E{1,,9}E \in \{1, \ldots, 9\} (both leading digits non-zero).

  2. Skip assignments where any two letters coincide.

  3. Test 4ABCCDE=EDCCBA4 \cdot \overline{A B C C D E} = \overline{E D C C B A} 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, (2,1,9,7,8,219978,879912)(2, 1, 9, 7, 8, 219978, 879912), matching the boxed assignment.

Riddler Classic

Seven pirates wash ashore on a deserted island and gather NN 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 NN 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 MM coconuts in the pile, tosses one to the monkey, and hides one-seventh of the remainder, the pile shrinks to f(M)  =  67(M1).f(M) \;=\; \tfrac{6}{7}(M - 1). For this to be a whole number, M1M - 1 must be divisible by 77, i.e. M1(mod7)M \equiv 1 \pmod{7}.

Seven pirates and a morning split. Starting with NN, after the seven night visits the pile has size f7(N)f^{7}(N). The morning split into seven equal heaps requires f7(N)  =  7kfor some non-negative integer k.f^{7}(N) \;=\; 7 k \qquad\text{for some non-negative integer } k.

Unwind the recurrence. Write ui=fi(N)u_{i} = f^{i}(N) with u0=Nu_{0} = N. The recurrence is ui+1=67(ui1)u_{i+1} = \tfrac{6}{7}(u_{i} - 1), i.e. 7ui+1=6ui67 u_{i+1} = 6 u_{i} - 6, i.e. ui+1+6=67(ui+6)u_{i+1} + 6 = \tfrac{6}{7}(u_{i} + 6). So the sequence ui+6u_{i} + 6 is geometric with ratio 6/76/7, giving u7+6  =  (67)7(N+6)  =  6777(N+6)  =  279,936823,543(N+6).u_{7} + 6 \;=\; \left(\tfrac{6}{7}\right)^{7} (N + 6) \;=\; \frac{6^{7}}{7^{7}}(N + 6) \;=\; \frac{279{,}936}{823{,}543}(N + 6). Setting u7=7ku_{7} = 7 k and solving for NN, N  =  77(7k+6)66767  =  5,764,801k+3,261,642279,936.N \;=\; \frac{7^{7}(7 k + 6) - 6 \cdot 6^{7}}{6^{7}} \;=\; \frac{5{,}764{,}801 \, k + 3{,}261{,}642}{279{,}936}. The denominator factors as 279,936=67=2737279{,}936 = 6^{7} = 2^{7} \cdot 3^{7}, and the numerator’s leading term has 777^{7} coprime to 66, so NN is an integer iff 279,9365,764,801k+3,261,642279{,}936 \mid 5{,}764{,}801 \, k + 3{,}261{,}642.

Solve the congruence. Reduce: 5,764,80177(mod67)5{,}764{,}801 \equiv 7^{7} \pmod{6^{7}}. The inverse of 77 modulo 66 is 11 (since 717 \equiv 1), and by lifting modulo 272^{7} and 373^{7} separately the inverse of 77 modulo 676^{7} is some specific residue α\alpha, giving k3,261,642α7(mod67)k \equiv -3{,}261{,}642 \cdot \alpha^{7} \pmod{6^{7}}. The smallest non-negative solution is k=39,990k = 39{,}990. Plugging back, N  =  5,764,801×39,990+3,261,642279,936  =  230,549,293,632279,936  =  823,537.\begin{aligned} N &\;=\; \frac{5{,}764{,}801 \times 39{,}990 + 3{,}261{,}642}{279{,}936} \\ &\;=\; \frac{230{,}549{,}293{,}632}{279{,}936} \;=\; 823{,}537. \end{aligned} So Nmin  =  823,537.\boxed{\,N_{\min} \;=\; 823{,}537.\,}

Sanity check on the sequence. Apply ff seven times to 823,537823{,}537: 823,537705,888605,046518,610444,522381,018326,586279,930.\begin{align*} 823{,}537 \to 705{,}888 \to 605{,}046 \to 518{,}610 \to 444{,}522 \\ \to 381{,}018 \to 326{,}586 \to 279{,}930. \end{align*} Each step subtracts one for the monkey, divides by seven exactly, and keeps six sevenths, and the final pile 279,930=739,990279{,}930 = 7 \cdot 39{,}990 splits cleanly into seven morning shares of 39,99039{,}990 coconuts each.

The computation

Search the smallest NN that supports seven successive integer-divisible night visits and a morning seven-way split.

  1. For NN from small upward, simulate ff seven times, abandoning early if any intermediate M1M - 1 fails to be divisible by 77 or any intermediate MM goes non-positive.

  2. Accept the first NN whose final pile is positive and divisible by 77.

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 N=823,537N = 823{,}537, final pile 279,930279{,}930, morning share 39,99039{,}990 per pirate, matching the boxed answer.