Coins And Levers Puzzle - Solved
You are a prisoner in a strange land, and the king has decided to play a game with you. The game works as follows:
The king will place 100 gold coins on a table in a room that is otherwise empty.
You will then be taken to another room, where you can't see or touch the coins. In this room, there are two levers, labeled "A" and "B".
You must pull one of the levers, which will either flip all the coins or leave them as they are. You don't know which lever does which.
You will then be taken back to the room with the coins, where you can see the result of your lever pull.
You must repeat this process as many times as you like, pulling either lever A or lever B each time.
Your goal is to ensure that there is an even number of heads (i.e. gold side up) on the table when you are finished. If there is an odd number of heads, the king will execute you.
The catch is that you can't keep track of which coins are heads or tails, and you can't communicate with anyone else while you're in the room with the coins. You also can't take any of the coins out of the room with you.
How can you ensure that you always leave an even number of heads on the table, no matter how many times you pull the levers?
Solution.
The key to solving this puzzle is to use binary numbers. Each digit in a binary number represents a power of 2, so for example, the binary number 1011 represents the number 11.
Now, back to the puzzle. If you start with all the coins showing tails (i.e. gold side down), then there are an even number of heads on the table. Each time you pull a lever, you flip some of the coins, so the number of heads on the table changes. Let's say that you pull lever 'A' k times and lever 'B' m times, for a total of k+m pulls. Then the number of heads on the table will be:
N = 100 + k - 2km
Here's how we get this formula. Each pull of lever A flips all 100 coins, so it changes the number of heads from x to 100-x. Similarly, each pull of lever B changes the number of heads from x to x+1 or x-1 (depending on whether there are an even or odd number of heads to start with). We can combine these two cases into one formula:
N = (100-k) + k*(1-2m)
Simplifying this formula gives us the one above.
Now, we need to ensure that N is even no matter what values of k and m we choose. One way to do this is to choose k to be any odd number (such as 1 or 3) and m to be any even number (such as 2 or 4). This ensures that the term k - 2km is always odd, so we get an even number of heads (100 + odd number = even number).
So, for example, if we choose k=3 and m=2, then we pull lever A three times and lever B two times. The number of heads on the table after each pull is:
Pull 1: 0 heads (all tails)
Pull 2: 100 heads (all heads)
Pull 3: 98 heads (two tails flipped by lever A)
Pull 4: 97 heads (one more tail flipped by lever B)
Pull 5: 94 heads (two more tails flipped by lever A)
Since 94 is even, we have succeeded in our task and avoided being executed by the king!