Guess My Number (Binary MagicTrick)

I just finished a classic “Guess My Number” project in Snap! and wanted to share the clever math behind it.

:joker: How It Works

  • There are 6 cards, each displaying 32 numbers (from 1–63)

  • The first number on each card is its “secret value”: 1, 2, 4, 8, 16, 32 (powers of 2!)

  • The player thinks of a number between 1 and 63

  • For each card, they answer: “Is your number here?”

  • If yes, the card’s secret value is added to a running total

  • At the end? :tada: The total is the player’s number!

:1234: Why It Works

Every number from 1–63 has a unique 6-bit binary representation.
Example: 42 = 32 + 8 + 2 → binary 101010 → appears only on cards starting with 32, 8, and 2.

The cards are pre-filtered so a number appears on exactly the cards whose secret values sum to it. No magic—just binary! :sparkles:

Cool project! I remember learning this trick from a teacher at school.

I personally would have the cards descend from most significant digit to least significant, but that’s me. You can keep it your way if you prefer :slight_smile:

Guess My Number (Binary MagicTrick)

No magic—just binary!

So is it magic or not?

Try the following. The (r) variable is not necessary. (Booleans are casted to 1s and 0s when used in math)

Oh, and I recommend you minimize the use of single-letter variables too. Might be a personal preference, but code is more readable if you use variable names that do describe the purpose.

Thanks for the tips.

This game is dear to me because it brings back memories of me making this self decoding deck shown below (the 2nd photo is what’s at the back of the 32-card. Play by dealing (from low to high. 1,2,4,etc) when player means yes, keep card upright,else turn upside down. keep doing this until the end, each time stacking unto the pile. After you place the final card (note that yes/no orientation is different, you will have put the master list ,(all numbers) on top. Flip the whole packet to see the selected number peeking out of the little window produced by stacking those binary holes! (I’m almost sure I saw this from Martin gardner’s articles in scientific american or one of his math books)

Wow. The way all of the papers and the holes allow the number to peek out is brilliant!

And this was in the 70s!