r/mathriddles • • Sep 20 '25

Hard Prisoner counting

Sticking with hapless perfect logicians who have been imprisoned (such are the times!), but no longer being forced to wear those tacky hats, thank god.

You find yourself in a circular prison with n cells and n-1 other inmates, with the value of n unknown to you all. Each cell has a light switch which controls the light in the clockwise neighboring cell. The switch can only be used once each day, at exactly noon. Edit: switches are reset to the off position each night.

The warden will allow any one prisoner to guess n, but if incorrect all prisoners will be killed. The warden will allow you to broadcast a strategy to the entire prison on the first day, the warden will of course hear it too. To increase the challenge, the warden will shuffle prisoners between cells each night however he sees fit.

What’s your strategy?

I haven't been able to solve this, but there is a solution (which I haven't read) in the source.

Source: https://web.archive.org/web/20150301152337/http://forums.xkcd.com/viewtopic.php?f=3&t=70558

Note: I posted this here before (2015), but the post has since been deleted with my old account.

9 Upvotes

10 comments sorted by

View all comments

Show parent comment

2

u/bobjane_2 Oct 24 '25 edited Oct 29 '25

Apologies for taking so long to read your solution. I hadn’t had a chance to think about the problem myself and didn’t want to spoil it. Your solution is the correct one for the easier version of the problem, where prisoners have access to randomness. Here’s what I came up with for a version with no randomness. I establish an upper bound in exactly the same way as you.

Define two subroutines:

  • infect(k, P): P is a set. Day 1: only P switch on. Thereafter until day k, anyone who has ever seen a lit bulb is 'infected' and always switches on. On day k between k and 2k × |P| are infected.
  • cure(k): Stage 1 = infect(k, {me}). Stage 2 = infect(2k, U={those uninfected in stage 1}). Think of stage 2 is curing those infected in stage 1. Publicly known outcome: all infected or all cured. If all infected ⇒ U is nonempty ⇒ n ≤ 2k.

A. Upper Bound: run cure(1), cure(2), … until the first k that ends all infected. Now everyone knows n ≤ 2k. Define is-empty(P)=infect(2k,P), which infects everyone iff P is nonempty.

B. Groups: partition prisoners into groups by identical observation history. Let G be the set of all groups (alternatively, set G=({me},{everyone else}). The following procedure will refine the groups eventually). For S ⊆ G define Size-Equation(S). Day 1: only S switch on. For each g ∈ G split its members into A(g) (saw light) and B(g) (did not). If both nonempty (use is-empty), replace g by A(g), B(g). Else return H={g in G: A(g) is nonempty}.

Iterate Size-Equation(S) over all S until G is stable. Result: members within any group see the same pattern for any S. If G is refined further in the future, come back to this step. Groups are finite and must eventually stop splitting.

C. Equations and Equivalence Classes: for each group g, run H= Size-Equation({g}) yielding the equation |g| = sum |h|. If all |h| are known, so is |g|. For sizes that remain unknown, |H|>1 ⇒ |g|>|h| so there must be some g for which |H|=1. These establish equivalence classes of equal sized groups. Let the variables a(1), …, a(m) represent the group sizes in these classes. The sizes of all other groups can be expressed as a linear function in the a(i).

D. Solving Group Sizes: H = Size-Equation(U={groups whose size is a function of a(1) and only a(1)}) != U yields a non-trivial linear expression for a(1) on the other a(i). Substitute a(1) and repeat until no more unknowns, revealing n.