Library

Invariants

8 items in the Thuztra Learn library carry the topic invariants: 3 reference articles, 1 puzzle and 4 brainteasers. They are gathered here so one idea can be met in every form it takes, which is usually the difference between recognising a term and being able to use it.

5 of them have pages of their own, linked below. Everything else is listed with the summary its author wrote. The 5 problems on this topic are set out under their own headings, and the longest are quoted in full.

Problems in full

Quoted exactly as the library sets them, so the topic is something you can try rather than something you have read about.

The prisoners and the switch in the empty room

A hundred prisoners are told they will each, individually and in isolation, be led into a windowless room containing a single light switch. The switch is either up or down; nobody knows its starting position. The warden will call prisoners into the room one at a time, in an order she chooses and that can repeat any prisoner any number of times, but every prisoner is guaranteed to be called in eventually, and again and again, forever. Outside the room the prisoners cannot see or signal each other in any way.

At any point, any prisoner who has just been in the room may declare, "every prisoner has now visited this room at least once." If the declaration is correct, all prisoners go free. If it is wrong, all prisoners are executed.

The prisoners are allowed to agree on a strategy beforehand, but never communicate again once the calling begins. Design a strategy that guarantees, with certainty and not merely high probability, that someone eventually makes a correct declaration.

Three piles, and the invariant that settles all of them

Three piles hold 5, 7 and 9 counters. Players alternate. A move takes any positive number of counters from exactly one pile. Under the normal rule the player who takes the last counter on the board wins.

  1. Who wins from 5, 7, 9 with perfect play, and what is the move?
  2. Prove the rule you used, in full.
  3. Now change the win condition: the player who takes the last counter loses. Who wins from 5, 7, 9? Who wins from 1, 1, 1?
  4. State the rule for the changed condition and say why so little of the analysis moves.

Reference articles

Something that never changes, and odd versus even

A quantity that no allowed move can alter proves some goals impossible without trying them. Odd and even is the simplest such quantity, and surprisingly often the right one.

Testing the data, not the code

A green test suite says the code did what it says. It says nothing about the rows. The invariants worth asserting, the ones that cost nothing, and the defect class none of them see.

The verify loop

Two generated failures worked end to end: an invented pandas method, and a returns function that runs cleanly and computes the wrong thing. Plus the four-step loop that catches both.

Puzzles

Three piles, and the invariant that settles all of them

Three piles, take as many counters as you like from any one of them, and the final counter decides it. Find the invariant that settles every position, then flip who wins at the end.

Brainteasers

Ants on a pole, and what happens when two of them meet

Any number of ants on a pole, walking at the same speed, reversing whenever two meet head on. Bound the time until every ant has fallen off, without tracking a single collision.

Chameleons that change colour when they meet

Three colours of chameleon on one island, and any meeting between two different colours turns both into the third. Decide whether the island can ever go a single colour.

The prisoners and the switch in the empty room

One switch, no other communication, a warden calling prisoners into a room one at a time forever. Design a protocol proving everyone has visited, with certainty rather than confidence.

Two corners come off a chessboard, and the dominoes still look like they fit

Cut two opposite corners off a chessboard and the arithmetic still works out perfectly. Decide whether a full domino tiling exists, without trying a single arrangement.

Related topics

The ideas this one is most often carried alongside, ranked by how much of each collection the two share.

Also in the library