27-Card Trick (Ternary Magic in Snap!)

This is a simple Snap implementation of the classic 27-card trick, written to show the math behind the trick instead of just the effect.
The program uses lists and ask blocks (no graphics) as a quick proof-of-concept while learning how Snap handles user input and lists.

The trick works by repeatedly dealing 27 cards into 3 piles, asking which pile contains the chosen card, and gathering the piles again. After three rounds, the chosen card always ends up in the location entered at the start.

The reason this works is that the trick is really based on base-3 (ternary) arithmetic.

If the chosen position is N, we first use:

N − 1

and convert that number to base-3.

Example:

Favorite number = 20

20 − 1 = 19
19 = 201 (base 3)

The ternary digits determine how the piles must be gathered at each round.

Because the first deal corresponds to the least-significant digit, the digits are used in reverse order:

201 → 1, 0, 2

Each digit tells where the chosen pile goes when gathering:

0 = top
1 = middle
2 = bottom

After three rounds the card is forced to the center, which is why the trick always works.

This version keeps everything text-based for simplicity, but it could be extended later to a graphical card layout.

this youtube video about this mathematical trick in action, inspired me to make a version for snap


My favourite card trick I used to do when I was little :slight_smile:

Slight visual issue at end - right card in position 11 (of course) but text at top a bit truncated

thanks for the catch. i’ve changed it - i moved the text a little to the left.
(BTW - do ‘Share’ and ‘Publish’ create different versions? also, how would i share this program so it shows up in Explore? also, how should i have uploaded this to the topic (when i created it) so it shows up as the program - ready to start, and not as a link, the way it appeared here?) thanks.

Mystery to me how/why stuff shows up on forums in different ways - I just accept whatever happens as long as it works :slight_smile:


Be nice if colour of suit was red :slight_smile:

it should be working now

Not reached me yet :slight_smile:

try again please - i don’t know how many versions there are and where are they!

:slight_smile:

I keep this tab open so I can see all my recent projects

i do that too, or used to - it’s been a long time since i played with snap. i remember now - by clicking publish, it goes to the explore area - open to the public.

Amazing project!

thank you!