I once stumbled upon a magic trick. It was in one of those party bombs, you know, those that spill all kinds of toys or sweets through the entire room when exploded.

The trick is called **The Magic Computer** and as a developer I was curious: How can a stack of six cards be a computer? Let me explain...

# Imagine being at a party...

So, the magician asks a volunteer to pick a number from 1 to 63 and keep it secret. Now the volunteer is presented six cards with loads of numbers on them:

They should give back all the cards with their number on it. The magician immediately knows which number the volunteer chose and announces it. The volunteer is baffled: the number is correct! The audience cheers, the magician takes a bow, and the volunteer? Still amazed.

How did the magician know? Did they memorize all the combinations? No, they added up the numbers in the top left corner of the cards they got back from the volunteer. That's it. That's the entire trick! But why does it work?

# Exploring the trick - something about those numbers

Let's have a look at the distribution of the numbers on the cards. I'll number the cards 1 to 6 (sorted by the number in the top left corner) and will analyse which numbers occur on which cards. For this, I'll use a table for the first few numbers:

Number | Card 1 | Card 2 | Card 3 | Card 4 | Card 5 | Card 6 |
---|---|---|---|---|---|---|

1 | Yes | |||||

2 | Yes | |||||

3 | Yes | Yes | ||||

4 | Yes | |||||

5 | Yes | Yes | ||||

6 | Yes | Yes | ||||

7 | Yes | Yes | Yes | |||

8 | Yes | |||||

9 | Yes | Yes | ||||

10 | Yes | Yes | ||||

11 | Yes | Yes | Yes | |||

12 | Yes | Yes | ||||

13 | Yes | Yes | Yes | |||

14 | Yes | Yes | Yes | |||

15 | Yes | Yes | Yes | Yes | ||

16 | Yes |

Hm, that looks like an oddly familiar pattern. Let's instead use 1 for "Yes" and 0 for "No" for the first 8 numbers, only to illustrate:

Number | Card 1 | Card 2 | Card 3 | Card 4 | Card 5 | Card 6 |
---|---|---|---|---|---|---|

1 | 1 | 0 | 0 | 0 | 0 | 0 |

2 | 0 | 1 | 0 | 0 | 0 | 0 |

3 | 1 | 1 | 0 | 0 | 0 | 0 |

4 | 0 | 0 | 1 | 0 | 0 | 0 |

5 | 1 | 0 | 1 | 0 | 0 | 0 |

6 | 0 | 1 | 1 | 0 | 0 | 0 |

7 | 1 | 1 | 1 | 0 | 0 | 0 |

8 | 0 | 0 | 0 | 1 | 0 | 0 |

Hmmm, that does indeed look like binary. Let's play through the trick for a specific number to check our assumption. I choose the number 27 and therefore hand back the cards **1**, **2**, **4** and **5**. I keep the cards **3** and **6**.

Since these cards are the only ones containing the number 27, I know that the others don't. I can therefore read the out the "Yes/No" pattern.

So the pattern of which cards I handed back/contain the number, this time in **reverse**, looks like the following:

```
0 1 1 0 1 1
```

And that is indeed binary 27! This explains why the numbers on the top left corners add up to the number chosen by the volunteer: They're powers of two!

```
1, 2, 4, 8, 16, 31
```

Or in other words:

```
2**0, 2**1, 2**2, 2**3, 2**4, 2**5
```

(`x**y`

is a way of writing `x to the power of y`

) So the number I chose earlier, 27, is made up of

```
(0 * 32) +
(1 * 16) +
(1 * 8) +
(0 * 4) +
(1 * 2) +
(1 * 1) = 27
```

So by handing back the cards, the **volunteer describes a binary string, representing the number they chose**. All the magician needs to do is converting it back to decimal by adding up the bits. Neat!

# Takeaway thoughts

That's where the name of the trick comes from: A computer stores numbers as binary strings. But those six cards are by no means Turing complete... In my opinion, this trick should have been called the **Magic Number Base Converter** - that would be a lot more accurate.

**What are your favorite magic tricks involving maths?**

I write tech articles in my free time. If you enjoyed reading this post, consider buying me a coffee!

## Top comments (0)