Conjuring With Computation - A Manual of Magic and Computing
Conjuring With Computation - A Manual of Magic and Computing
World Scientific
Published by
World Scientific Publishing Co. Pte. Ltd.
5 Toh Tuck Link, Singapore 596224
USA office: 27 Warren Street, Suite 401-402, Hackensack, NJ 07601
UK office: 57 Shelton Street, Covent Garden, London WC2H 9HE
For photocopying of material in this volume, please pay a copying fee through the Copyright Clearance
Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, USA. In this case permission to photocopy
is not required from the publisher.
Printed in Singapore
This page intentionally left blank
This book is dedicated to my friend and co-author Peter W. McOwan,
who was the magical brain behind it. He originally came up with the idea
of our doing magic shows to inspire kids about computer science. He also
taught me to do magic and gave me the confidence, even as a complete
novice, to do live shows. For the book, more or less, he supplied the
magic and I fitted the computer science to it. He died before we finished
the book. It is not so much fun without him.
Paul Curzon
This page intentionally left blank
Contents
Chapter Summaries xv
Chapter 0. Introduction 1
ix
x Conjuring with Computation
Acknowledgements 369
Chapter 0. Introduction
This is a book that will teach you some magic, but also explains
the many links between computing and magic, with the result
that you will learn about both magic and computation.
xv
xvi Conjuring with Computation
three numbers that divide exactly into it. Even more surprisingly,
when the divisions are done, the answer is the original number.
A key step in programming, and a part of computational think-
ing, is in turning a possibly imprecise version of an algorithm into
very precise steps with no room for doubt. That requires a precise
language with a very precise mathematically defined meaning.
This is a difference to magic where informal descriptions are
used.
We can make more new tricks from simpler ones by chaining trick
elements together in sequences where they pass information from
one to the next. The resulting trick can be much more powerful.
The same applies to programs.
What you see depends on where you focus your attention, and
where you focus your attention depends partly on how salient
the things in the scene are. If you want important things to be
seen on a screen, then you have to make them visually salient.
Interlude: Dynamo
Chapter Summaries xxxi
Introduction
This book is for people who want to learn to do magic tricks for others
but who are also interested in learning more about computation and
how ideas from computer science overlap with those of conjuring.
Computer science and magic have a lot in common. Famously,
Arthur C. Clarke said that sufficiently advanced technology was like
magic. In fact, magicians have always known this and often work at
the leading edge of science and engineering to invent new tricks. As
it is at the forefront of technology, understanding computer science is
important for magicians interested in technology-led magic. The link to
computer science is much deeper than just using advanced technology,
though.
A popular saying is that Computer Science is no more about
computers than astronomy is about telescopes. It is at core about com-
putation, which is what computers do. What is computation? A simple
version is that it is just doing calculation by following instructions.
A more sophisticated version is that it is about the manipulation of
symbols. Conjuring is also closely linked to computation.
Computer scientists also learn to think in a particular way, called
computational thinking. It is the general skill that underpins program-
ming but is also important in more general situations. Magicians, it turns
out, have to think in a very similar way to computer scientists.
Computational thinking is in part about creating precise instructions
that always achieve a desired effect. When applied to magic, it is not
about the performance of tricks. That is playing the part of the computer
following the instructions in a program to get something done. It is more
1
2 Conjuring with Computation
about inventing new tricks, modifying old ones to improve them, and
finding ways to be sure they always work. Many more general ideas from
computer science have close links to magic too. We can learn a lot about
computer science from magic, and vice versa, and that is what this book
is about.
All the tricks in this book are simple ones that are fairly easy to
perform, and variations of which are widely known and easily found on
the Internet or in introductory magic books. Just as computer science
is about creating new computer systems, we hope that they will inspire
you to start to invent your own tricks. Perhaps at first, you might just
make tweaks to these tricks, changing the presentation, or combining
effects, but you will ultimately move on to invent your own completely
new tricks.
When you do the tricks for others as part of a magic show though,
keep the secrets. Never use them for anything other than entertainment
(or if you are a teacher, for education).
The best magic, by professional magicians, of course still leaves us
as baffled as everyone else.
Introduction 3
Algorithmic Thinking
Computers cannot think for themselves. They just blindly follow instruc-
tions. Those instructions must always work when followed. Algorithmic
Thinking is the skill of writing such instructions.
This page intentionally left blank
Chapter 1
Invisible Palming
What is an Algorithm?
Conjuring
You invisibly move a card from one pile to another. You can do it
even as a novice magician who has no idea how it is done.
Computation
An algorithm is a set of instructions that if followed precisely, and
in the right order, without thought, leads to a guaranteed result.
The Trick
You need 15 identical cards. One way to get them is to buy several
identical packs of playing cards, then take out and use the Jokers so all
15 cards are the same. Have a volunteer put out both hands as though
they are playing the piano with fingers and thumbs touching the table
(see Figure 1.1). Tell the audience they must chant the magic words:
“Two cards make a pair” each time you take a pair of cards.
Take two cards and, as everyone says “Two cards make a pair,”
place them together between a pair of fingers or finger and thumb of
7
8 Conjuring with Computation
Figure 1.1. In Invisible Palming, the magician places pairs of cards between the
fingers/thumb of each hand of the volunteer, with the last place taken by a single
card.
the volunteer. Keep doing this until you have one card left. Place it
between the final fingers saying there is “one leftover”.
Now take the first pair back, again all chanting “Two cards make a
pair”. Separate them and place them face down on the table to start two
piles, one card from the pair in each pile. Take each pair back in turn
adding them to the piles: one card from each pair being neatly placed on
each pile. As you do, everyone should continue to say the magic words.
Eventually, only the final, single card is left. Take this card saying: “We
have one extra card.” Let the volunteer place it on top of one of the
piles of their choice. Square up the piles pointing out: “That pile now
has the extra card.”
Explain that now the room is full of magic from all the chanting,
you are going to do “Invisible Palming”. The extra card is on one
pile. You are going to invisibly move it to the other. Place your hand
over the pile with the extra card. Rub the back of your hand to
“make the card go invisible”. Lift your palm showing that the card
is there but is invisible. Move your hand to the other pile. Tap it,
“to make the card drop”. Announce that the card has now moved
piles.
Invisible Palming: What is an Algorithm? 9
To show the magic worked, take the pile where the extra card was
placed and count off a pair at a time into a new single face-down pile.
Everyone still should be saying the magic words as you take each pair:
“Two cards make a pair. Two cards make a pair. . .”. Make sure the pile
is neat. You find there are only pairs: the extra card has gone! So where
is it? Take the other pile and do the same, peeling off pairs and putting
them back into a pile. Amazingly the extra card is there. It has moved
from one pile to the other!
Before thinking about how the trick works, do it, just following the
steps with a friend as the volunteer (you may find the crib sheet of steps
below helpful: see The Magical Algorithm below). If you follow the steps
precisely, it should just work . . . even if you have no idea how you did it.
You could even swap with your friend and let them be the magician. It
should work for them too.
How can you do a magic trick, magically moving a card from one
pile to another without knowing what you did or how it works?
7. Place your hand over that pile, rubbing the back of your hand.
8. Lift your hand, show the palm pointing out that the card is there
but invisible, and then place it over the other pile.
9. Tap your hand and remove it, saying the card has moved.
10. Pick up the first pile.
11. DO THE FOLLOWING 4 TIMES:
a. Add 2 cards from the pile you are holding into a single, neat
pile on the table.
b. Say “Two cards make a pair”.
12. Note that the extra card is no longer there.
13. Pick up the second pile.
14. DO THE FOLLOWING 3 TIMES:
a. Add 2 cards from the pile you are holding to a pile on the
table.
b. Say “Two cards make a pair”.
15. Point out that you are holding the extra card: it had moved to
the second pile.
How It Works
Magicians call this kind of trick a self-working trick. It always works if
you follow the steps precisely. It works whatever the volunteer does given
the choices they have (so here wherever the extra card is placed).
There are 15 cards. After dealing out the pairs into the two piles,
those two piles are identical: there are 7 cards in each. Of course, you
don’t give anyone a chance to count the cards. First, think about the
pile where you put the last, supposedly “extra”, card. That card makes
the pile up to 8 cards: 4 pairs. When you count out in pairs, there will
only be pairs there, so apparently no “extra”card anymore. It appears to
have vanished, but it just finished a pair where it was.
The other pile will be left with 7 cards: 3 pairs with one left over.
When you count in pairs, that one left over seems to be the original
extra card. It seems to have appeared there, but it was there all the time.
You pretend it has magically moved without doing anything. Nothing
has to move!
It appears magical only because you have confused everyone. They
believe when they add the last card to a pile that they are adding an
Invisible Palming: What is an Algorithm? 11
extra odd card. You are actually completing the last pair, though. It
doesn’t matter which pile they add the last card to: that card will turn
an odd pile into an even pile. You count out the cards in pairs to hide
the exact number.
Thinking Computationally
Algorithms, magic and programs
Computer scientists have a similar idea to self-working tricks called
algorithms. An algorithm is just a set of precise instructions that if
followed exactly leads to a guaranteed effect. The instructions have to
cover all eventualities, just as a trick does.
Once you have created an algorithm that works, you don’t have
to think about solving the original problem anymore or what to do to
achieve that effect. You just blindly follow the algorithm, and the right
thing happens. It is just like following the Invisible Palming instructions:
the magical effect of apparently invisibly moving a card just happens.
If you followed the steps exactly, it will have worked for you even if
you had no idea how it worked. In doing the trick by following the
instructions, you were acting like a computer following an algorithm.
More specifically you were acting as a computational agent. It is
something or someone following algorithmic instructions precisely and
blindly.
A computer cannot think for itself. It can only blindly follow
instructions (the instructions are its program). They were written
for it by a programmer. It does computation as a result. Computers
are therefore computational agents. Computer programs are essentially
algorithms written in a programming language (like Python, Java or
Scratch). They are languages of instructions in a form that a computer
can follow.
The detail matters in algorithms. Take our trick. Just saying we
should pretend to move a card from one pile to the other isn’t good
enough. We need to say exactly how to do it in a way that cannot be
confused. Similarly, a program needs to tell the computer exactly how
to do every little step.
12 Conjuring with Computation
Wizards
A magician who invents new self-working tricks is creating algorithms.
A programmer writing a program is doing the same. They are both using
algorithmic thinking skills: working out a series of steps that will always
have the effect they are after. Algorithmic thinking also involves writing
instructions really clearly and precisely so that there is no confusion
about what to do as the instructions have to work when being followed
by a Computational Agent.
Expert programmers, male or female, are often called wizards. They
really are!
Conjuring
You cut a ring into two pieces down the middle, only to find you
still only have one ring. Try again, and at last, you have two rings,
but bizarrely they are linked together.
Computation
There are three fundamental ways to organise instructions. The
simplest way is sequencing: following instructions one after the
other in the given order.
The Trick
Start with a strip of paper about 5 cm wide and at least 30 cm long
(ideally longer) and glue it in a loop, but with a twist so the top of one
end of the strip is glued to the top of the other end (see Figure 2.1). This
is called a Möbius strip. Show it to the audience without mentioning the
twist. Explain that you will cut the loop into two thinner rings. Gradually
cut it down its centre. When you get back to the start, confidently say
you now have two rings. Gently untangle them.
13
14 Conjuring with Computation
Figure 2.1. A Möbius strip. Cut along the dotted line all the way around the centre
to make two interlocked rings.
Look shocked when you see it is still a single ring and wonder out
loud how that could possibly happen, saying that you will try again.
Take this new longer, thinner ring and carefully cut it down the middle.
Say you must have two rings now, and gently untangle them. Look even
more shocked as you show that you do have two rings, but somehow
they became interlinked as you cut them.
Tell the audience that there is clearly far too much magic in the
room if paper rings are going to do that to you.
How It Works
This is another simple self-working trick that relies on an area of maths
called topology. Cut a Möbius strip down the middle, and it makes a
longer strip. Do it again, and you get two interconnected rings. It is all
due to the twist which meant you had created a ring with only one side:
draw a pencil line down the middle and it will eventually join back to
the start, having covered both sides of the original strip!
Thinking Computationally
The algorithm behind this trick is about as simple as can be as it is
only a few steps. There are two aspects of an algorithm that matter: (1)
the actual instructions and (2) the order you do them in. The latter is
called the flow of control. It is the plumbing that connects instructions
together.
This algorithm is just a sequence of instructions that must be
followed one after the other, in the order given. Follow each step
correctly, exactly once and in the right order . . . and the magic will
happen. This way of linking instructions is called sequencing. It is the
simplest kind of control structure: constructs that determine the flow
of control, the order things are done.
Sequencing of individual commands is the core to other kinds of
instructions that people and computers follow including recipes, the way
you learnt to add numbers, and programs.
Simple programs are very similar to our magic trick’s description.
They are a sequence of instructions to be followed one after the other in
the given order (see Figure 2.2). Here, we have labelled the steps (1) (2)
(3) . . . to show the order to do them. Most programming languages just
use the order that the instructions are written on the page to indicate
the correct sequence, so in most languages the steps tend not to be
numbered.
Any computer or computerised gadget that is doing one thing after
another is following a sequence of instructions some programmer previ-
ously wrote. Take a chip-and-pin card machine. It shows you the amount
you owe, asks you to touch your payment device (card, phone, etc.) and
16 Conjuring with Computation
Figure 2.2. The sequence of instructions for the Möbius rings trick as a flowchart.
The instructions are followed one after another.
Conjuring
A volunteer chooses one from a series of magical objects, only to
find that you predicted their choice.
Computation
The second way of determining which instruction to follow next is
called selection: choosing which of several sets of instructions to
follow next based on the truth or otherwise of some test.
The Trick
Before the trick, slide a note into a pack of cards with the message “You
WILL choose ME” written on it. Place six different objects, including
the pack of cards, in a line on the table. All should have some magical
connection. You might, for example, use a magic wand, a cup with a ball
underneath, a pack of cards in their box, a silk handkerchief, a coin and a
watch. Ideally, all should be solid objects that could not possibly contain
a note, except for the cards. The cards should be in the third position
19
20 Conjuring with Computation
from the left. You stand on one side of the objects with a spectator on
the other.
Explain that magical objects get infused with magic if they are used
in magic shows and all these objects are especially magical because they
have all been used in tricks many times.
Ask the volunteer to choose a number from 1 to 6 telling everyone
what it is.
The number they choose, when you count to that object, leads to
the pack of cards. Explain that as you do lots of card tricks the cards
are full of magic and like to be chosen. Ask them to open the pack of
cards and see what is inside, then get them to read the note. Note again
how magical your cards are.
How It Works
You do something different depending on their choice of number (see
Figure 3.1). Whichever number they said gives a different way of
counting that forces the choice of the third object: the pack of cards.
If they choose the 1, then you start at your left and count out letters
to the third object: O . . . N . . . E pointing to an object on each step.
If they choose 2, then similarly you count to the third object:
T . . . W . . . O.
If they choose 3, then you just count to the third object: 1 . . . 2 . . . 3.
If they choose 4, then ask them to count out 1 . . . 2 . . . 3 . . . 4 from
their left (your right) to end on the 3rd object from your left (the 4th
from theirs).
If they choose 5, then ask them to say the letters of the word FIVE
from their left (your right) F . . . I . . . V . . . E to end on the 3rd object
from your left (4th object from theirs).
If they choose 6, then you count out the letters to the third object:
S . . . I . . . X.
They have been given a false choice: you did not tell them in advance
what you would do with the number they picked. As you only do the
trick once, you only do one of the alternatives, so they do not get any
inkling that it would have happened differently another time.
Six Magical Objects: Selection 21
Figure 3.1. Whichever number is chosen, you choose a counting method that counts
to the third object.
Thinking Computationally
The algorithm behind this trick is again a sequence of instructions as
steps 1 to 5 are followed in sequence, but now there is something new,
a new kind of plumbing. In a sequence, every instruction is followed.
Here, there are six possible instructions in step 4 (instructions a–f).
However, only one of them is followed each time that you do the trick.
These instructions are part of a bigger structure that controls the flow:
a selection.
Programmers call this kind of structure an IF statement. It
combines instructions to follow with tests that are either true or false.
For example, “they say 1” is a test. Either they did say 1 (it is true) or
they didn’t (it is false). Programmers call these kinds of test boolean
expressions (boolean means true or false). If the test is true, then
you do the corresponding instruction and ignore the other tests and
instructions in the selection. If the test is false, then you ignore that
instruction and move on and check the next test. Whichever instruction
you ultimately follow, after doing it you ignore the other tests with their
linked instructions and instead move on to the next instruction after the
whole selection statement (here instruction 5). The selection instruction
is just one statement in the sequence of instructions followed in order,
but it controls which of several instructions is actually done at that point
(see Figure 3.2). In fact, you really only need a simple IF statement that
has just a true and a false option. More complicated selection structures
like the one here that choose between lots of options can then be built
from it.
Every time you used a gadget and it gave you a choice, buried
somewhere in the code was a selection statement that a programmer
wrote to do that choice. Do you have a choice of avatars? Do you have
a choice of buttons to press? Or a choice of links to click? Do you have
Six Magical Objects: Selection 23
Figure 3.2. The flow of control of the IF statement in the Six Magical Objects trick.
It is a sequence of IF statements inside the outer sequence of instructions.
24 Conjuring with Computation
Conjuring
A volunteer chooses a card. You unerringly find it even though you
don’t see the card or where it is placed back in the pack.
Computation
The third way of controlling the order instructions are executed
is called repetition. It involves following the same instructions
repeatedly until a test allows you to move on. If you have
sequencing, selection and repetition, you do not need any other
control structure. Other control structures just make instructions
easier to read and write.
The Trick
Have a volunteer shuffle the deck. When they hand it back, secretly
glimpse at the bottom card and remember it. This is your key card.
Spread the deck face down and have them pick out a card without you
seeing it. Tell them to remember it.
25
26 Conjuring with Computation
Figure 4.1. The state of the cards in the middle of the cut.
Reassemble the pack face down on the table. Then ask them to
place their card on the top of this face-down pile. Turn your back, and
ask them to cut the pack wherever they want. Next, have them place the
bottom section onto the top section to complete the cut (see Figure 4.1).
At this point, turn back around. Point out that their card, which you
don’t know, is now buried in the pile at a position that you also don’t
know. This is all true, but unknown to them your key card is now right
next to their chosen card.
Start to deal the cards one by one onto the table. Deal until you
reach your key card, then turn over one more card. Announce that it
was their card.
For a slightly more entertaining variation, you could employ the
“magician in trouble” presentation. Deal three cards past their chosen
card, then pause. Confidently announce “The next card I turn over will
be your card”. Of course, they saw you deal past their card, so they think
you are going to be wrong. Then, in the final twist, you don’t turn over
the next card in the pile you are dealing from. Instead, you pull their
chosen card out of the face-up pile and turn that over instead.
You have saved yourself, with a nice added comic twist.
Turning the Key: Repetition 27
How It Works
Cutting the pack, in the way described, places whatever is at the bottom
of the pack directly before the card that is on top of the pack. Therefore,
as they put their card on the top, you have guaranteed that it will be
the one after the card you remembered.
Thinking Computationally
The third way of connecting instructions to make algorithms is called
repetition. The line
DO THE FOLLOWING UNTIL . . .
indicates we are about to do repetition. We call this kind of instruction
a loop (there are many variations of loops: this is just one kind).
28 Conjuring with Computation
Counter-controlled loops
There are other forms of flow of control, but none allow you
to write out any algorithm that you can’t already do with
these three kinds of instruction. They are just variations of these
three.
Turning the Key: Repetition 29
For the more fun version of the trick, you replace steps 12 and 13
with the new steps:
14. DO THE FOLLOWING 4 TIMES:
a. Deal the next card face up onto the table.
15. Announce that the next card you turn over will be the volunteer’s
chosen card.
16. Turn over the card four places back in the face-up cards.
Here, we use repetition again but in a slightly different form. The first
line tells us we have a loop. Rather than just a test, it says exactly how
many times to repeat the instruction in the body. The line labelled (a)
is, as before, the body, so the instructions are to be repeated.
This kind of loop is called a counter-controlled loop. The difference
here is that we know in advance how many times we need to repeat the
body and just repeat it that many times. We said we don’t need any
new kinds of flow of control though and we don’t. Having a counter-
controlled loop construct like this doesn’t allow you to do anything you
couldn’t do with a general repetition instruction. To do the equivalent
with our original general kind of loop, we would just need to add more
instructions to keep track of a counter. Our test is just that the counter
has counted up to the target number.
Set a counter to 1.
DO THE FOLLOWING UNTIL the counter is 5:
a. Deal the next card face up onto the table.
b. Add 1 to the counter.
All that our new counter-controlled loop structure is doing is packaging
together the general loop and instructions to keep count. It is just a
shorthand for the above. Either way, we had to keep track of how often
we dealt a card. Without the counter-controlled loop, we need to add
the appropriate instructions explicitly. The specialist counter-controlled
loop structure also makes our instructions more readable to a human.
They immediately can see it is a counter-controlled loop from the way
it starts, rather than having to work it out by looking at the whole loop.
Making instructions easy for a human to understand is really important
in programming if mistakes are to be avoided.
30 Conjuring with Computation
Calculator Conjuring
Programming Languages
Conjuring
A friend types a six-digit number into a calculator based on a
number they thought of at random. Miraculously, when you give
them three numbers to divide into it, they all divide exactly. Even
though you had no idea what their original number was, they end
up back with that original number.
Computation
Computer scientists don’t just invent algorithms, they have to be
able to formulate them using the specific instructions available in a
given notation (i.e., a programming language). This involves a
whole new set of skills of being able to work with a fixed notation.
The Trick
Ask a friend to think of a three-digit number. Give them a calculator, or
have them use the calculator on their phone, and ask them to key in the
number. Then, say it would be better to have a bigger number, so have
31
32 Conjuring with Computation
them type in the number again to make a six-digit number. Tell them
that you haven’t seen the number but even so can do some mathematical
magic on it. Point out that the chance of a number dividing by 11,
so giving a whole number answer, is very slim. Suggest they try it.
Amazingly, their number does. Suggest it would be even more surprising
if it was also divided by 7. Suggest they try it. Even more staggering, it
again gives a whole number. What about 13? Surely they couldn’t have
randomly chosen a number that would divide by that too! They do so.
Point out it would be totally amazing if they were now looking at their
original number!
How It Works
The reason this works is all about the number 1,001.
Suppose they thought of the number 123. If you multiply that
number by 1,000 then you turn it into 123,000. If you then add the
original number to this you get 123,123. This works whatever the number
is, as long as it is three digits long. Start with 999: multiply by 1,000
and then add 999, which gives you 999,999. So multiplying a three-digit
Calculator Conjuring: Programming Languages 33
number by 1,000 and adding the number is the same as making a six-
digit number out of a three-digit number written out twice. Now, this
is also the same as multiplying by 1,001 as the 1 in 1,001 causes the
number to be added to what you get by multiplying it by 1,000. Try it by
multiplying any three-digit number by 1,001. So, when your friend puts
the number into the calculator twice, they are actually just multiplying
by 1,001.
Now, try multiplying 7, 11 and 13 together. The answer is also
1,001. Putting the number end to end and then dividing by 11, then 7
and then 13 is the same as multiplying it by 1,001 and then dividing
by 1,001. Dividing is the opposite of multiplying (mathematicians call it
the inverse), so that is why we get back to the number we started with.
Written out as an equation, we are using the fact:
Secret × 1,001 ÷ 1,001 = Secret
This also explains why it divides by all of 7, 11 and 13. Another way
of writing the number in the calculator is
Secret × 11 × 7 × 13
since 1,001 is just 11 × 7 × 13. This means that all three numbers we
use divide exactly into the total, and when we do each division in turn,
we are just cancelling out that number from the total.
Thinking Computationally
We are mainly using similarities between magic tricks and computing
concepts to help you understand computing. However, here we focus on
a difference that sets them apart.
One thing that distinguishes computer scientists from most magi-
cians is that of precise notation. An important part of thinking
computationally is to be able to write instructions very precisely and
in a precisely defined language: one that can be followed by a computer.
You must be able to get the job done using only the commands available.
Magicians put the onus on secrecy rather than sharing their tricks.
That means that the need for writing down instructions for anyone but
themselves to be able to follow is not a key part of what they do.
34 Conjuring with Computation
Only magicians writing magic books need to worry about the instructions
being easy to follow. Magicians (and likewise cooks) tend to fall back
on human languages like English to write instructions. That is why
sometimes manuals are quite hard to follow as it is quite hard to be
really precise in a human language like English. Using human languages
is usually fine, but they can be ambiguous or just are not precise enough,
with the meaning not always totally clear. If you don’t bake cakes, try
baking one for the first time following a recipe you’ve never seen before
to get the idea. It probably won’t be perfect the first time. The lack of
preciseness in languages is the reason why when we try to follow written
instructions, whether how to do a trick, fix a puncture or bake a cake,
things sometimes go wrong.
Computer scientists, on the other hand, very specifically do want
others (both computers and humans) to be able to follow their
instructions. This means that ultimately algorithms are written out as a
program in a programming language. What that really means is that they
are written out using only specific commands with very precise, agreed
meanings. Learning to program is in part about deeply understanding
what each command does and learning how to convert vaguer plans
into the commands available.
Magicians could use precise languages too, and indeed alchemists,
those pre-chemists-come-magicians who tried to turn lead into gold, did
create notations to write down their “spells”.
A magic language for magic tricks might include commands like
those given in Figure 5.1.
We can implement the algorithm as a program in our language.
It could then be followed by either a human or a robot magician.
This involves not only working out what the specific steps are but also
the detail. Attention to detail really matters in programming. What
exactly do you say and when? What numbers do we allow as a “three-
digit number”. Does 000 count? What about 001? Our program very
specifically rules those out.
STORE x n
This means the number n is given the name x in the remainder of the
description of the trick.
SECRET person {n1…n2}
This means the person must choose a number between n1 and n2, keeping
it secret. It delivers a number.
DIVIDE n
This means whoever holds the calculator should divide the current number
displayed on the calculator by n leaving the result displayed on the
calculator and then read out the result. Here n is a whole number.
TYPE n
This means whoever is holding the calculator should type the number n into
the calculator.
PASS person object
This means give person the object.
SAY person s
This means the given person must say s aloud.
In the above, object is a magical prop chosen from {wand, hat, silk handkerchief,
and calculator} and person is chosen from {Volunteer, Magician} n, n1 and n2 are
whole numbers. They could be the name given to a number, an actual number or
an operation (like SECRET) that delivers a number.
12. DIVIDE 7.
13. SAY Magician “A whole number. That is staggering. What are the
chances! Let’s try one more. What about 13? Surely you couldn’t
have randomly chosen a number that would divide by that too!
14. DIVIDE 13.
15. SAY Magician “Wow . . . it divides exactly too! . . . And even more
magically, you have ended up back at the number you started
with! That is mathematical magic!”
To be able to write programs like this requires several extra skills over
just thinking up algorithms that computer scientists develop. The first
is the ability to formulate instructions using a specific set of precisely
defined available instructions. That means a programmer needs to be
able to twist what they want to happen to fit what is available. This
is a core skill of programming. They also need the ability to think very
precisely about detail: had you thought about whether 000 was a three-
digit number or not? If you are writing the program, you have to.
Most programming languages are universal meaning that a program
can be written in the language to do any computation that can possibly
be computed. However, the same thing may be done in different ways.
Depending on the problem (and so algorithmic solution developed),
the algorithm as designed might very directly be converted into the
programming language . . . or not. If it can, then once you have the
algorithm, writing the program is relatively easy. In other situations,
the constructs used in your algorithm are far from those available in the
language. We didn’t have a way to say “three-digit number” directly,
for example, so had to work out the range we meant instead. That is
one reason why there are so many languages. Different languages are
designed to make different kinds of problems easier to solve. Choose an
appropriate language for the job at hand, and your task as a programmer
becomes easier.
A key reason we use programming languages is that the instructions
have to be easy to convert into a form that is executable on the
machine. However, there is more to writing programs than that. Just
as importantly, they are for humans to read too; something many
programmers pay too little attention to. Large programs are written
Calculator Conjuring: Programming Languages 37
Conjuring
You show that books about magic can control people’s minds. A
volunteer freely chooses a word and then lets the words of the book
jump them randomly from place to place until they land on a final
word. Amazingly, you had placed that word in a sealed envelope,
held by the volunteer throughout.
Computation
It is very easy for programs to contain bugs and subtle mistakes
that mean that sometimes they do the wrong thing. A good
program will work whatever the situation, and to ensure this, we
must actively look for bugs. One way to do this, called testing, is
to run the program many, many times and check it works correctly
every time. With some logical reasoning, we can do better than
that, though.
41
42 Conjuring with Computation
The Trick
First, you need a copy of the book of Shakespeare’s play, Macbeth. You
can buy a cheap copy from any bookshop. (At the end of this chapter,
we will show you how to create your own version of this trick with a
book you already own.)
Write the word “HEATH” on a piece of paper, and place it in an
envelope clipped onto a clipboard. Place on the clipboard, over it, a
piece of paper with the opening lines to Shakespeare’s Macbeth written
out as in Figure 6.1. They are from the famous first scene where the
three witches meet. Draw a dotted line after the first sentence. Write
the last part of the quotation in bold as shown (or write it in red). Make
sure there are no spelling mistakes or missed words.
Now for the trick. Give a volunteer the clipboard and a pen. Give
another person an actual copy of the book, Macbeth.
Tell the audience how later in the play the witches meet Macbeth
and predict that he will be King. But words read aloud over and over gain
a magic of their own. Macbeth has been read so often that the words
of the play can control a person’s mind and so make other predictions
come true.
Ask the person with the book to read the opening lines, while the
first volunteer checks that the words on their clipboard are exactly the
same words that the witches speak.
Figure 6.1. The first lines of Macbeth written out as needed for the trick.
Wizardly Book Magic: Testing 43
Get the volunteer to put their hand on the book and “draw on its
magical powers”. Have them then pick a word at random from the first
sentence of the quotation (before the first dotted line), as spoken by the
first witch. It should be one of the words:
When shall we three meet again/In thunder lightning or in rain.
They should circle their chosen word, telling everyone what it is. For
example, if they choose the word “three”, then that is the word they
circle and announce.
Now, explain that they must let the book control them. They
will jump from word to word, guided only by the magic of the words
themselves. They will stop only when they end a jump on a bolded
word (after the second dashed line). That bolded word will be their final
chosen word.
To do this, they should count the number of letters in the original
chosen word. They then step forward that many words to land on a new
word. They circle it. For example, if they did choose the word, “three”
originally, then it has five letters. They step forward five words and land
next on “lightning” which they circle. Have them do this with their word.
Double-check that they don’t make a mistake.
They do this repeatedly bouncing from word to word, each time
counting the number of letters in the new word and moving on that
far. They should ignore punctuation. They should also ignore the stage
direction in the book which is why you’ve given them a version with only
the words spoken.
They keep going until they jump to a bolded word for the first time.
Check each step with them and the audience. They should announce
what that final bolded word is. Have them agree that it was their own
free choice of which word to choose originally and that they had no idea
what bolded word they would land on. No one knew.
Finally, explain that using the magic in the book, you made a
prediction beforehand. Point out that there is an envelope on their
clipboard under the sheet they have been writing on. They should open
it and read out what it says. Magically, they find the word written is the
bolded word that they finally landed on!
44 Conjuring with Computation
How It Works
Whatever word they pick from the first sentence, your prediction will be
correct (of course). Each word in that first sentence leads to the same
bolded word. We will come back to exactly why it works after we have
looked at some computational thinking, though.
Deeper magic?
Now you know the trick, you can do it yourself, even if you don’t
understand how it works (yet). If you start with the words from Macbeth,
it will always work. In fact, it is even more magical. It will work with just
about any book about witches, wizards or magic, though the word to
Wizardly Book Magic: Testing 45
predict will be different for each. Try it with the words from the start of
a different book: The Wonderful Wizard of Oz. Don’t trust me to give
you the words. Find a copy. For this book, if you pick a word from the
first sentence, you will always end up on the word “FOUR”.
Want more evidence? Try another completely different magical
book: The Cat in the Hat by Dr Seuss. It’s about a magical cat. Choose
a word from the first sentence. I predict you will land on the word “SAT”.
The magic is even deeper than that. It will work with just about any
writing, though in some books the sentence to make bold is further on
than in others.
A conspiracy?
Is this really some powerful, deep magic? Or has there been a secret
conspiracy by authors, all agreeing for centuries to write their books
so this trick works? No. It’s just a simple, if surprising, property of the
words in books. It is another “self-working trick”, another algorithm. It
always works if you follow the steps, whatever book you choose.
Thinking Computationally
Testing times
I’ve claimed the trick always works for that passage from Macbeth. Do
you believe me enough to try the trick for yourself with an audience?
How can you be sure it always works? Well, you could try it a few times.
Would that convince you? Does that give you enough evidence? What
if they pick a word you didn’t check? You have no idea whether it works
for that one. You will look a bit silly if your prediction turns out to
be wrong.
It sounds a bit tedious, but you should check every alternative word
that could be chosen does work. You have to be very patient to be a good
magician or a good computer scientist! Careful checking and attention
to detail really, really matter. Miss a step and things can go wrong.
Programmers call checking programs like this, testing. Programmers
spend more time testing programs than writing them. Testing is one
kind of evaluation of a program: evaluating if it works.
46 Conjuring with Computation
out of that first sentence: “thunder”, “lightning”, “in” and “rain”. What
appeared to be 12 possibilities for the volunteer to choose from is actually
only four different choices of onward paths: only four paths cross the
first dotted line. A similar thing will happen with any book. Long first
sentences may look like they are offering lots more choices but actually
only a few words will jump the first dotted line and escape as separate
paths from that first sentence. Once we are beyond the dotted line, no
new paths are created. We don’t have to check every word from that
point, only those we can land on from one of the existing paths.
So what happens to those four paths for Macbeth? Does that still
mean there are four possible bolded words we could end up on? Well, no.
As their paths bounce around, eventually by chance two paths hit the
same word. Then those two paths merge and stay together from then
on. Once they do, there is one less possibility for what the final word
could be. That is what happens in the diagram for Macbeth. The further
we go, the more likely it is that the different paths left will eventually
meet and so land on the same bolded word. For Macbeth, they all meet
31 words from the start. If you go far enough, it is virtually certain all
paths will join up whatever the book. You just may have to push the
bolded sentence back.
Bugs
Programmers call the mistakes they make bugs, and finding mistakes is
called debugging. So when double-checking your version of the trick, if
you found a word it didn’t work for (and so pushed the bolded sentence
Wizardly Book Magic: Testing 49
back to fix it), you are essentially debugging your version of the trick.
The word bug had been used before its use in programs as a mistake but
caught on in programming supposedly because one of the first times a
computer went wrong, it turned out it wasn’t because of a programmer’s
mistake, it was because a real bug (a moth) got trapped in one of the
switches! One of the first programmers, Grace Hopper, stuck the moth
in her notebook and the name stuck too.
All books?
Does it work for all possible books? How could we check that? We would
have to work through every book in turn and see if the paths for each did
join together as in Macbeth. If ever we found one that didn’t work, we
would have proof in that book that it does NOT always work. However,
each time we check a new book that it does work for, we’ve proved
nothing for certain about all books. We would have to keep testing to
be sure it works for the next book, and the next. . . . To prove that it
worked for every book (if it does), we would need to check every single
book ever written. That would take forever! Let’s hope it doesn’t always
work! We need some logical thinking.
If it doesn’t always work, we should be able to think of a way that a
book might be written so that at least two paths don’t join up. Well, if
two words are the same length, then they will jump to positions the same
distance apart. If that keeps happening, their paths will never meet. So if
we have a book that is something like the following all the way through,
then the trick won’t work.
Here, all the words are three letters long, so we end up with only three
paths straight away. However, those paths just bounce along together,
never joining up. Check it. If every word in a book is the same length
like this, the trick will fail to converge on a single word however far you
go. So without checking any real book at all, we have worked out that
50 Conjuring with Computation
it is possible for the trick not to work on some books. (Though we still
don’t know if anyone ever wrote a book like that.)
With similar reasoning, we can also see what will help make it work
quickly. The paths will generally join quickly, for example, if the book
uses lots of short words but that have different lengths: say lengths 2, 3
and 4 letters.
Avoiding disasters
Just as we originally tested the trick for all possibilities to prove it works,
programmers test their programs over and over again to make sure they
work, whatever happens.
When a computer program, perhaps a game you are playing, crashes,
it means you have just found a situation that the programmers didn’t
test. It isn’t your fault, it’s theirs. It is virtually impossible to write
complex software, without it containing mistakes somewhere. Why?
As real programs can be millions of instructions long, it takes perfect
attention to detail and logical thinking to get it right. It is just too easy
for the programmer to make a mistake, for a program to contain a bug.
Testers don’t find all the problems because there are just too many
possibilities to check them all. Testing real software is actually more like
checking the trick works for all the books in the world than checking
that Macbeth or The Wonderful Wizard of Oz works. There might be
millions, billions or more possibilities to check, and it just can’t be done
in any sensible time. Instead, programmers often test what they hope is
a good enough sample. They then cross their fingers and hope. That is
why programs are constantly being updated with “bug fixes”. It is only
when millions of people start using the software that it starts to get
properly tested and the bugs are found. They are using their customers
as their testing team.
Testing is an important part of any evaluation, and the testers have
to test as completely as possible. But testing alone just isn’t good enough
to find the problems in really complex programs. There are just too many
possibilities to check. There are also problems testing won’t find.
Better ways are needed.
Wizardly Book Magic: Testing 51
Conjuring
A volunteer picks a card and thinks about nothing but that card
throughout the trick. You show you can read their mind by telling
them their card, even though they told no one of their choice.
Computation
Real programs are too complex to test all possibilities in any
reasonable amount of time. A complementary method is to use
logical reasoning to give a convincing argument or proof as to why
a program or specific algorithm always works without doing any
testing. By doing both, you increase confidence that the program
really is correct.
The Trick
Tell a volunteer that they must think of a card. You are going to read
their mind and work out which it is. Explain they must think hard about
the same card all the way through the whole trick. First, you need some
cards for them to choose from.
53
54 Conjuring with Computation
Shuffle a pack of cards and then deal 21 cards face up into three
piles of seven cards. Deal one card at a time to each row, before moving
back to the first pile and so on, making sure all cards are visible.
Ask them to secretly pick any one of the 21 cards and think of it
and of nothing else. Explain that if they think of anything else or giggle
or laugh, it will make it really hard to read their mind. Go up to them
and stare at their forehead, concentrating, warning them not to giggle.
If they do giggle or smirk, say that spoiled it. Either way, say they seem
to be thinking of other things so it’s making it hard. You will need to
try again. Ask them to point to the pile their card is in, without telling
you which card it was. You collect the cards up and deal them out again
for another attempt.
When you collect the cards, collect them in their three piles, placing
the pile the volunteer pointed to in the middle. Then deal out the cards
again in the same way into three equal piles, across the rows.
Now as you couldn’t read their thoughts through the front, the front
of the skull is after all pretty solid, try the back this time. Remind them
not to move and especially not to giggle but just think of their card.
While they do, stand behind them and concentrate on the back of their
head.
Again, explain something is going wrong: perhaps they moved or
giggled or someone else made a noise. Say you almost had it: you think
you got the colour this time. You will need to try again.
Once again, ask them to find their chosen card and only tell you
which pile it is in. Collect the cards in the same way as before, casually
placing the new pile containing the chosen card in between the other
two piles and dealing them out a third time.
As their skull is obviously hard for thoughts to go through this time,
you will try through the ear. Again, they think of the same card and you
stare at their ear from one side. This time, announce you think you’ve
got it: it is so much easier to read the thoughts coming out of a person’s
ears, but say you want to triangulate and check from a different angle.
Have them point to the pile their card is in, collect the cards in the
same way with their pile going in the middle and deal them out again
across the rows for a final time.
The 21-Card Trick: Logical Reasoning and Proof 55
Now, go to their other side and stare at their other ear. Announce
that yes you’ve got it and go to the pile of cards. Stare at them saying
you’ve now just got to find it. Turn over all of the first pile saying it
is not one of them, checking with the volunteer that you are right so
far. Next, turn over the last pile: it is not one of them. That leaves
the middle pile. Start to turn most of them over, then pause, go back
and turn over the bottom three. Say that it is not any of those, then
quickly turn over the top three too, leaving the single middle card face
up. Announce that that is their card, asking them to confirm you are
right.
How It Works
There are two important steps to this trick. You must always deal out
the cards across the piles. You must also always take the pile they point
to and put it in the middle of the other two piles when you pick up the
piles. Do that and their card is guaranteed to end up as the middle card
of the middle pile after the fourth deal.
Before you read on, have a go at working out why that works.
Perhaps try it a few times. Clearly, no one can read your mind. We
are just following the steps: the same ones every time. It seems to never
fail (as long as the volunteer keeps thinking of the same card all the
way through and does not lie about the pile it is in, of course). To trust
the magic enough to do it for a live audience, we must be pretty sure it
always works, though.
Thinking Computationally
One way you could convince yourself is by doing the trick multiple times:
by testing. Would that convince you? Try it. What about the next time
you do the trick? Will the rules it is following work that time too? How
many tests are enough?
You could test every possible way the trick could start and see
how it ends. That’s what we did first with the Wizardly Book Magic
trick. In that trick, it was a bit boring, but it was possible, as there
were only 12 different situations to check. What are the possibilities
here? There are 52 cards in a pack and any 21 could be used in any
order. We would have to test all those different possibilities. There are
9,809,042,663,139,505,407,817,600,204,800,000 permutations to try.
That’s a lot of times to do a magic trick. For each, we would need
to test the case where the volunteer chose each of the 21 cards. So do
that 21 times over to be completely exhaustive. That is a lot of testing.
You would have to stop eventually. Or is there a better way?
Well, as with the book trick, we can do some simple logical
reasoning. Here, it is vital. Maybe you worked this part out already. We
don’t actually have to do all those permutations as it’s not what’s on
the card that actually matters to the trick. It is only where the chosen
The 21-Card Trick: Logical Reasoning and Proof 57
card is at the start that matters. If, from each starting position, the
chosen card always ends in the middle, the trick always works whatever
the cards actually are. Thinking that through, it means there are actually
only 21 tests we need to do, one for each place the chosen card could
start from. It is actually possible to do the trick 21 times to check what
happens in each case (if you’ve an odd hour to spare and are meticulous
about keeping track of what positions you check).
Prove it!
However, we can do better than that. In fact, I know it always works
without any tests. I’ve proved it (so I can sleep easy at night now before
a magic show). With a little logic and proof, I can be sure the rules are
right for all 21 possibilities without testing any.
This magic trick works because putting the pile containing the
chosen card in the middle of the other two piles and re-dealing the
cards in effect limit where their card can go. Let’s step through it.
After Deal Number 1: After the first deal of the cards into three piles,
the chosen card could be anywhere, but when we pick up the pile pointed
to, it goes into the middle seven-card pile. Why? Because we put that
pile (whichever it is) in the middle of the other two. There are now
only seven positions it could be in within the pack (positions 8–14 in
Figure 7.1).
After Deal Number 2: You deal the cards into three new piles dealing
across the piles. Where do those seven cards (numbered 8–14) from
the selected pile go from the middle pile? They end up in the middle
positions of each pile (see Figure 7.2). The chosen card has to now be
in one of those seven places. Those places are the fourth or fifth card of
the first pile; the third, the fourth or fifth card of the middle pile, or the
third or fourth card of the last pile. You ask the volunteer to say which
pile, and again put that pile between the other two. Wherever the seven
cards were, they are moved to the middle pile. They now must be one
of the middle three cards in the middle of the pack (as shown by the
arrows of Figure 7.2).
After Deal Number 3: After this deal, the chosen card has to be the
middle card of the first, middle or last pile (see Figure 7.3). Why? It was
58 Conjuring with Computation
Figure 7.1. The positions of the cards after the correct pile is pointed to and moved
to the middle the first time. The cards are numbered by positions 1–21 at this point.
Here, we draw the piles as if still laid out. In the trick, they would be in this order
in a single pile at this point. The selected card will be ultimately in one of the
positions 8–14 in the collected deck.
Figure 7.2. The positions of the cards after the second deal. A similar pattern follows
if it was in one of the other piles. If the chosen pile is not in the centre, then when
the piles are collected up they will have been moved to one of the positions: 8, 11
or 14.
The 21-Card Trick: Logical Reasoning and Proof 59
in one of those middle three cards in the middle pile. By dealing across
the rows, they all get dealt one after another, one to each pile. Once
you are told which of the three piles has the card this time, you already
know exactly where their card is. However, when you collect the piles,
whichever of the three it is, it is moved to the middle (as shown by the
arrows in Figure 7.3).
After Deal Number 4: The fourth deal simply moves the chosen card
to the middle card of the middle pile (see Figure 7.4), which really is
just for effect.
What we have just done is give a convincing (we hope) argument
that the trick, or actually the underlying algorithm, always works. That
is all that mathematical proofs are: convincing arguments where there
is no room for doubt if you follow the detail. Here, we were just proving
that a trick works, but in doing that we have shown we can prove
algorithms are correct, more generally. Now, computer programs are
based on algorithms. So we can do the same thing for a program and
prove it works in a similar way. It is very important that programs always
work too.
Figure 7.3. The positions of the cards after the third deal. On moving the selected
pile to the middle, the selected card will have ended up at position 11.
Figure 7.4. The positions of the cards after the fourth deal. The chosen card that
was collected into the middle position stays there as the cards are dealt across the
rows. It ends in position 11.
The 21-Card Trick: Logical Reasoning and Proof 61
Conjuring
A volunteer picks a card from three, then moves cards around, all
with your back turned. After more mixing of the three cards, you
can still name the card chosen.
Computation
One form of logical reasoning is reasoning by cases. This involves a
form of decomposition: splitting your argument into several cases
that together prove the whole. It is naturally used at decision points
in a program, so with selection instructions. There is one case for
each path through the code.
The Trick
Three-card Monte, or Find the Lady, is a popular game which is
often used to swindle people. Three cards are used, one a red queen.
63
64 Conjuring with Computation
The cards are moved around, and the spectator tries to find the queen,
normally unsuccessfully. Whilst this game can be rigged by sleight of
hand, we present a variation, Mental Monte, that makes it more magic
than scam.
Put three cards face up on the table and ask your volunteer to choose
one mentally and remember its position. You turn your back and ask the
volunteer to point at their card, to show the audience which card they
chose. Then have them silently switch the two other cards, the cards
not chosen. They then turn the three cards face down. When you turn
round, you ask the spectator to switch pairs of cards in the row of three
as many times as they wish, so mixing up the already mixed-up cards.
You then turn the cards over. Even after this multiple mixing, you are
able to read the volunteer’s mind and reveal their chosen card.
Now, you have the basic idea, how about an alternative presenta-
tion? You can perform this trick with three train tickets for three different
destinations. If you don’t have the tickets, just write three places on three
bits of identical cards. In this presentation, you can talk about changing
the destination, and about long complex train journeys. This gives a
natural justification for moving the face-down tickets around. It also
gives a strong finish when you reveal the name of the mystery-selected
journey.
How It Works
The key to this trick is that you memorise the card in the middle of the
three when they are face up at the start. When you turn back around,
you need to casually keep an eye on the position of the face-down card
in the middle, and track it as it is moved around in the swaps. Once the
swapping is finished, you flip over the card you have been tracking.
There are a few possibilities. If the card you flip over is the card you
had originally memorised, then you correctly announce it as the chosen
card. If it is not that card, then the chosen card is neither this card nor
the one you memorised. Flip over the others, and announce that it is
the other card (the one neither tracked nor memorised).
To understand why this works, we will need some logical thinking . . .
Mental Monte and the Trains of Thought: Reasoning by Cases 65
Thinking Computationally
To use logical reasoning to be sure this trick always works, we need to
work through the separate cases that can occur. The algorithm has a
selection statement, that means you do something different depending
on what the card you turn over is. That IF statement gives us two cases
to consider. The case when the card flipped over first is the memorised
card, and the case when it isn’t. We must therefore think through those
two cases.
66 Conjuring with Computation
Figure 8.1. If the third card is chosen, the other two are swapped, one being the
memorised one, the other the tracked one.
neither is the chosen one. The card they picked was therefore neither the
card you memorised (2) nor the one you just turned over (1). It must
be card 3.
As the situation is symmetrical, the same reasoning applies if they
instead chose card 1. This time they swap 2 and 3 giving the order 1,
3 and 2 and because it is in the middle when you turn back, you track
and eventually turn card 3 over. Again the card they picked was neither
the memorised card (2) nor the one turned over (3). It must be card 1
(see Figure 8.2).
Our overall logical reasoning is a proof by cases, with one case for
each possibility resulting from the IF statement. This idea is the key to
logical thinking around algorithms that use selection. You must consider
68 Conjuring with Computation
Figure 8.2. If the first card is chosen, the other two are swapped, one being the
memorised one, the other the tracked one.
each of the cases. The structure of the algorithm describes the structure
of the logical thinking you must do.
Conjuring
Stare at the picture of the Cyclops’ eye, and it starts to magically
move. Don’t look for too long or you will be forever trapped by its
stare!
Computation
When writing software, you have to understand that our brains
have limitations that lead us to make mistakes. You must write
the software (and especially its interface) in a way that works with
those limitations. If you ignore our limitations, you will produce
bad software.
The Trick
This trick is just a few steps: warn everyone that they should not look
for too long or they will be trapped forever by the Cyclops’ stare, have
everyone look at the picture of the Cyclops’ eye (Figure 9.1) and move
71
72 Conjuring with Computation
Figure 9.1. The Cyclops’ eye was created by Kelly Burrows based on an illusion
invented by Japanese artist, Hajime Ōuchi, in his book Japanese Optical and
Geometrical Art.
their head slowly from side to side. Everyone who looks at it will see the
central part of the eye start to hover and float around.
How It Works
As with other optical illusions, this is just your brain playing tricks on
you. Your brain is fooled because it separately tries to work out what is
going on with the horizontal and vertical patterns. As your eyes move,
the strong patterns seem to be doing different things, so your brain
makes sense of this by assuming they are moving separately.
The Cyclops’ Eye: You Cannot Trust Your Eyes 73
Thinking Computationally
This is our first indication that to be good at developing usable software
you need to be much more than a logical, algorithmic thinker. You must
understand the way our brains work, and in particular their limitations.
As we will see, some of the limitations and simplifications our brains
make mean we are highly likely to make mistakes using poorly designed
software. When creating software, you must take these limitations into
account when you design the user interfaces of software (the parts
that people see and interact with). Good computational thinkers need to
understand people too. Most software now uses some sort of graphical
user interface: one that tries to make tasks easier to do based on the
limitations of our brains. They can be well designed, so easy to use, or
poorly designed, so much harder, with mistakes likely.
74 Conjuring with Computation
Conjuring
At the snap of your fingers, you make boxes clearly heavier than
they were only a moment earlier.
Computation
Our brain’s limitations are not just about what we see, but about
our other senses too. Multimodal software that makes use of our
other senses can make the software easier to use, not least for
those with disabilities. However, the software developer must take
into account the limitations of the senses being used.
The Trick
Beforehand, take three empty playing card boxes. Stuff the middle one
with coins to make it heavy and tissues to make sure the coins don’t
rattle around.
Place the three boxes in a pile on the table with the heavy one in
the middle of the pile (see Figure 10.1). Ask a volunteer to pick up the
boxes all at once to feel how heavy the pile is, then put them down.
75
76 Conjuring with Computation
Figure 10.1. First lift all three boxes, then just the top two.
Now explain you can change their weight at the click of your fingers.
Click your fingers and ask them to pick only the top two boxes up. They
will feel immediately heavier than the three did.
How can three boxes be lighter than only two of them?
Repeat this trick, having different people try it in turn.
How It Works
It is all in the mind. It is like an optical illusion, but an illusion of touch
rather than of sight. Again, shortcuts made by our brains in making
sense of the world are being fooled. This is one example from a whole
Magically Weighted Boxes: Your Brain Cannot be Trusted at All 77
Thinking Computationally
Illusions can occur with all our senses, not just sight, though for similar
reasons. We really do have to accept that our brains have limitations
in the way they process information from the world. This matters when
designing multimodal interfaces that make use of other senses. For
example, increasingly computers are being built into physical objects so
that manipulating those actual objects in the world acts as an interface
to computers behind the scenes. Recognising the limitations of our brains
is not just about the way we design graphical user interfaces that are
based on sight.
Conjuring
You make a robot appear and disappear from a jigsaw as everyone
watches.
Computation
Interaction design principles are rules designers follow to help
ensure software is easy to use. One of the simplest rules is to keep
the interface simple. The more controls like buttons and drop-down
menus you have, the harder a system is to use. One way to keep it
simple is to avoid feature bloat. Rather than adding every feature
you can imagine, design just so that key tasks are supported and
designed to be easy to do.
The Trick
Download a copy of the magic jigsaw (see Figure 11.1) from
https://conjuringwithcomputation.wordpress.com
79
80 Conjuring with Computation
Cut out the six pieces of the jigsaw along the dashed lines. Complete
the jigsaw by placing the short piece with the grey robot in the top left
corner as in the original. With the audience, count the robots out loud,
and make sure everyone agrees there are 17. There is also a robot dog
and some Hexapus monsters (which are like an octopus but with only
six legs).
Now, mix up the pieces and then rebuild the jigsaw, but this time
with that first short jigsaw piece with the grey robot in the top right
corner. The other pieces still fit together but in different positions. You
are building it back with the pieces in the top two layers swapped over.
As you mix up the pieces and rebuild it, talk about how you have to keep
an eye on robots. As with WALL-E, they have a tendency to disappear on
their own little excursions once they start to develop a mind of their own.
Now, with the audience, count the robots again. To everyone’s
surprise, there are now only 16 robots. The 17th has gone, leaving no
trace. Ask which robot has disappeared and where it has gone. Did it
wander off? Did a Hexapus eat it, or did it teleport away?
Once everyone is sure that there are 16 robots, shuffle the pieces
and put them back the original way. Let everyone see there are 17 robots
again. Their challenge is to work out which robot disappeared and how
it did it.
The Teleporting Robot: Keep It Simple Stupid 81
If they spot that you rebuilt the jigsaw in a different way, point out
that that explains nothing. It is the same pieces with the same heads
and bodies. They are just good recycling robots who swap their parts
rather than throwing them away. How can one disappear just because
they swapped bits?
If someone claims it is a particular robot that disappears, then have
them put a finger on the two parts of it, move the pieces around with
them pointing at the same two parts and they will see that both parts
are still there. That demonstrates their robot cannot be the one that
is gone. How could any part of a picture vanish from a physical jigsaw
anyway!
How It Works
This works because of a limitation in the way our brains process what
we see. Look at the complete jigsaw. There are clearly 17 robots. When
you put the jigsaw together the other way, there are definitely only 16.
You’ve fooled yourself!
Error!!! Can’t Compute!
This trick is so powerful that you cannot see the robot disappear
even when you know what is happening. If you try and work out which
specific robot has gone, you have no chance of working it out. It’s not
one robot. All are involved.
82 Conjuring with Computation
Figure 11.2. A simplified version of the effect used in the magic jigsaw. Slide the
top half so each line joins with the bottom of the next along so becomes taller. You
are left with one fewer line.
differences by eye. The new robots look “about the same” as before.
“About”, yes, but “the same”, no! That’s why we invented rulers!
The robot jigsaw design is more cunning than the lines though.
Rather than just having the robots in order, where a simple slide of the
top section would make the remaining robots taller, the jigsaw design
has lots more confusing detail for our brains to take in but also weaves
the vanish across different parts of the picture. That makes it hard to
take in everything that you need to see in order to see what is actually
happening even when you know.
Presentation matters
Magic is about more than algorithms, about more than the self-working
part. You have to get the presentation right too.
Even the most cunning trick can fail to be magical without a good
presentation. The two versions of the trick, one with the lines and one
84 Conjuring with Computation
with the robots, use the same mechanism. It is the presentation that
makes one impossible to see what is happening, whereas in the other it
is fairly easy. Here it is done by making the picture more complicated.
There is too much going on for anyone’s brain to take it all in.
Magicians rely a lot on understanding how our brains work, and
particularly how they get things wrong. They rely on an understanding
of cognitive psychology: the science of how we think. The key point is
that our brain has limited resources. For example, we can only pay close
attention to one place at once and only remember a few things at once.
Thinking Computationally
Presentation matters
Computer science, and especially writing software, is about more than
algorithms too. You have to get the presentation right, just like a
magician.
Rather than have your software try to do everything for everyone, focus
on designing in a way that makes it easy to do the key tasks one group of
people want to use it for. If other people want to do completely different
things, then you design a different interface for them.
The reason many people think they are not good at using computers
and other gadgets is not because those people are stupid. It is because
the people who designed the software or gadget did not think hard
enough about people. They made the programs work more like a magic
trick than a usable program, so hard to follow and easy to get wrong. If
you become a programmer or interface designer, make sure your designs
are really easy to use, so no one makes mistakes. Make sure they are the
opposite of a magic trick.
Conjuring
By reading the mental impression of it in their head, you are able
to draw a picture that is selected randomly from a book of images
by an audience member.
Computation
An abstraction is a simplification of something. Computer scien-
tists use abstraction a lot, both as a way of making a complex piece
of software easier to understand and to help make it possible to
write in the first place. Similar ideas can be used in magic.
The Trick
You pass a small notebook around the audience for them to inspect.
In it, you have written, on numbered pages, a series of random objects
that came to you “in a dream”. The spectators check that each page
does have a different and unrelated thing written on it. The task is now
to choose one.
89
90 Conjuring with Computation
How It Works
This effect relies on two separate elements: a mathematical technique
called the 14/15 stack, used to force page numbers, and the notebook
itself.
To create the stack (before doing the trick), you must first
remove the Ace of Hearts and the Ace of Spades from a normal pack of
cards. The fact that this pack only has 50 cards rather than 52 won’t
be spotted. You now need to carefully arrange the remaining cards in
the following full-deck stack order (where C means Clubs, D means
Diamonds, H means Hearts and S means Spades). It should end up with
the 7 of Diamonds at the bottom of the deck, with all the cards face
down.
7C 8C 6D 9S 5C 10H 4D JS 3C QS 2D KS AC KH 2H QC 3D JH 4S
10C 5D 9C 6S 8S 7S 8D 6H 9H 5H 10D 4C JD 3S QH 2S KD AD KC
2C QD 3H JC 4H 10S 5S 9D 6C 8H 7H 7D
This order is specially created to ensure that every adjacent pair
of cards, including the last and the first, add up to either 14 or 15.
Cutting the pack does not actually change the cyclic order, it just starts
it in a different place. That means when the volunteer cuts the pack,
the bottom two cards of the resulting pack will still add up to either
page 14 or 15.
Pre-booked Picture Magic: Abstraction 91
You now have a way to use a free cut of the pack to force either
page 14 or 15. You next need a way of dealing with the problem that
there are still two possible outcomes. That’s where the way you set up
the notebook and make the prediction comes in.
On your prediction pad, you sketch a large circle, with a smaller
circle in the middle (see Figure 12.1). This prediction and its simple
design allow you to claim it represents either of the two things you write
on pages 14 and 15. Use your imagination for the objects you name on
pages 14 and 15 of the book. For example, you could write Doughnut,
Car Tyre or Porthole. That means you are covered whichever of the two
values the spectator uses.
Remember that the spectator will tell you their choice before you
show your prediction, so you can be ready with the right convincing
argument about how the mental impressions you got made you roughly
but correctly draw the answer they gave you. If you really could read
minds, it would be difficult, so the fact you didn’t get the sugar sprinkles
on the particular doughnut they were visualising will be forgiven.
Use a cheap spiral-bound notebook for the book, as it allows for
easy flipping of the pages. you need to create the illusion of free choice.
Therefore, fill the book with enough random and unrelated things to
make it convincing. It should certainly include a lot more than 15 things.
Write the names clearly in block capitals so they are easy to read and
92 Conjuring with Computation
clearly label the pages in the same place at the bottom, as they need
to easily find the page number. Importantly, make sure that you use
a separate sheet of paper for each object. Don’t write the things on
facing pages in case your spectator gets suspicious when they see that
the outcomes on both pages 14 and 15 could match your prediction.
There are lots of alternative presentations for this trick. You could,
for example, use world landmarks, either writing the names of the
landmark or creating a book with pictures of the famous sites. Then
your prediction could be an inverted V shape pointing upwards and a
line underneath signifying the ground. The landmarks on pages 14 and 15
could then be The Eiffel Tower and Mount Everest. Both are arguably
represented by your prediction. Come up with your own version: get
creative!
7. Have the volunteer take the bottom two cards and add them up,
remembering the total.
8. Give them the book.
9. The volunteer goes to the other side of the room, finds the page
of their remembered number and silently reads the word on that
page.
10. The volunteer closes their eyes and visualises the object given on
the chosen page. While they do so, you pretend to focus on their
thoughts, telling them you will draw what is coming through to
you from their thoughts.
11. Draw two concentric circles on your pad.
12. The volunteer tells you the word they were thinking of.
13. Show them what you have drawn and point out you drew the
object they were thinking of.
Thinking Computationally
Sketches are an abstraction, a simplification of the real thing. Your
prediction is an even simpler version still. Abstraction is all about giving
a simplified version of things, but that still captures the essence of
the original. The abstraction (like the drawing) discards detail. A good
abstraction will only throw away detail that is unnecessary for the task
at hand.
Computer scientists use abstraction a lot, though they have very
precise versions of it, that we look at in more detail in later chapters.
For now, think about it as hiding or discarding detail that is unnecessary
for the task at hand (a doughnut is still recognisable as a doughnut even
without the sugar sprinkles).
The most important kinds of abstraction we will look at are about
program instructions themselves, but computer scientists do use this kind
of drawn abstraction in one situation: icons. Icons are small pictures used
to represent programs. The image is a simplified picture representing
what the program is or does. For example, a trash can represents the
deletion operation on most graphical user interfaces. Drag a file to the
trashcan picture and it is deleted. Icons are very simplified versions of
the thing they represent, but to work well they must catch the essential
94 Conjuring with Computation
detail and be instantly recognisable. The best are really good visual
abstractions.
The magic trick, by contrast, relies on a poor abstraction in the
sense that a pair of circles can represent two different specific nouns in
the book (though it is a perfect abstraction for its purpose: to make
the trick work). Graphical user interfaces need much clearer ones than
this. If you could mistake an icon for meaning two different things, it
would be a poor icon. Many modern icons are very confusing in this way.
Abstractions generally lose information, but a good abstraction will not
lose anything important!
Trained Rice
Decomposition and
Procedural Abstraction
Conjuring
You train rice so that unlike normal rice it can grip onto a
chopstick.
Computation
Programs that are millions of lines long cannot be written without
ways to simplify the task. The most immediate way is to use
decomposition: split the big difficult program into lots of small
meaningful parts that can be treated as distinct problems (i.e., mini
programs) to solve. These separate parts are called procedures. By
giving each a name, we can use them later just by giving their
name.
The Trick
For this trick, you need two small plastic drink bottles with narrow necks,
chopsticks and uncooked long-grain rice.
95
96 Conjuring with Computation
Figure 13.1. The bottles are filled with rice and a chopstick is inserted.
You must prepare the bottles by filling them with long-grain rice
before you present the trick. Fill the first with rice and leave it as is.
Pushing a chopstick into it, the chopstick should go in and then come
out easily (Figure 13.1).
With the second bottle, after pouring in the rice, repeatedly tap the
bottle on the table so that the rice packs closely together. As you do,
you will create more space at the top. Keep adding more rice to fill it up.
When it no longer packs down, try pushing a chopstick into it. It
should be harder to do than with the first bottle. If it is ready then,
when you pick the chopstick up, the whole bottle should lift and stay in
place. If not, keep tapping the bottle packing the rice even closer.
Remove the chopsticks, and you are ready.
Now for the performance. Announce that you have discovered a
strain of rice that can be trained to grip things. Show the two bottles
and explain that you have normal rice in the first bottle but trained rice
in the second.
Trained Rice: Decomposition and Procedural Abstraction 97
Place the chopstick in the first bottle and demonstrate that it does
not grip the stick. Allow a member of the audience to try. Now place a
chopstick in the second bottle and moving your mouth up close, whisper
to it over and over “grip the chopstick, grip the chopstick. . .” Then,
slowly lift the chopstick and watch the rice grip and the bottle rise
with it.
How It Works
The secret of this trick is just the laws of physics and, in particular,
friction. In the first bottle, the rice is not tightly packed so the grains
easily slip past each other. In the second because they are pushed tightly
against each other and the edge of the bottle, there is more friction
between the rice grains and between the rice grains and the chopstick.
This friction is enough to hold the chopstick in place.
Thinking Computationally
Here we have decomposed the algorithm into two subparts: the
preparation and the performance. In fact, we did that informally in earlier
tricks, though without naming the parts and without giving separate
explicit instructions of the order to do them. Decomposition is just the
idea of breaking up an algorithm into smaller, easier-to-manage parts
like this.
Here, we are using decomposition and a programmer’s version of
abstraction (referring to the parts by name) purely as a way to organise
the way we write the algorithm. This is to make it easier for a human
(a magician or a programmer) to understand the instructions.
This matters a lot when writing large programs. Writing programs is
about writing instructions computers can follow. Writing good programs
is also about making them easy for humans to understand, so follow too!
If you don’t, then they are likely to have more bugs and, when changes
to the program are needed, those changes will be harder to do.
Decomposition into meaningful parts is one of the most important
ways of making programs easier for humans to understand. Often
programs or even individual algorithms have a setup part, where data is
collected or organised, followed by the part that does the actual work,
and this is usually a good way to decompose the program. Both parts
have very clear tasks to do. What you don’t want to do is decompose
Trained Rice: Decomposition and Procedural Abstraction 99
a program you are writing into arbitrary parts where the separate parts
have little real meaning.
There are three important aspects to what we are doing here.
The first is decomposing the algorithm into parts. The second is to
give those parts a unique name that can be used to indicate when
those instructions should be followed. The final aspect is to use that
name instead of the actual detailed instructions when we do wish it to
be followed. These latter points combine to give another key tool in the
computational thinking toolkit, that of abstraction or hiding of detail.
We previously saw abstraction in drawings. Here, we are doing a more
technical kind of abstraction. This naming of parts of an algorithm is
called procedural abstraction. It involves using a name in place of a
group of detailed instructions (hiding the detail of the instructions behind
the name). These groups of instructions are known as procedures. We
will explore procedural abstraction more in later chapters.
A Magical Diversion
Magicians can think of the performance of great tricks as being
decomposed into three parts1 in addition to the preparation:
The Pledge is where you set the scene, giving the effect its meaning;
The Turn is where you perform the trick leading them along with
the story;
The Prestige is where you reveal the twist at the end, the delight!
1
Named by author Christopher Priest in his novel, The Prestige (1995).
100 Conjuring with Computation
Conjuring
We develop a new version of the Invisible Palming trick, replacing
the palming part with a top hat teleporter.
Computation
Decomposition, combined with procedural abstraction, allows us
to build large programs from small. Once a procedure is written,
we can forget about the detail of how it is actually implemented.
This also means that we can later completely change the way a
step is done without it affecting the rest of the code that uses it.
The Trick
This trick is exactly the same as Invisible Palming (Chapter 1) except
that you swap in new steps for those where you pretend to move the
card from one pile to the other. Instead, you “move” the card with
your magical top hat, into which the card disappears and reappears.
At the point where you are to move the card, place your top hat over
the pile with the extra card. Tap it with your magic wand and say the
101
102 Conjuring with Computation
magic words. Move the hat to the other pile, tap it once more with your
magic wand and say the magic words once again.
How It Works
The mechanics of the trick are exactly the same as they were before.
We have actually just changed the presentation of one part. It doesn’t
change how or why the trick worked. The substituted step does exactly
what the original did (nothing to the cards, but a lot to make the
audience think something else is going on).
Thinking Computationally
We have used our procedural abstraction (giving a group of instruc-
tions a name), to make it easy to swap a new implementation for one
of the steps, i.e., one of the procedures. The key point is that the main
instructions do not change at all. As long as the new steps have the
same effect as the old, nothing else has changed.
The same can be done with programs. If the program is split into
procedures, then we can take any of those procedures and change how it
works as long as we do not change what it does. The rest of the program
is then guaranteed to still work, without any change to anything but the
instructions in that one procedure.
The use of procedures makes a program much more maintainable
because of this. It is easier to improve. It is simple to change the
implementation of this part of the program in that when doing so we
do not have to worry about any of the rest of the program. We know
nothing else has to be changed. We might want to do this, for example,
to make a critical part work faster or use less storage space.
Furthermore, if the procedures are used multiple times, improving
the procedure improves it everywhere it is used throughout the program
with no extra effort: we do not need to track down all the places it is
used as no changes there are needed.
104 Conjuring with Computation
There are many different kinds of false shuffles and similar well-defined
magical manoeuvres that have a specific effect. Just by giving the name
of one of these steps, a magician would know what was meant and be
able to use it as part of a trick.
This corresponds to procedural abstraction where clear self-
contained pieces of code are created for use in lots of programs. Once
created, they are incorporated into any running program (or “called”)
with a command that just gives their name.
We describe a series of shuffles and cuts. In some, you really do
mix the pack; in others, you just appear to shuffle it, but actually leave
either the whole pack or an important part of it, unchanged.
This page intentionally left blank
Chapter 15
Overhand Shuffle
Procedures
Conjuring
This shuffle really does mix up the cards in a random way.
Computation
When we have a meaningful task that we will want our instructions
to refer to lots, we package the instructions for that task as a
procedure, give it a name and refer to the name, using it as an
instruction (call the procedure) at the point we want the task to
be done.
107
108 Conjuring with Computation
second hand. The whole thing is then repeated several times until the
cards are thoroughly shuffled.
It is a legitimate shuffle and does mix the cards. However, there
are various ways it can be performed that keep parts of the pack in the
places you want them while still looking like a real shuffle, as we will see.
If you haven’t done an overhand shuffle before, practice it for a while,
and once you are reasonably good at it, you can start to get sneaky.
Thinking Computationally
The overhand shuffle is a generally useful card algorithm. You might
use it in a range of tricks where the order of the cards doesn’t matter
at the start. We don’t want to have to explain it in the instructions to
tricks every time we need it though. Instead, in the English description,
we might just say “Start by shuffling the cards”. More precisely and
rigorously, the first step of the trick’s algorithm might just be:
1. DO an Overhand shuffle.
and you, the reader, now know precisely what we mean, anytime we
mention its use!
The definition itself is not telling you to do the trick now (though
you may of course want to practice it and check it works now). It is not
a trick on its own. Elsewhere you will be told to do it when needed with
an instruction like the DO instruction above as part of an actual trick.
That is the point when the instructions are followed. Programmers refer
to it as the procedure call.
This is decomposition and abstraction being used in tandem again.
We have split off a part of the instructions and given them their
own name. The difference here to our previous use is that we have
decomposed our trick in a way that gives us a generally useful procedure
that will be used over and over again in lots of tricks, and possibly even
several times in the same trick. The benefit is thus much more than
organising that one trick. At a stroke, we have set up a decomposition
to be used in tricks that we haven’t even thought of yet. Whenever we
are planning a new trick and need a shuffle, we no longer need to worry
about how it is done at all. We do not need to think about the algorithm
of the shuffle, just know what it does: i.e., mix up the cards.
This is a really important use of decomposition and procedural
abstraction. When deciding how to decompose, one important question
to ask is: “can I create a generally useful procedure here.” If you can,
it is likely to be a good decomposition. In the software world, classic
“library” procedures like this are often provided ready written to include
in your programs as predefined procedures. Examples include code to
search a list of data, sort data into numerical order, print a message to
the screen, read some data from the keyboard, write to a file, and so on.
The idea is used everywhere in programs. Most languages include lots
of libraries of previously written, predefined procedures. Like our shuffle,
the instructions can be written down once, made sure they always work,
and then referred to by name, in later programs we, or other people,
write. When writing instructions that use them, we no longer have to
think about how that code actually works, we just need to know what
it does so that it is right for our current purposes.
110 Conjuring with Computation
Conjuring
This shuffle leaves the pair of cards that are at the top and bottom
of the pack in place, just swapped.
Computation
It’s important that it’s clear what each procedure does, so we
know when to use it. This can be done with English comments
or using maths. Important things to be clear about are both what
it ultimately does and any assumptions that must hold before the
procedure is called if it is to work correctly.
The Trick
Take the deck in your right hand for a standard overhand shuffle. Peel
the top card off as a single card, depositing it in the empty hand
(Figure 16.1(a)), then proceed normally pulling piles of cards off the
pack in a standard overhand shuffle way and dropping them in front
of that first peeled card. Continue doing this until you get near the
111
112 Conjuring with Computation
(a)
(b)
Figure 16.1. To do a Swapped Ends Overhand Shuffle, (a) first peel off the single
top card and then drop the rest of the pack as in a normal shuffle in front of it. (b)
After doing a normal shuffle for the middle of the pack, leave the last card on its own
to be dropped to the front in the last step.
end of the pack. At that stage, pull the last few cards off one by one.
In particular, peel off the very last card on its own (Figure 16.1(b)).
a. Peel off a single card with the second hand’s thumb into the
front of the pile forming in that hand.
{The first and last cards have swapped positions.}
How It Works
The first step moved the top card to the bottom. Peeling the last card
from the stack alone means that the final card, which was the original
bottom card, is now deposited on the top. Cards in the middle have
been mixed a little. The top and bottom pair of cards are still at the top
and bottom, just swapped over.
Thinking Computationally
An important property of well-written procedures is that they have a
clear and clean specification. That is you can easily and precisely say
what they do (as opposed to how they do it). For example, the algorithm
of this shuffle guarantees, whatever the initial order of the pack, to swap
the positions of the first and last cards. That is the specification of the
algorithm. Nothing is guaranteed about any other cards. That is left
unspecified.
Specifications come in two parts. A precondition (what must be
true before the procedure is followed for it to work) and a postcondition
(what will then be true afterwards). The precondition here is basically
that the cards can be in any order (though it is the case that cards of
interest are in the first and last positions). The postcondition is that
those two cards have been swapped.
A precondition says what must be true for it to work. We have
specified that anything can be true by writing “Nothing is assumed”.
Computer scientists specify this by just writing “true” (stating that all
that matters is “true must be true” which is always the case so not
imposing any restrictions!).
Details matter though, and actually here perhaps there could be a
precondition. We have referred to the deck of cards in the algorithm, and
an implicit assumption is that this is a normal deck. The shuffle works
114 Conjuring with Computation
with any cards so if we wanted to take that into account explicitly, then
something to note as a precondition is there have to be at least two
cards in the deck. The precondition could then be
Conjuring
This shuffle leaves the pair of cards that are at the top and bottom
of the pack in their original place.
Computation
Once you have created generally useful procedures and made sure
that they work, you can start to build new procedures out of them.
You build new reliable code from existing reliable code, rather than
creating everything from scratch.
The Trick
In this trick, you just do the Swapped Ends Overhand Shuffle twice.
117
118 Conjuring with Computation
How It Works
As a single Swapped Ends Overhand Shuffle swaps the places of top
and bottom card, doing it twice puts them back to where they started.
Cards in between change position.
Thinking Computationally
Once we have created a procedure, we can use it as though it was a
core, primitive instruction provided by the language. In particular, we
can build new, generally useful procedures out of simpler ones as here.
From the original false shuffle, we create a new false shuffle that does
something slightly different (leave the first and last cards alone). We are
building layers of abstraction. First, we create primitive procedures.
We then no longer need to worry about how they work when designing
more complex procedures. As long as we know our basic false shuffle
swaps the first and last cards, we don’t care how. That knowledge is
enough to be sure that doing it twice will leave the first and last cards
alone. Now, knowing exactly what our new shuffle does, we can use it
when designing new tricks, without worrying how the original basic step
or the new composite one is actually done while inventing the new trick.
The details of lower layers of procedure are hidden in the instructions of
later ones (see Figure 17.1). This layering of procedural abstraction is
key to creating really large programs that do actually work. Without it,
the detail quickly becomes too complex to avoid mistakes.
Really well-designed programs are built out of a set of primitive
procedures. A more complex set of primitives are created from those,
Ends Overhand Shuffle: New Procedures from Old and Layers of Abstraction 119
Figure 17.1. Building new components from existing ones in layers of abstraction.
then more complex ones from those, and so on. Groups of such
primitives are put into libraries to be incorporated into any program
that needs them.
Of course, we are talking here about the process of designing new
tricks (and programs). You do, of course, need the detail when actually
doing a trick using such a shuffle, and those details are there, in the
named instructions, when you need them. When practising a trick from
a book like this that needs a false shuffle, for example, you just jump to
the right page for each step, and when done jump back.
However, this multilayered decomposition probably helps a lot to
learn a trick too. You can first learn the basic false shuffle. Only when
you have got that down perfectly, and the trick is “loaded” into your
brain, do you start to practise the more complex version that uses it
twice. Thus this sort of abstraction, this computational thinking trick, is
helpful for humans following instructions, not just when writing programs
or designing instructions.
120 Conjuring with Computation
Conjuring
This shuffle leaves the top cards of the pack in place.
Computation
The procedures we have seen so far give a name to code that
does a single task. Parameters allow one procedure to do a whole
range of related things by providing the information that makes the
difference at the start. They allow a procedure to be generalised:
that is work in more general situations rather than just one
specific one.
The Trick
Pick up the deck as for a standard overhand shuffle. However, press the
bent second finger of the hand holding the cards up against the back of
the deck. Peel off a pile of cards from the front into the other hand as
in a normal shuffle. Make sure this first pile contains all the cards you
want to ultimately leave in place.
121
122 Conjuring with Computation
Continue with the shuffle, but as you drop the second group into
your other hand, pick back up the first cards at the back of the original
pack by gripping them between the thumb and other fingers.
You now have two groups of cards in your original hand: the remains
of the pack and, behind them, those you split off first with your second
finger between them. The new small pile you split off second is in your
other hand.
Push the tip of your second finger that is now between the original
last card and original first card, up against the original top card. With
this finger, you keep the former top cards in place at the back of the
pack in your original hand. Continue normally pulling piles of cards from
the pack in a standard overhand shuffle way. When you place the cards
from the bottom of the original pack into the other hand, you will be
left holding the group of cards that came from the top. Slide them down
at the front as though they had been at the back of the pack all along.
How It Works
The first cards that you peel off the front of the pack are held at the
back through the shuffle. They are put back at the front at the end.
It therefore guarantees that those cards are still on the top of the deck
at the end. By peeling off more than the number of cards you want
preserved in place, you can ensure that definitely that many cards are
left alone by the shuffle. You can do this shuffle as many times as you
wish, always peeling off a similar number to start.
3. Peel off a group of more than n cards from the front of that hand
dropping them into the other hand.
4. Move the rest of the pack in front of these, peeling off a second
group of cards from the front into the second hand, while pushing
the middle finger against the top card of the original group.
5. Lift off the rest of the pack with the original cards held at the
back, leaving only the second group behind.
6. DO UNTIL all cards are in the second hand apart from those being
held at the back.
a. Peel off a small packet of cards from the front of the original
deck into the front of the pile forming in the other hand.
7. Drop the remaining cards from the original hand to the front of
those in the other hand.
8. Pick up the deck in the original hand.
{The deck is shuffled, but the positions of at least the first n cards are
unchanged.}
Thinking Computationally
This procedure can be used in more varied situations than the previous
false shuffles. This shuffle doesn’t always do exactly the same thing.
Some tricks might need only the top card to be left in place. Another
might need the first dozen left alone.
We have provided a number n to the procedure that is used to guide
how many cards to leave at the front. The n can stand for different
actual numbers depending on what a particular trick needs. This is a
parameterised decomposition. We have created a description of a
general version of the shuffle where, when we use it in a trick, we specify
how many cards are to be left alone using the parameter, n.
In our algorithm, the parameter, n, is the number of cards to be left
in place. Notice we have a precondition that n is less than 20: (n < 20).
This is essentially saying that you should not rely on this shuffle to leave
close to half or more of the pack in place. If you try it, it may work, but
it probably won’t look so convincing. For it to be convincing, keep the
number of cards you preserve to be less than 20.
124 Conjuring with Computation
I do not have to invent new false shuffles for tricks that need more
cards left in place. Likewise, by generalising procedures, we save coding
work later.
Conjuring
This false shuffle leaves the first quarter or so of the pack
unchanged.
Computation
If two procedures do the same thing, then you can swap one for
the other, and everything else should still work. This allows you to
make small changes without needing to change or check the rest
of the code.
The Trick
Hold the stacked deck in the overhand shuffle position. You want to
ensure the top of the stack (say the top 13 cards) remains intact. On
the first part of the overhand shuffle. pull off a large section of cards
from the front, more than the number you want to be left in place.
Place the next set of cards you pull from the front slightly off to one
side. Magicians call this side jogging. Shuffle the remaining cards in
small packets by doing a normal convincing overhand shuffle.
127
128 Conjuring with Computation
Figure 19.1. Side jogging involves lifting off the front section into the other hand
first, then putting the subsequent cards offset slightly. Finally, the original cards are
cut back to the front.
All that remains is to cut the cards, which you do by lifting the
section containing your protected card stack from the back of the pack.
It is easy because it’s side jogged from the rest. Move this section to
the top of the deck (Figure 19.1).
Practise until it looks like a convincing overhand shuffle and final
cut, while you have actually kept your stack in order on the top of the
deck.
How It Works
The side-jogged cards were the front cards, and they were replaced
unchanged back on the top by the final cut.
Thinking Computationally
The specification of this trick is identical to the previous false top
overhand shuffle. That means any trick that used that false shuffle could
swap in this one instead, and it would work just as well. You might do
that because you are doing two tricks in a show that start with the same
false shuffle. Therefore, it gives more variety to swap one for the other.
Or perhaps you are far better at one false shuffle as you have practiced
it more, so in an important show, you switch that shuffle into a trick
that is supposed to use the other. Or perhaps one is quicker, and you
have limited time to do the trick, or to do the cut, you need a table,
but you are doing the trick without one . . .
This is another important use of procedural abstraction. It gives
a framework for modifying algorithms, so tricks and programs, in a
way that is guaranteed not to affect, algorithmically, whether they work
or not.
With programs, we might want to swap in a new version of a
procedure that was faster or used less memory, perhaps, or one that
was just available in a new standard library that had been developed to
130 Conjuring with Computation
make the whole program easier to maintain (as we will see in the next
chapter).
Conjuring
This cut leaves the cyclic order of the pack unchanged, just starting
at a different point in the cycle.
Computation
Programmers collect together similar procedures such as ones
needed to manipulate a single data structure into what are called
libraries.
The Trick
Fan the cards and allow a spectator to touch the back of a card. Split
the pack at that point, moving the top half to the bottom. Repeat this
for as long as the spectator wants to, to “mix them thoroughly”.
131
132 Conjuring with Computation
How It Works
This is actually just a series of normal cuts of the pack. Cutting the
pack does not change the cyclic order of the cards. You can do it as
many times as you like, and the cycle will be the same, just starting
at a different point. To see this, imagine the cards laid out in a circle,
before the cut and then again after the cut. The circle is the same before
and after the cut, just rotated (see Figure 20.1). The same cards follow
each other as before. You can do it as many times as you wish, and the
cycle remains. By fanning the deck each time, it is less obvious what is
happening than just cutting the deck repeatedly. You can also let lots of
people take part, so making it a communal decision as to how the pack
is “shuffled”.
Thinking Computationally
The techniques from this section are useful components that can form
part of a range of tricks. Any good magician will build up a repertoire
of them, to call on as needed when inventing new tricks or developing
their own versions of old ones. A false shuffle at the start of a trick, that
relies on cards being in a known position or order at the start, will throw
the audience off the trail of what is actually happening.
In programming, the idea of a library is similar to this. Calling it a
“library” is actually a design metaphor using the idea of a library of
Cyclic False Cut: Libraries of Useful Procedures 133
Figure 20.1. The cards of the pack laid out in a circle before and after a cut. The
cyclic order is unchanged.
structure, like a list manipulation library, that gives you everything you
might need to manipulate lists of things, or a String library of operations
for manipulating strings of characters (words, sentences, etc.).
Rather than describing all the procedures in detail whenever they
are needed, giving the full algorithm of every trick or program, we can
just include the library noting they are available. If writing a series of
magic manuals to record all our tricks, we might similarly write one book
just about cuts and shuffles, and refer to it whenever we mention one.
We would then avoid having to explain each, so avoid writing out the
instructions, like those for a cyclic false cut, in every book.
This is an organising principle that allows us to make good use of
procedural abstraction and decomposition, reducing the amount of
work and actually making our instructions clearer.
Building Bigger
We have now seen most of the basic techniques that allow us to build
large programs a little at a time. In this section, we first look at the
importance of clear descriptions of what procedures do, then look at
how to chain results from one procedure or trick to the next and apply
the techniques to a trick we have already seen.
This page intentionally left blank
Chapter 21
Conjuring
The false choice is a way of leaving a volunteer thinking they chose
one of three things when actually you did.
Computation
Procedures need to have clear interfaces, i.e., specifications or
descriptions of exactly what they do and when they can be used if
they are to be used in large programs without introducing mistakes.
The Trick
Suppose there are three objects, and you wish to be left with a specific
one, say the third. This could be three piles of cards, for example, or
three boxes with a reveal hidden in one of them. Ask the volunteer to
point to one of the objects. If they point to the one you wish to keep,
then say “Ok, we will keep this one”. You remove the other two objects.
If, on the other hand, they point to one of the ones that you wish
to lose, then say “Ok, we will remove that one”. You remove it. You
still have, in this second case, another object to lose. You now fill up
137
138 Conjuring with Computation
some time with a story that fits the situation, so everyone loses track
of what you did originally. Then you ask them to point to one of the
two remaining objects. Whichever they point to, you remove the one
you wanted to lose, and keep the other, saying the appropriate thing to
justify this, as above (see Figure 21.1).
Figure 21.1. If (a) the person points to the object you intend, then you take that
as the choice. Otherwise (b, c) you note the pointed-to object is discarded and do a
further false choice.
The False Choice: Programming Interfaces 139
How It Works
Make sure you say “point” not “choose” when asking them to pick a
pile as that is neutral language that does not imply anything about
what you will do once they point to it. Because you did not tell the
person what you were going to do with the object they pointed to and
used such neutral language they see nothing strange over whether you
remove it or keep it. As long as you only do the trick once they have no
idea, there was some alternative course of action that could have been
taken.
Thinking Computationally
It is not just cuts and shuffles that can be used to enhance different
tricks. Here we have given a generally useful false choice. In a similar
way, there are many different kinds of procedures that can be written
and stored in libraries to be pulled out when needed. Here we have a way
of forcing a particular choice while making it look like a free choice. “The
Forcing Matrix”, described in the next chapter, is another example. We
could therefore have a separate collection, or “library”, of false choice
algorithms too.
Each separate mini trick like this, or library procedure, we create
needs a clear interface in the sense of it being designed, so there is a
clear situation when it can be used having a clear and precise effect.
This then makes it easy to give a precise description (i.e., specification)
of when it can be used and what the effect is. For example,
The False Choice can be used when you have three objects (or
piles of objects) and wish to be left with just one specific one of
the three.
After the false choice, you are left with the one of the three
you wanted, but the audience believe that an audience member
made the choice.
The False Choice: Programming Interfaces 141
We have tried to make sure all the cuts, shuffles and false choices
described here do have clear interfaces. For example, an interface for the
Cyclic False Cut (Chapter 20) is
The Cyclic False Cut is a false shuffle that can be done any
time that the precise order of the cards does not matter, only
the cyclic order matters. It will leave the cyclic order alone, just
change the starting position.
Conjuring
The forcing matrix gives a way to always force the selection of the
same number, despite a volunteer having a series of free choices,
choosing numbers from a grid to add together.
Computation
We have previously used decomposition to create procedures which
just get a job done. A variation is the function. The job of
a function is to deliver an answer, such as a number or data
structure. That answer is used later in the program. We also look
at preprocessing data: organising data in advance, with the result
used over and over again later in the program.
The Trick
You set out a grid of “random” numbers. There are various ways you
can do this such as dealing the top cards from a pack of cards into
143
144 Conjuring with Computation
How It Works
To set the trick up, you first need to decide the number you wish to
force and the size of grid you wish to use. Let’s say the number you
want to force is 127 and you want to use a 5 × 5 grid. The grid has
5 rows and 5 columns. You need a “seed” number for each row and
each column, so in this case, you need 5 + 5, i.e., 10, seeds. Create any
The Forcing Matrix: Functions 145
Figure 22.1. Creating a forcing matrix: (a) seed numbers labelling rows and columns
that add up to the number you want to force, 127. (b) Fill in the numbers by adding
row and column seeds. (c) The final forcing matrix is produced by removing the seed
row and column.
done a false shuffle such as a false top overhand shuffle (Chapter 18) to
appear to have shuffled the pack while leaving those cards in place. Use
your imagination to invent more creative ways of making the grid!
Why does it work? Each position is the total of a row and column
seed. When you cross out everything else in that row and that column,
you cross out all other chances of picking those two seeds. That means
each choice gives you two of the seeds that have so far not been used
to add in to the total. Eventually, when all numbers are chosen, you
have all the seeds represented in the numbers chosen and so part of the
addition.
Thinking Computationally
We have up to now been decomposing magic tricks (and programs) into
procedures whose purpose is to get a job done. Here we are seeing a
variation of this. Instead of just getting a job done, we are decomposing
into parts whose purpose is to return a result (like a number calculated,
The Forcing Matrix: Functions 147
Conjuring
A number is randomly chosen. An audience member turns to that
page in a book. The selected page then magically directs them to
a word. It is both on a random page and a random word on that
page: a word no one could have possibly predicted. . . but you did.
Computation
Once you have decomposed a program into functions and written
the functions, those functions provide answers to be passed on to
the next procedure or function in a sequence.
The Trick
This trick just combines the forcing matrix trick with Wizardly Book
Magic (Chapter 6) to add an extra level of mystery.
149
150 Conjuring with Computation
Before the trick, find an interesting page (say page 127) in your
chosen book (perhaps in The Wonderful Wizard of Oz) and work out
the predicted word on that page as in Wizardly Book Magic of Chapter
6. Then create the appropriate forcing matrix (say of size 5 × 5) for that
page number, i.e., that forces 127. Write the predicted word from that
page in an envelope ready for the reveal at the end.
When doing the trick, use the forcing matrix to pick a page in the
book apparently at random, but actually known to you in advance. Then
use that page to do the Wizardly Book Magic trick. It finally gives you
a word in the book that is your final prediction. Have someone open the
envelope to reveal that you predicted the word.
How It Works
This is combining two tricks that each is guaranteed to force a result,
with one leading to the other. If you understand how each works (see
Chapters 6 and 22), then it is clear the combined trick is guaranteed to
work.
Forced Wizardly Book Magic 151
Thinking Computationally
This involved combining two tricks, by passing the result of one to the
next, to give a bigger, better trick. Programmers do the same thing,
chaining together existing functions that do separate things. The first
provides the input to the next. Programs are often decomposed in this
way, using a similar structure to the above algorithm. A first function
returns a value or data structure that it computes (like the matrix).
That value is passed to the next function as a parameter. The second
function, in its turn, uses the value and calculates from it something new
(like the page number). It returns that to be used in the next function,
and so on.
In the above, forcing matrix, f, is the result of the function that
creates the forcing matrix. It is then the actual parameter (i.e., value
passed) to the function that gives instructions on how to use a forcing
matrix. A similar thing then happens with the page number. It is the
result returned by using the forcing matrix, and then that page number
value is passed to the actual Wizardly Book Magic trick.
This also shows that to create a better trick, you don’t necessarily
have to combine just simple trick elements like shuffles. You can
sometimes combine whole tricks, that are magical on their own, to get
much more powerful tricks.
A similar thing is done with programs, building ever more sophis-
ticated programs, by combining functions that already do something
useful and have been used as a self-contained program so that the
results of one are passed to another. Procedures are also used when
an action is needed on the results calculated so far by the chain of
functions. For example, a procedure might be used to display the results
of the function on the screen. By building up ever large sequences, more
complex programs are created.
152 Conjuring with Computation
Conjuring
We return to Invisible Palming and the Teleporting Top Hat and
look at them anew.
Computation
Procedures with parameters allow us to create generalised pro-
grams. We can decompose them into parameterised parts and
sequence them one after the other, using the same parameters.
By matching preconditions and postconditions, we can give an
outline argument as to why the whole algorithm works without
worrying about how the parts work.
The Trick
In this chapter, we are going to just look again at the same Invisible
Palming trick from Chapter 1 and its variation from Chapter 14, the
Teleporting Top Hat. In doing so, we will put together some of the ideas
we have seen so far.
153
154 Conjuring with Computation
Thinking Computationally
We wrote out the original Invisible Palming trick as a single algorithm
that wasn’t decomposed at all. We then saw in the Teleporting Top Hat
that with a little decomposition we could make it easy to substitute a
new part with an identical effect. Here, we are going to see how we
could decompose it further into a whole series of procedures, but also
use parameters to make it more general. We also use specifications to
make clear the intended effect of each part. In doing so, we are seeing
the foundations of the way large programs are built.
7 cards (both odd) with that last card making 8 cards in one pile. In
each final pile, there are 3 pairs, plus one extra in each to make 7 and
then that last “extra card” to put on one of the two. As you have two
odd piles, that “extra card” disappears into a new pair whichever pile it
is placed on.
Thirteen cards doesn’t work because it splits into 6 cards and 7
cards: 3 pairs in each pile and one leftover. The 6 pairs form two piles of
6, and now whichever pile you place the “extra card” on cannot complete
a pair, so doesn’t appear to disappear.
When we take away the extra card (so have n − 1 cards), then we
need two piles of an odd number of cards. So, that number (n − 1) of
cards must divide by 2, and when halved the resulting number of cards
should be odd. This gives us a new fact about n that:
{(n − 1) ÷ 2 is odd.}
Combining this with the fact that n itself also has to be odd, we get
the precondition for the trick:
{There are n cards AND n is odd AND (n − 1) ÷ 2 is odd.}
This says that the trick is only guaranteed to work if these three
assertions are true. We could treat it as an instruction to execute that
essentially terminates our attempt to do the trick if we find we have the
wrong number of cards. If that happens, we need to start again with the
right number. I always count the cards to double-check this precondition
holds before doing this trick, exactly for that reason.
Our top-level instructions in our new written version now just give
an overview of the trick. By the end, a card should have appeared to
jump piles.
TO DO Invisible Palming WITH n cards:
{There are n cards AND n is odd AND (n − 1) ÷ 2 is odd.}
1. DO Deal out the cards in pairs with one left over WITH n cards.
2. DO Take the cards back as pairs splitting the pairs into two piles
WITH n cards.
3. DO Place the extra card.
4. DO Pretend to move a card between piles.
156 Conjuring with Computation
5. DO Reveal that the extra card has magically moved piles WITH
n cards.
{An extra card appears to have moved piles.}
We can still get the detail of exactly what we need to do when we
want it by looking at the separate instructions for each step. However,
this gives an overview, uncluttered by all the detail.
Chaining assertions
We still need to write out the detailed instructions for each step as its
own procedure. Each can have its own precondition and postcondition,
saying what it does. Actually writing out those procedures’ assertions
can help us check overall the trick is going to work BEFORE we worry
about the detail. In the sequence of instructions, each postcondition
should match the precondition of the next step. If it does not, we might
be using an invalid step. The first procedure call has to guarantee the
precondition of the second, and the second has to do the same for the
third. This provides a chain of reasoning as seen in Figure 24.1.
The first step in the algorithm is to deal out the pairs with one left
over. It has the same precondition as the overall algorithm: the same
thing must be true at the start for both. The first step guarantees that
on its completion (its postcondition):
{There are an odd number of pairs of cards dealt out with one extra
card on its own, totalling n cards.}
That is exactly the precondition of the next procedure to be followed.
Our preconditions and postconditions therefore are giving us an outline
logical argument as to why the trick as a whole works, step by step. The
argument runs something like this:
If we start with the right number of cards to make two odd piles with
one extra card (i.e., n is odd AND (n − 1) ÷ 2 is odd . . .
then on dealing them out into pairs . . .
There are an odd number of pairs of cards dealt out with one extra
card on its own, totalling n cards.
After collecting them back in, there are two piles each holding . . .
and so on.
Invisible Palming (Again): Sequencing Procedures Together 157
Figure 24.1. Linking the procedures together with matching preconditions and
postconditions.
158 Conjuring with Computation
In summary
Overall, we have written the instructions for a general version of the
trick that works with any appropriate number of cards as specified. We
have decomposed it into a series of major steps and used preconditions
and postconditions for those steps to give an outline argument as to
why they work. Finally, we have written the detail of those decomposed
instructions. If we can give a convincing argument that each meets its
own specification, then our overall argument shows that the whole trick
works.
TO DO deal out the cards in pairs with one left over WITH n
cards:
{There are n cards AND n is odd AND (n − 1) ÷ 2 is odd.}
1. DO THE FOLLOWING (n − 1) ÷ 2 TIMES.
a. Place 2 cards in a pair.
b. Say “Two cards make a pair”.
2. Place a single card in a pile on its own.
{There are an odd number of pairs of cards dealt out with one extra
card on its own, totalling n cards.}
TO DO take the cards back as pairs splitting the pairs into two
piles WITH n cards:
{There are an odd number of pairs of cards dealt out with one extra
card on its own, totalling n cards.}
1. DO THE FOLLOWING (n − 1) ÷ 2 TIMES.
a. Take a pair and split them into the two piles being formed.
b. Say “Two cards make a pair”.
{There are 2 piles each holding (n − 1) ÷ 2 cards, where (n − 1) ÷ 2 is
odd, with one extra card still to be placed, totalling n cards.}
We’ve seen abstraction applied to the way we write out the order of
instructions in the form of procedures. Now, we explore similar ideas
with data: data abstraction. Data can have different representations
which are abstract versions of how the data is really stored. We look at
what it means to hide the underlying representation of data.
This page intentionally left blank
Chapter 25
Drawn to You
Codes Representing Data
Conjuring
Four people each draw a picture on a piece of paper. The paper
is jumbled up, but you are able to read each person’s personality
and work out who drew each.
Computation
Tricks often rely on representing information in some particular
way. Choosing an appropriate data representation is core to
computational thinking and programming.
The Trick
You tear the bottom half of a sheet of paper out of an A4 pad. Fold this
piece in half, then half again and tear it into quarters along the folds.
Hand a piece to each of four friends who you have asked to stand in a
line. Next, give each an identical pencil and ask them to secretly draw
a simple picture on their piece of paper. You may want to give them
165
166 Conjuring with Computation
a book to lean on while doing this. Each can draw anything they like.
Have each crumple their paper into a ball and put it into a small bag
where they are well and truly mixed.
You announce that you are able to judge the personalities of
people from their drawings. You can also read personalities from their
body language (which you have been observing). You will now do a
psychological reading of their picture and match each to the person
who drew it.
You remove the screwed-up bits of paper one at a time, uncrumple
them, and through the psychological reading of the picture you correctly
identify which person drew which picture!
How It Works
Though it seems that simply tearing the half sheet into four is an
innocent action, it’s actually the secret. It encodes information on the
bits of paper.
If you look at four bits of paper torn from a pad in this way, you will
see that each is similar, but actually subtly different (see Figure 25.1).
• The bottom right quarter has two smooth, clean edges, the
original corner of the sheet from the pad you started with. It
is the only piece that has two such smooth, clean edges.
• The bottom left section has only one smooth edge (from the
bottom edge of the original sheet), but that edge is long (about
10 cm if you used A4 paper).
• The top right section also has only one smooth edge (from the
side of the original piece of paper), but it is a shorter side (roughly
7 cm if you used an A4 pad).
• The top left quarter has no smooth edges at all. It is torn all
around, as it came from the middle of the original piece of paper.
By giving out the paper squares in the above order, each person has been
given an identifiable piece of paper. Instead of looking at the drawings,
you can just look for the different pieces of paper.
Drawn to You: Codes Representing Data 167
Figure 25.1. The bottom half of a piece of paper torn out of a pad of paper. Each
side has different smooth sides.
Thinking Computationally
A big part of computational thinking involves working out the best way
to represent information to make the task at hand easy. Western
cultures use characters like A and 1 strung together into words and
numbers to represent information, but there are lots of different ways
to represent the same information. In many oriental cultures, characters
are used that represent words. In South America, knotted strings, called
quipu, were once used to represent numbers and possibly words . . .
Choosing a good representation matters. This trick is based on
choosing a representation for four different things (the four people,
or equivalently the four numbers representing their positions). The
representation is one that you, the magician, can easily see, but they do
not notice at all. The audience just see the four bits of paper as the same.
However, the performer, in the way they tear them out, is building their
own secret representation of the vital information. For them, knowing
the representation is just like writing on the corner: Person 1, Person 2,
Person 3 and Person 4.
To help remember, you could even go a step further and create
a representation in words: a code, where S represents a smooth side:
(1) S-S, (2) LONG-S, (3) SHORT-S and (4) NO-S. Remember the
code in that order, and it is even easier to remember which is which.
Drawn to You: Codes Representing Data 169
This is made easier still if you organise the people into a line rather
than just letting them stand anywhere. By doing that, you physically
give the people an order too (their position is now a representation of a
number). Their order matches the order you have remembered for the
paper. You are organising the information they embody by positioning
them in the line. The organisation of information (its structure and
representation) makes the task easier to do. Organising information into
the most convenient form is what data representation is all about.
Programmers often choose how to organise data before writing code.
For example, if you sort names into an alphabetically ordered list first,
then it is possible to find a given name much faster. You can use fast
search algorithms. That is why dictionaries and the index of a book are
in alphabetical order, as were old-fashioned telephone directories. Search
engines organise the information from web pages before you do searches,
allowing them to search every web page in the world and return a list of
those that are relevant in a fraction of a second.
Choose the right way to organise information for the task, and that
task can be easy or hard, whether it is a computer or a person doing the
computation.
Figure 25.2. The way the information about the people is represented in the paper
for the “Drawn to You” trick.
170 Conjuring with Computation
Conjuring
In this challenge, the audience must do some Roman numeral
maths, making a Roman numeral sum add up.
Computation
The way you choose to represent numbers affects how hard or easy
it is to do different tasks. Addition and subtraction are fairly easy
with Roman numerals but multiplication and division are harder.
When choosing a representation of data, it is important to think
about what you will need to do with the data.
The Trick
Explain that this is more a challenge than a magic trick. Ask a friend
if they know Roman numerals. Remind them that Roman numerals are
just a different way of writing numbers, though one that makes doing
some maths harder than ours. They used I to mean one, V for five,
X for 10, counting I, II, III, IV, V, VI, VII and so on. Say “Let’s do some
Roman maths. . . ” On a pad, slowly write out the start of a sum using
171
172 Conjuring with Computation
Roman numerals, saying each number after you have written it to check
they are the following:
II + II = IV
“Two . . . plus . . . two . . . equals . . . four”
II + IV =
“Two . . . plus . . . four . . . equals . . . ”
Next, write in the Roman numeral for 9 (IX) saying,
“This is an obviously wrong bit of maths using Roman numerals”:
II + IV = IX
Finally, lay the challenge: “The challenge is: can you make it work so
that the two sides are equal by adding just a single line?”
Give them a few minutes to try, and if they fail, then show them the
answer. (If they manage to do it, praise them for their brilliance!)
How It Works
The solution is to draw in an S before the IX to make the correct sum.
II + IV = SIX
Point out that you didn’t say they had to use a straight line! You also
didn’t say they had to stick to Roman numerals.
6. Say “The challenge is: can you make it work so that the two sides
are equal by adding just a single line?”.
7. Give them a few minutes to work it out.
8. Write an “S” before the IX to make the sum work.
9. Say “I didn’t say you had to use a straight line!”.
Thinking Computationally
When we think of numbers, we tend to think of the digits we use to
write them, but that is just a representation: one particular way of
writing them. There are lots of different ways to write numbers. Early
civilisations just used tally marks: a very direct representation with one
mark for each thing you were counting. If you have 12 goats, then you
would write 12 lines.
IIIIIIIIIIII
This representation is called unary. It is fine for small numbers, but
it gets harder to keep track of what number it is for larger numbers.
The first improvement is to make the groups of five more obvious
by clustering them, such as making every fifth line into a bar. This
representation is called tallying.
IIII II
IIII
That makes keeping track of how many you have easier as you can check
by counting the tallies in fives. Adding two numbers represented as tallies
is fairly easy too for small numbers as you just combine the two lists
of tallies, and then arrange any extra ones into clumps of five. It also
works well given a limitation of the way our brains work. Scatter some
number of coins on the table. If there are less than five or so, you can
probably see how many are there at a glance. If there are more though,
then you will start having to count to tell. Our brains are good at just
seeing small numbers, but can’t do it naturally for larger ones. You can
probably see how many there are in this sum:
II + III = II III
But, what number is the following? You have to count them all over
again! It’s just marks you are counting now instead of goats.
IIIIIIIIIIIIIIIIIIII
174 Conjuring with Computation
Conjuring
Everyone chooses a lucky lottery number. You then turn the room
into a lottery machine with everyone bouncing lottery balls around.
Three numbers are chosen at random and added, and then we see
if anyone has a lucky lottery ticket. The lucky winner is of course
you, the magician.
Computation
We use the Hindu–Arabic numeral system. The really amazing idea
here is that the value of digits changes depending on their position
in the number, so 2 can stand for two, twenty or two hundred
depending on its position. This makes large numbers more concise
but also makes operations like addition and multiplication easier.
It makes for simple algorithms.
175
176 Conjuring with Computation
The Trick
Before the trick, write the numbers 3, 4 and 8 each on identical pieces
of red paper, the numbers 1, 5 and 9 on yellow paper, and the numbers
2, 6 and 7 on blue paper. Secretly, write the number 1,665 on a piece of
paper made to look like a lottery ticket and put it in a sealed envelope.
Explain to the audience that you are going to play a lottery game, but
where the whole audience will be the lottery machine. Everyone should
write down their lucky 4-digit number on a piece of paper and place it
in an envelope. It will be their lottery ticket. Hold up your envelope and
say you will play too and that the envelope contains your lottery ticket.
Tape it to the wall so all can see it, with either masking tape or some
other way that will not damage the wall.
First, you need to randomly choose the order of the colours that will
determine the order that balls are removed from the lottery machine.
Have a volunteer decide the order of the three colours (red, yellow and
blue) at random to use. Write the colours in the order they decide on a
flip chart.
Explain that you are going to make the lottery balls out of screwed-
up coloured paper so they can bounce around the room without hurting
anyone. Show the audience the pieces of paper with the numbers. Explain
that they will be the lottery balls, noting that just like lottery balls they
are coloured and have numbers. You have used three colours and the
numbers one to nine.
Next, ask a volunteer to screw each piece of paper up into a ball and
toss them into the audience. The audience should then toss the balls of
paper around as if they are in a lottery machine.
Next, go to the order of colours chosen, and ask the volunteer to
pick out one ball of the first colour. Unscrunch it to find its number.
Write this under the colour. Do the same for the next colour, and then
the third. Explain you have the first 3-digit random number but that
you are going to do this three times and just add the numbers so that
all the balls are used. Repeat this process with the same order of colours
to get a second 3-digit number and finally a third. You now have three
3-digit numbers (see Figure 27.1).
The Lottery Trick: Place-Value Number Representation 177
Figure 27.1. This shows what would be written on the flip chart if the colour order
chosen was: blue, then yellow, then red; and then balls are drawn randomly as follows:
Blue — 6, Yellow — 9, Red — 3, Blue — 2, Yellow — 5, Red — 8, Blue — 7,
Yellow — 1 and Red — 4. The numbers 693, 258 and 714 are added on a calculator
to give the answer and so the final lottery number, 1,665.
Give the volunteer a calculator and have them add up the three
numbers. Then, turning to the next sheet on the flip chart, write the
total and circle it. Announce that is the winning number and ask if
anyone has won. Finally, have the volunteer open the envelope to reveal
your lottery ticket and read out your number. You have indeed picked
the winning number, 1,665!
How It Works
The choice of order of colours appears to add randomness to the process,
but it actually makes no difference. Let us look at just one colour, red.
The three numbers there are 3, 4 and 8. Whichever position red is put
in, those three numbers will always be in that same column as that is
where the red numbers are written. That means that even though the
order they are chosen changes, they will always be added together (and
they add up to 15). The three numbers on the other coloured pieces of
paper will also always be added together, whatever order the balls are
pulled out. The yellow numbers, 1, 5 and 9 will be added also giving 15.
Similarly, the blue numbers 2, 6 and 7 will be added together and add
up to 15 too.
This means that whatever order the balls are chosen, the three
columns of numbers will add up to 15. You will always be adding 15, 150
and 1,500 as each represents a different decimal place so is increased
178 Conjuring with Computation
by 10 from the previous one. 1,500, 150 and 15 add up to 1,665, the
number you wrote on your lottery ticket.
8. Ask the volunteer to add up the three 3-digit numbers under the
colours using a calculator.
9. Turn to the next sheet on the flip chart and have them write the
total there and circle it.
10. Announce that that number is the winning number.
11. Ask anyone with a winning ticket to claim the prize.
12. Have the volunteer open your envelope to reveal that you have
a winning ticket.
Thinking Computationally
This trick relies on the fact that we use the Hindu–Arabic number
system. It was invented by Indian mathematicians in the first few
centuries BC but brought to the West by Arab scholars and traders.
The really amazing idea that matters most about Hindu–Arabic numbers
is that digits have a position in a number. Unlike in Roman numerals
(where for example X means 10 wherever it appears), their value changes
depending on their position. For example, in the Hindu–Arabic system,
2 can stand for 2, 20, or 200, and so on, depending on its position in
the number. This is a vital part of the trick that makes it work. The
numbers are placed in a position labelled by their colour so that they all
come back together to be added.
This representation makes large numbers very concise, so taking up
little memory in computer terms. It also makes manipulating numbers
by humans relatively easy: it makes for relatively simple algorithms such
as the ones we learn in primary school. Counting is easy, for example, as
are doing things like addition and multiplication. Multiplication is much
easier than with Roman numerals. The choice of representation makes
tasks easier or harder.
This is the key idea about choosing a good representation for data,
whether numbers or other kinds of data: you want to make it easy to do
the key operations. With computers, that usually means you want there
to be fast algorithms, though sometimes taking up as little memory as
possible matters more, and sometimes you just want it to be easy to get
the algorithm right.
180 Conjuring with Computation
Conjuring
Four volunteers jointly pick a number and jointly concentrate on
it, helped by your set of magic cards. Despite them keeping it
secret, you are able to read their collective mind and announce
their secret number.
Computation
Digital computers do not use the decimal system that we use but
instead use a different place-value system called binary. Binary is
also the underlying representation that makes certain tricks work.
The Trick
Ask four volunteers to help in an experiment in mind reading. Give them
a list of 15 numbers and ask them to jointly pick one without telling you
what it is. Then give each a card which contains a jumble of numbers
and letters. They stand in a line facing you, cards hidden from your sight.
They each look to see if the jointly chosen number is on their card. If it
is, then they must close their eyes and think hard only of that number.
181
182 Conjuring with Computation
Those holding a card without the number choose one of the letters on
their card instead and stare at it, thinking only of that letter. That way
they won’t confuse you, you say. Once they are all concentrating, you
close your eyes, think hard, then pick up the thoughts about the secret
number and announce what their secret number was!
How It Works
You will need the set of 5 cards shown in Figures 28.1 and 28.2. You can
download them from https://conjuringwithcomputation.wordpress.com,
print them out, and stick them onto card or write out your own with
the same numbers and letters on.
First, you give four volunteers the card with the numbers 1 to 15
to look at (Figure 28.1). They jointly choose one and keep it secret.
Next, you give them the 4 special cards with numbers and letters on,
one each, in the order as shown in Figure 28.2.
Figure 28.1. The card with the numbers 1 to 15 on, for the “Cards on Your Mind”
trick.
Cards on Your Mind: How Computers Represent Numbers 183
Figure 28.2. The 4 cards to give out for the “Cards on Your Mind” trick.
These cards are special in that the numbers on each are carefully
selected. Secretly think of them as card 8, card 4, card 2 and card 1.
These are the numbers in the top left corner of the card. Stand the
volunteers in a line and give out the cards in that order, so the person
on the right facing you has card 1 and the person on the left has card 8.
Now, get those with their jointly chosen number on their card to
close their eyes. You add up the card numbers of the people closing
their eyes, and that is their secret number. For example, if the people
holding card 1 and card 8 have closed their eyes and the other two
volunteers are staring at their card, eyes open, then add 1 and 8 and
announce they are thinking of number 9. If instead the people holding
card 2, card 4 and card 8 have their eyes closed, then the secret number
is 2 + 4 + 8 so 14. It is that simple. You don’t need to be able to read
minds, just add up powers of 2.
Thinking Computationally
The secret of this mind-reading trick relies on the binary representation
of numbers. Figure 28.3 gives numbers from 1 to 15 with their binary
version. It is a way of writing numbers using sequences of 1s and 0s. It
is a positional number system that works just like decimal, but where
there are only two symbols: 0 and 1 instead of 10, and the positions
increase in powers of 2 (1, 2, 4, 8, . . .) instead of powers of 10 (1, 10,
100, 1,000, . . .).
To work out the value represented by the number in both decimal
and binary, you just multiply the number in a column by the column
heading and then add the answers. In decimal, the column headings are
powers of 10 (as there are 10 symbols). In binary, the column headings
are powers of 2 (as there are 2 symbols).
That means the 1 in the binary number 10 means 2, the 1 in the
binary number 100 means 4, and the 1 in the binary number 1,000 means
8. So just as the number 1,204 in decimal represents 1 × 1,000 + 2 ×
100 + 0 × 10 + 4 × 1, the binary number 1,101 represents 1 × 8 +
1 × 4 + 0 × 2 + 1 × 1 (so means 13 as 8 + 4 + 1 = 13).
You can use any two different things to represent those 1s and 0s
as they are just arbitrary symbols. In the trick, eyes open is used for 0
and eyes closed is used for 1. The secret number is then being spelled
Cards on Your Mind: How Computers Represent Numbers 185
8421
1 0001 = 0+0+0+1
2 0010 = 0+0+2+0
3 0011 = 0+0+2+1
4 0100 = 0+4+0+0
5 0101 = 0+4+0+1
6 0110 = 0+4+2+0
7 0111 = 0+4+2+1
8 1000 = 8+0+0+0
9 1001 = 8+0+0+1
10 1010 = 8+0+2+0
11 1011 = 8+0+2+1
12 1100 = 8+4+0+0
13 1101 = 8+4+0+1
14 1110 = 8+4+2+0
15 1111 = 8+4+2+1
Figure 28.3. The numbers 1 to 15 with their binary representation and as an addition
of powers of 2 corresponding to the positions of 1s.
out in binary by the four people’s eyes. You can just read the binary to
know what the chosen number was.
This works because the numbers on the cards are just those with a
1 in the corresponding position of that number when written as binary.
Card 1, for example has all the odd numbers written on it as in binary
all odd numbers have a 1 in the first position (the 1s column). See
Figure 28.3 and notice how the first column has a 1 for numbers 1, 3,
5, . . .
Similarly, card 8, the leftmost card, has the numbers from 8 to 15
written on it as those numbers have a 1 in the fourth position (the 8s
column) in the binary.
By telling people to close their eyes if the number is on the card you
are secretly saying close your eyes if there is a 1 in the binary at your
position. The volunteers are representing their chosen binary number.
Adding up powers of 2, for eyes shut or not, is then just the way you
work out what number that binary number actually represents.
You don’t actually have to remember the table of Figure 28.3 to
work out what decimal number the binary number stands for. You can
just do the calculation instead to work out what number your volunteers
are thinking of. We put the numbers 8, 4, 2 and 1 above the columns,
and you should think of the person in each position corresponding to
186 Conjuring with Computation
that number. You then just add up the column numbers to get the secret
number. If there is a 1 (eyes shut) in the binary in a column, then that
column number is added to the total.
For computers, binary is a better representation of numbers than
decimal. In the trick, the 1s and 0s are represented by eyes shut or eyes
open. In a computer, they are just differing electrical signals: high and
low voltages on wires or transistors that are on or off, for example. These
binary signals are easily stored and easily transferred from place to place
in the computer, meaning the binary numbers can be moved to where
needed to do a calculation. There are also very fast algorithms for doing
arithmetic, and it is easy to create specialist hardware for computers
to use to do the maths. This all makes binary an ideal representation
for numbers in a computer. Therefore, all data are ultimately stored in
computers as binary 1s and 0s rather than as decimal numbers.
Conjuring
A postcard is chosen from a selection while your guest magician is
out of the room, but despite that they can quickly say which card
was touched.
Computation
All sorts of things can be represented by numbers and once they
are then we can bring them into the digital world, as those numbers
in turn can be represented by binary.
The Trick
Explain that your guest magician has extremely heightened powers of
observation and can tell when an object such as one of the set of nine
postcards on the table has been touched by someone. Have them leave
the room, then wearing gloves, take the postcards and shuffle them. Lay
them out on the table in a rough three-by-three grid and ask a volunteer
to pick one by touching it. They should then remember their card.
187
188 Conjuring with Computation
Collect the cards back up, shuffle them, and then lay them back
down on the table, still wearing gloves. Call in your partner magician.
Immediately, just by looking at the cards, they are able to say which
card was touched.
How It Works
In advance, you agree a number from 1 to 9 for each card. Both memorise
these numbers for the cards. To make this easier, you could choose cards
that have different numbers of objects: perhaps the one you label 1 is
a picture of a single lighthouse, whereas the one labelled 3 has three
seagulls flying in the background, and so on.
As your partner enters the room, facing them, you casually put your
hands together, touching that many fingers together as you do so. If you
do this as your accomplice enters the room, announcing their arrival, the
audience are likely looking at the door not at you to make it even less
likely they will see or be suspicious about the movement of your hands.
A variation, rather than labelling the cards (should you find that hard
to remember), would be to communicate the position of the chosen card
as a pair of numbers giving an x and y coordinate with the fingers you
touch together on each hand. So if the card was in the bottom left, you
would touch one finger on each hand. If in the top left, touch one finger
to three fingers.
Thinking Computationally
We have ways to represent numbers in binary, but numbers themselves
can be used to represent other things: cards and the images on them
in a simple way as part of this trick. The same can be done for music,
images, money . . . just about anything. That idea is the foundation for
the digital world.
Images do not have to be represented just by a count of the objects
in them. The actual image can be turned into numbers that describe
the image in detail. There are several ways to do this. One way, called
vector graphics, involves describing each line or shape in the image
by its size and position. Another, called raster graphics or bitmap
graphics, involves breaking the image into small squares and giving
numbers to each square.
To do this, first, we need to represent colours as numbers. One
simple way is just to give each colour in the image a single number: red
is 1, orange is 2, yellow is 3, and so on. Next divide the image into lots
of small squares and note which colour is the main one in that square.
Represent that square (or pixel) by the number for that colour. Now the
list of numbers for all those pixels is a representation of the picture. Each
number in turn can be represented by binary so stored on a computer or
sent over a network. By sending the list of numbers as binary over the
Internet or phone network to someone else, they can reconstruct a copy
of the image.
If an image or piece of music or a film or a book can be represented
by numbers like this, then it can be converted into a digital object.
190 Conjuring with Computation
Conjuring
Someone flips over a card in a grid of cards. Blindfolded, you can
leave your body, and hovering from the ceiling watch what happens
and so can immediately say which card was changed once you
return and the blindfold is removed.
Computation
Computers use special representations to send data over networks.
Error-correcting codes are ways to represent, and so store and
transmit, data so that if some are corrupted, a computer can not
only tell it has been changed but also correct the changes. It can
do this without knowing in advance what the original data was.
The Trick
For this trick, you need a partner magician. Explain to the audience that
you are able to float out of your body and see things you would otherwise
not be able to see. You go to the back of the room where a volunteer
blindfolds you. Have them check the blindfold first to ensure there is
191
192 Conjuring with Computation
no trickery. They stay with you throughout the whole trick to both
“guard your body” and make sure you remain blindfolded throughout.
You explain that your spirit is about to leave your body and as you do
so your body slumps.
Once you are blindfolded and spirit departed, your partner magician
shuffles a pack of cards. A volunteer then deals them out in a 4 × 4
square. They place some cards face up and some face down at random.
Your partner suggests that to make it harder they will make the square
even bigger and turn it into a 5 × 5 square which they do by adding
more cards at random.
You are blindfolded and lean against the wall at the back of the
room with your back to the proceedings through all of this. Your spirit,
of course, left your body and flies up to the ceiling so you can watch
from above. It has a target to watch in the square of cards. Next a
new volunteer chooses any card from the grid and flips it over. No one
speaks. You are still blindfolded. You can only know which one was
flipped if your spirit really is floating above, watching. You are told by
your partner to return to your body, which you do. As you return, you
bang against the wall as you re-enter too quickly. As you were upside
down on the ceiling, you will be a bit dizzy when you return so you may
wobble about a bit. Return to the front and look at the square of cards.
You have trouble working out which way up the square was if you were
upside down, so struggle to work it out. You turn your head to one side
before finally pointing to the one that was flipped over!
How It Works
The secret is in that extra row and column of cards your partner adds
apparently at random (see Figure 30.1). It isn’t in fact random. It also
isn’t making things harder, but easier. Your partner counts the number
of face-down cards in each row. If that number is odd so that either 1
or 3 of the cards is face down, they put the new card face down. With
that added card, there is now an even number of cards (2 or 4) face
down in the row. If the number of face-down cards in the row is already
even (0, 2 or 4), then instead they put the new card face up. This also
leaves the number even.
The Out-of-Body Experience: Error-Correcting Codes 193
(a)
(b)
(c)
Figure 30.1. (a) An initial layout of cards, some face up (white), some face down
(grey). (b) The extra cards added to the rows and columns make each have an even
number of face-down cards. (c) When one is turned over, it can be identified by
finding the row and column with an odd number of face-down (grey) cards.
Once they have done this for all rows, every row has an even number
of face-down cards.
Next they add cards to the bottom of the columns, and do this in
exactly the same way, ensuring all columns end up with an even number
194 Conjuring with Computation
of face-down cards, including the last card in the corner. That ensures
that the extra column of cards added to each row also ends up with an
even number of face-down cards.
Working out which card has been turned over is now easy as long as
only one card is changed. The row that the turned card is in will now have
an odd number of face-down cards. Similarly, the column containing the
turned-over card will also have an odd number of face-down cards. This
is because in each case one has been turned over either turning a face-up
card to face-down or vice versa. Turning one card either way means the
even pattern is broken but only in one row and one column.
Where the row and column with odd numbers of cards meet is the
card that was turned.
The trick works just as well, of course, however big the square of
cards is. It does not have to be a 5 × 5 square.
Thinking Computationally
We are using a clever, more advanced kind of data representation here,
called an error-checking code. They allow us to spot when mistakes
have been made, and for some codes like this one, allow the mistakes
to be corrected too. If they can correct the error, as here, then they are
called error-correcting codes.
Everything in a computer is ultimately stored as binary bits:
sequences of 1s and 0s. That is also how data is transmitted. If you
stream music, the music is represented as a long stream of numbers,
but those numbers are each a series of 1s and 0s. Those bits can easily
be corrupted in transit: whether zapped by cosmic rays or just corrupted
by other electrical interference along the way. If bits are flipped by all
this noise, then the music you receive will be corrupted. For some kinds
of data, one change could make the message meaningless. We need a
way to deal with the problem. Rather than protecting the message as
it travels over the network, error-correcting codes allow us to just fix
problems when the data arrives.
Instead of thinking about face-up and face-down cards in the trick,
now think about binary 1 and 0. The square of cards could just as easily
represent 16 bits (16 1s or 0s) of computer data. Take 16 bits and
organise them in a square and then add the extra bits to make each row
and column an even number of 1s. Computer scientists call these extra
196 Conjuring with Computation
(a)
(b)
(c)
(d)
(e)
(f)
(g)
Figure 30.2. (a) The message to send. (b) Those bits laid out in a square. (c) Extra
parity bits are added to each row or column. (d) The longer message is sent. (e) A
version of that message is received with the 12th bit flipped. (f) The received bits are
laid out in a square, and the corrupted bit is identified. (g) The message is ultimately
received after the correction.
The Out-of-Body Experience: Error-Correcting Codes 197
bits parity bits. Now you have a slightly longer message of 25 bits that
you can send instead of the original 16.
At the other end, the receiver of those 25 bits can put them into a
square and check all the parity bits are correct. If any row or column has
odd parity, then a bit was corrupted. If there is one row and one column
wrong, then the flipped bit can be determined just as in the trick, and
the mistake can just be flipped back (see Figure 30.2).
If more than one bit was flipped, the receiver at least sometimes also
knows the data was corrupted and can ask for it to be sent again. If you
have more data to send than 16 bits, then you can just split it up into
blocks of bits and add the parity bits to each. The receiver checks each
in turn. Blocks can also of course be bigger than 16 bits as long as it is
a square number. The best size of blocks will depend on how likely it is
that bits will be corrupted.
This error-correcting code is a very simple one. There are much more
powerful representations that can be used to both detect and correct
multiple corrupted bits in data.
Without a variation of the parity trick to help, all digital data
transmitted across the Internet, from text messages, websites to emails,
or streamed music and films, would be full of errors. Given how integral
digital data is now to our lives, it is a pretty important trick.
Human–Computer Interaction
Conjuring
The audience try to keep track of the Aces in a game of cards.
The person who clearly held all the Aces turns out to have nothing.
Instead, you, the magician, have them all.
Computation
Programmers must make their programs easy to use so that
people do not make mistakes using them. Programmers must take
our limitations into account. One such limitation is our focus of
attention. Programs should actively control our attention to help
us both avoid mistakes and recover from mistakes when we do
make them.
The Trick
Get a volunteer to the front, and have everyone else gather around the
table. Explain that this is a trick about why you should never gamble, and
certainly not with magicians or computer scientists. Place the four Aces
201
202 Conjuring with Computation
from a pack of cards face up onto the table to start four piles. Add three
random normal cards face down on top of each Ace showing them to
the audience as you do so (see Figure 31.1).
Explain that you have dealt out four hands for the purposes of
demonstration. Everyone should imagine that a game had just been
played and this was the way the cards fell and that you just happened to
notice where the Aces had ended up. In this game, the person with the
most Aces wins. That means that anyone who can keep track of where
the Aces go will have an advantage and know when to stay in the game
and when to drop out, when to gamble big and when not to gamble at
all. Everyone should therefore watch where the Aces are at all times.
You turn each Ace face down leaving it at the bottom of its pile
and pick up the four piles in order. Point out that if the dealer does
not shuffle the cards, then you know exactly where the Aces are and
so where they will go on the next deal. They are every fourth card in
the pack, so when four new hands are dealt out, all four Aces will land
together in the fourth pile. That person will have a winning hand. Start
to deal out the cards to illustrate it happening. Count: “1, 2, 3 and an
Ace” as you deal the cards face down on the table. On dealing the Ace,
stop and, pointing to the Ace with the next card from the deck, ask the
volunteer to show the audience it really is an Ace, so all can see that you
are definitely right and that person does get the Aces. Have them then
leave the Ace face up on the table so everyone can see that that is where
The Four Aces: Usability and Attention 203
the Aces go. Continue to deal the other cards out, counting “1, 2, 3 and
an Ace” as you do to emphasise that the Aces end up together in the
fourth pile.
Point out that the fourth person (the volunteer) has all the Aces so
will win. If this was a gambling game, they would put lots of money on
the table. They will keep raising the stakes as long as anyone is willing
to match them as they can’t lose.
Two hands are then eliminated, perhaps because like us they know
where the Aces are, or perhaps they just cannot afford to lose that
amount of money. The third hand is yours, so you really ought to drop
out too if you had any sense.
In this game, any player still in the game can now swap a card with
another player if they wish. The person with all the Aces will pass as it
would make their hand worse. You, the magician, do swap a card. Take
the face-up Ace from the bottom of the fourth pile and swap it with the
bottom card of your pile, turning the other card over to show there is
no trickery. Note that you have gained an Ace, but the other hand still
wins as it has three Aces left, pointing to the three face-down Aces as
you say this. Tell everyone that you really can’t win so ought to fold.
Instead, you are going to steal those Aces with everyone watching.
Tell them to keep their eyes on the fourth pile. You will make them
all look away at just the moment when you steal the Aces. On a count
of three, clap your hands in front of the volunteer’s face.
Ask if anyone saw you steal the Aces!
Point out that the volunteer definitely had three Aces and a winning
hand, but now . . . turn the volunteer’s pile over . . . The Aces are gone.
All that money gambled has been lost! So where did the Aces go? Turn
over your pile to reveal that you now have all the Aces. You really did
steal them without anyone seeing.
That is why you should never gamble, and certainly not with
magicians or computer scientists!
How It Works
The secret behind this trick is that you use misdirection, making the whole
audience miss something important even though it is there to be seen.
204 Conjuring with Computation
When the volunteer shows the audience the Ace, they are being
misdirected. You pointed to that card to take their attention to it, and
the volunteer helps keep it there by turning it over and showing that it
is an Ace.
So what do you do that they all miss? You use the card that you
are holding to point at the Ace. As you stop pointing, you pull that card
back to where your other hand with the pack is. You slip that card to
the bottom of the pack and slide off the top card in its place. By the
time the audience’s attention is back to you, unbeknown to them, you
are holding a different card. More importantly, as the previous top card
is now at the bottom, the next Ace is now only three cards away from
the top, not four. That means as you continue to deal the cards, the
next Ace will fall on the third pile, not the fourth as everyone expects.
The third Ace is still four cards away, so it will also fall on this third pile
as will the final Ace. Only the Ace shown to the audience is actually in
the fourth pile. The others are placed in the third from the start, but
the audience don’t know that.
When you later swap the bottom card from the third pile with that of
the fourth, you are moving that lone Ace to join the others. Everything
from this point on is just performance to disguise what has already
happened.
The trick can be made more powerful by combining it with a false
choice (Chapter 21). Rather than choosing the two piles to drop out
yourself, let the volunteer apparently do so. After making clear the fourth
pile with the Aces is theirs, ask them to point to one of the other three.
If they point to the third, then you say that that hand will be yours and
the other two piles drop out. If they don’t, then you say the pile they
pointed to drops out. Then, using time misdirection, you talk about other
things for a while (like how everyone should drop out if they’ve tracked
the Aces not just the person that has). This is to help people forget
exactly what happened. Then, have them point to one of the remaining
two piles. Again whichever pile they point to, you ensure the third pile
stays and the other pile is removed. This makes it seem far more magical
that the Aces end up in that pile, making it seem less likely that the
misdirection happened when it did.
The Four Aces: Usability and Attention 205
17. Introduce the rule that anyone left at this point can swap a card
with their opponent. You will, though the volunteer with all the
Aces won’t as it can only make their hand worse.
18. Turn over the bottom card of the third pile and swap it with the
face-up Ace that is in the fourth pile.
19. Note that the volunteer still has more Aces so will still win but
should watch those Aces closely as you are now going to make
the volunteer blink and steal the cards when they do.
20. Count to three, then clap loudly in front of the volunteer.
21. Ask if anyone saw you steal the Aces.
22. Turn over the fourth pile to show the Aces are gone.
23. Turn over the third pile to show you now have all the Aces.
and miss something important. Good user interface designers can use
the same understanding of cognitive science to make people look in the
right place at the right time. With good design, that takes account of
our limitations in the same way that a magician exploits them, designers
can help people avoid making mistakes or recover when they do.
Programming is not just about writing programs that work. It is also
about making them easy to use. Computer scientists call designing to
make things easier to use, usability. Many programmers write programs
with themselves in mind rather than the actual users who then find
them much harder to use than necessary. After all, the programmer
understands deeply how it works. The users do not. Many programs,
and features of programs, are so hard to use that they are never used.
Making a program easy to use is all about understanding the way our
brains work and what their strengths and limitations are.
Good programmers will want to know if people make mistakes with
their software and want to fix the problems. When people make mistakes
using their software, a good programmer will always ask, “How can I
improve it so that no one can possibly make that mistake again. If I
can’t, then how can I design it so they will always realise they made a
mistake and know immediately how to fix the problem.”
Some usability problems and the resulting mistakes are just mildly
irritating, like closing a program when you meant to minimise it, so
having to open it again. Others can lead to a lot of wasted time, like
forgetting to save the homework you’ve spent hours doing. Sometimes
the consequences of a mistake are impossible to undo: if you set your
alarm clock but didn’t realise the volume was muted, so miss an
interview, for example. In other situations, the consequences can be
about life or death. Hospitals are now full of computer-based gadgets
helping cure people or keeping them alive. Someone wrote the program
for every gadget. Someone did the interaction design (designed the way
users use the program), and so made it either easy to use, or conversely,
easy to make mistakes with, or most likely they made it somewhere in
between. If a nurse accidentally types in a dose that is 10 times too
much into an infusion pump, a machine pumping life-saving drugs into
a patient, then the mistake could kill them. High doses of otherwise
wonderful drugs are often toxic. Avoiding medical error matters.
208 Conjuring with Computation
In all the above cases, different designs can prevent the problems
or at least make them far less likely to happen and easier to spot and
correct. Word processors can save documents every few keystrokes rather
than leaving you to do it. That alarm clock could detect that the volume
is muted so know the set alarm will have no effect and ask you to fix the
problem before you finish setting it. The infusion pump could know the
safe range of doses of each drug and alert the person inputting it that
something is wrong.
Thinking about why a nurse might enter the wrong dose without
realising leads us back to our trick. There are many possible reasons,
but one issue is whether their attention was in the right place. If they are
typing in digits on a calculator-style digit keypad, then their attention
has to be on their fingers to see what they are doing, not on the screen
to see their mistake. When finished, their eye is likely drawn to the
start button. Perhaps we can pull the magicians’ trick and draw their
attention to the screen itself by making it flash so the nurse can check.
Even better, we can use a different way of entering numbers that means
the nurse’s attention stays on the screen as the numbers are entered.
One way to do that is to enter numbers using a cursor-based system.
For example, you might have two buttons that move a cursor left and
right from one digit to the next, and two more to change the value up
or down. Once the cursor is in the right place, a person’s attention is
then no longer on the buttons but on the screen watching the numbers
change, where it needs to be.
Controlling people’s attention is as important for a programmer as
it is for a magician.
Computers solve problems for people. If people are to use the
programs developed, then those programs must be designed to be usable.
They must be designed to be easy to use, despite the limitations of our
brains. Programmers must make sure mistakes are both hard to make
and easy to recover from.
Magicians design magic systems so that the whole audience make
the same mistake at the same time. Computer scientists must design
computer systems so that no one does if their systems are to be usable
and safe. They can use the same tricks, the same cognitive psychology,
to do so. Rather than directing a person’s attention away from things
The Four Aces: Usability and Attention 209
Figure 31.2. Magic tricks and programs are composed of the same basic compo-
nents: an underlying algorithm or method and the way it is presented or interaction
design.
Supernatural Suggestion
Visual Salience
Conjuring
You show five playing cards on the screen and ask everyone to
choose the one their eyes settle on. You then show the cards with
one removed: the one you think most people will have chosen.
When you ask, it turns out EVERYONE chose the removed card.
Computation
What you see depends on where you focus your attention, and
where you focus your attention depends partly on how salient the
things in the scene are. If you want important things to be seen
on a screen, then you have to make them visually salient.
The Trick
You explain that you are going to attempt an experiment in the power
of suggestion. Whilst any magician can make one volunteer from the
audience choose the card they want, you, by subtle suggestion, will
attempt to make as many people in the audience as possible choose
211
212 Conjuring with Computation
the card you want them to. You will try and draw everyone’s eyes
to the same card. Explain that what you are about to try requires a
deep understanding of the way the brain works, of cognitive psychology.
You explain that you will display on the screen a picture containing
five playing cards and ask everyone to choose one. They can choose any:
perhaps an end one, perhaps the middle, perhaps the ones in between,
perhaps to the left and perhaps to the right. They should let their eyes
settle on one without too much thought and then stare only at it. It
will be their choice. Once they have chosen, they should stand up and
remain standing up.
You show the slide with the five cards, reminding them to go with
the first one their eyes settle on and stick with it, and then stand up.
When all are standing, you switch to a blank slide and tell everyone
they must think about their card and only their card. They must
concentrate purely on it. You will then deal out the cards again, but
you will have removed one card, the one you were trying to suggest
to everyone that they pick. You tell everyone that if they picked the
card you have removed, then they should sit down. Note that with five
cards you would expect one in five of the audience to sit down if you
had no powers of suggestion. Less than that and you would have done
the opposite of what you intended. More and your powers of suggestion
have worked at least to some extent.
You now show a new slide with the four remaining cards, without
the one you were forcing them to choose. Everyone sits down!
How It Works
This is a very simple trick. How it works is that you just replace ALL
the cards with a set of four cards that are similar. Everyone’s card has
gone, so everyone sits down. To make it work, you need to use cards
throughout that are very nondescript: so especially no court cards or
Aces. Make them all 7s, 8s and 9s of similar red and black suits and
swap them for different cards that are also all 7s, 8s and 9s of similar
red and black suits. This ensures that the ones not chosen are not very
noticeable.
Supernatural Suggestion: Visual Salience 213
similar cards. It works because some things are more noticeable, more
visually salient, than others. That is why we use 7s, 8s and 9s as they
are less salient than the court cards and Aces. We want them all not
really to be noticed.
Our eyes are drawn to some areas of a scene more than others, and
as we have seen, once our attention is focussed, we miss other things
completely. So what are our eyes drawn to? Our eyes are, for example,
drawn to areas with high-contrast differences. Points in such areas of a
scene are visually salient.
This matters when designing interfaces as it can be used by the
design team to ensure people’s attention is drawn to the right places at
the right time. In doing so, the interaction designer can help ensure that
users of the interface do not miss important things. Having spent 20
minutes choosing things on a shopping website, the last thing you want
is to then not be able to buy them because you can’t find a checkout
button! That is something your eyes should easily and immediately be
drawn to.
Conjuring
A member of the audience calls out 10 random numbers. You
write them down, mix them up and spread them out face down,
and a volunteer picks one. Somehow, you are able to tell them the
number they picked.
Computation
It is really important that the design of a computer system prevents
“automation surprises”, where a user loses track of the internal
state of the system. An important interaction design principle to
do this is that of “visibility of system status”. This involves ensuring
that the important parts of the internal state of a computer system
are clearly visible to the user. That means they do not have to
mentally track them, reducing the likelihood of such mistakes.
215
216 Conjuring with Computation
The Trick
Do this as an apparently spontaneous dinner-table trick. Take a piece
of blank paper you happen to have in your pocket and tear it up into
16 pieces. Get someone from the table to call out 16 numbers at random
that you write on the pieces of paper. Shuffle the pieces of paper so they
are thoroughly mixed up. Next, divide them into two piles face down on
the table. Ask someone else to point to a pile allowing you to eliminate
half of the possibilities leaving only eight pieces of paper. Now spread
them out face down on the table and ask a third person to pick one and
look at it without telling anyone what it is. Explain that you can tell
what it was from the micro-expressions that pass over their face as they
look at it. You tell everyone which number you think they have picked.
The person shows everyone the one they took. You got it right.
How It Works
What the audience do not realise is that you don’t write each number
they are calling out, but instead write the first number they said on
every piece of paper.
All the numbers on the cards are the same, so of course, you know
the number on the card they predict. It is only magical because the
audience just assume that you are writing the numbers down as spoken.
For a simpler version, you can skip dividing the paper into two piles.
It just gets more people involved and adds an extra layer of confusion
over what is happening.
Conjuring
This false cut leaves the whole pack unchanged.
Computation
A way to help make interactive systems easy to use is to give them
a clear and simple “conceptual model”. The way they work should
be obvious. One way to do this is to use a design metaphor: you
make the system remind its users of some real-world thing they
already know how to use and then have them interact with the
computer system in a similar way. On the other hand, if you use a
metaphor that does not match the actual design, then the system
will be very hard to use.
The Trick
Place the pack on the table (pile 1). Now cut off the top third or so from
this pile and place these cards to the right. Call this pile 2. Now cut half
of what’s left in pile 1 and place this further to the right of pile 2. We’ll
call this pile 3. All that remains is for you to pause and then reassemble
221
222 Conjuring with Computation
the pack. Place pile 2 onto pile 3 and then take this combined pile and
place it on the cards in pile 1 (see Figure 34.1).
To the audience, this looks like a fair series of cuts, but if you try it
with a pack, you will discover that the pack is in exactly the same order
at the end as it was at the beginning.
How It Works
You have just confused the audience. All that takes is to put the piles
back together in a different order to the way they were taken apart!
They are expecting that a series of cuts is fair. Because we put them
back together in a different order to the way the piles were cut, it is easy
to lose track of the detail of what is actually being done which is to put
each third back on top of the one it started off on top of. The pause in
the middle adds a little time misdirection too.
The magician is deliberately projecting an idea of how the world
works that is wrong. Everyone who has played cards knows without
thinking that if you cut a pack of cards, they end up in a different order.
The Three-Way False Cut: Conceptual Models and Metaphors 223
mental model of how they expect it to work and what they have to do.
Those expectations may or may not match the actual system. If it is an
accurate version of the system behaviour, then it will help the person
use the system with ease. However, if it is not accurate, it leads to
misconceptions of what to do when using the system.
For example, if you have seen lots of hand dryers in public toilets
that are activated by a large button, then when you see a new hand
dryer, you will form a user model that includes the expectation to press
a button. If in the place of the button you see something button-like,
then this will reinforce that user model. You are likely to press it without
thinking. This is likely even if it is actually just a sign in a box saying
“DO NOT PRESS: place hands under dryer to activate”.
Magicians set up system images of the magical system explicitly so
that users form the wrong mental model of what is happening. That is
what is happening in this cut. Everyone has seen people cutting packs
of cards before, and they know that it is one way to mix up a pack and
bring random cards to the top. The first cuts look like that is what is
happening, and rebuilding the pack seems to fit what happens with a
sequence of fair cuts. The magician’s presented system image makes the
audience form a user model that the pack is being randomised a little.
They do not follow the detail but instead assume they know what is
happening.
Programmers and interface designers need to do the opposite. Bad
designs work like a trick making the user form a faulty mental model,
and good designs help them form a correct one: so hand dryers that
are triggered by sensors should have nothing that looks like a button
that will reinforce the wrong mental model. Better still they should look
completely different and ideally have some very obvious visual clue that
you put your hands in the right place. The same considerations apply to
interfaces for more complex software.
Design metaphors
One way to help users quickly form the right mental model is to make
the controls look like some familiar real-world thing. For example, a paint
program might have a set of colours to choose from that look like they
226 Conjuring with Computation
are laid out on a painter’s pallet. To change colour, the user moves their
virtual brush to “dip” it in the paint on the pallet.
This is called a design metaphor, and it helps the user understand
the way the system works. The actions needed to achieve a task,
like choosing a colour, match those in the real world when dipping a
paintbrush in paint. The interaction design principle to remember is
that if it looks like something familiar, then it should behave like it.
If it looks like a painter’s pallet, then it should behave like a painter’s
pallet . . . or it will cause confusion.
To take another example, the metaphor of a dustbin is used on many
computer desktops so you naturally understand that files you put there
are “rubbish” so intended to be deleted. However, they are not deleted
immediately. That happens only later when we empty the trash. We
have that expectation, and so if that is the way the system works, we
naturally understand the right thing to do. There is no actual dustbin
in a computer, of course, it is just a metaphor. If instead dropping
something in the virtual bin deleted it immediately, then people would
likely often make mistakes. That would need a different metaphor to
help: a shredder metaphor perhaps. When we shred things, we expect
them to be gone for good immediately. It is what we see (the icons), the
language used, and the feedback we see that sets up the expectation.
Similarly, in the trick, the magician sets up the metaphor of doing
a cut, and so we use that as our conceptual model watching the trick.
We use that metaphor to judge what happens. What is done is close
enough to our expectations that no alarms are raised. We know what is
happening. The design model, what is actually happening, is, however,
different. As the magician hopes, our expectations mean that we make
a mistake and do not realise the pack is unchanged.
When we design and evaluate computational systems, we don’t want
people getting confused, so need to ensure we take expectations like this
into account. We need to design in a way so that the interface leads to
the user’s conceptual model matching the design model. We also need
to check whether we have succeeded, evaluating the design with real
users to ensure that they do understand the metaphor and it sets up the
correct expectations.
The Three-Way False Cut: Conceptual Models and Metaphors 227
Conjuring
This trick is about finding troublemakers (i.e., jokers) in a
classroom and getting them to the front of the class. A volunteer
thinks of a number, deals out that many cards, and amazingly they
have brought the only joker in the pack to the top.
Computation
No one has a perfect memory. We all forget things, especially
short-term things, as our limited working memory can quickly be
filled. Forgetting something we know we have to do in the future
is a particularly common mistake, especially in stressful situations.
Interaction designers need to keep our memory limitations in mind,
ensuring the interface only requires us to remember things if they
can’t be avoided.
The Trick
Tell the audience that teachers must be able to spot troublemakers: the
jokers of the class. You will show them a test for prospective teachers
229
230 Conjuring with Computation
to see if they can do the necessary crowd control. The person being
tested will use their natural talent at detecting problems before they
happen. They will try and find a joker in the pack. A volunteer thinks of
a number, say between 2 and 8, and announces what it is. You shuffle
the pack and then show them what you want them to do. You count
out their chosen number of cards aloud 1, 2, 3, . . . from the top of the
pack of cards onto the table, face down. When their stated number of
cards have been dealt, turn over the last card dealt and point out what
it is. Place the cards back on top of the pack and hand the whole pack
to them. Take some time to remind them that they are looking for the
joker, which stands for a naughty student who is about to cause trouble.
They must get them to the front of the class (so the top of the pack),
where you can see them before they have a chance to make mischief.
While doing this, you shuffle the pack.
They now do as you instructed, counting out the cards, 1, 2, 3, . . .
up to their number. Before they turn over the top card, take back the
rest of the cards that weren’t dealt out and spread them face up on the
table, pointing out that there are no jokers. Pull out some single cards
and say aloud what they are, to emphasise they are not jokers. Remind
everyone that you shuffled the pack. Now have them check to see if
they found the joker by turning over the top card of the pile they dealt.
Amazingly, the card they turn over is the joker!
Despite it being the only one in the pack, they have brought the
troublemaker to the front with no trouble at all. They are a natural at
crowd control and a natural for the classroom.
How It Works
To make the trick work, before the performance, you simply place a joker
on top of a shuffled pack. Each time you shuffle the cards during the
trick, you do a fake shuffle that leaves the top cards unchanged while
appearing to mix the cards up (e.g., a False Top Overhand Shuffle: see
Chapter 18).
When you count the cards onto the table, do it casually so that they
think nothing of it: you are just showing them what to do.
The Joker in the Pack: Memory 231
The joker starts at the top of the pack. The fake shuffle leaves
it there. Suppose the person chooses the number 4, then in your
demonstration, you count 4 cards onto the table face down. The joker
is now at the bottom of these cards, so in this case, it is the fourth from
the top. You place the cards back on the top of the pack so it is now
the fourth card in the whole pack. A similar thing happens whatever
number is chosen (see Figure 35.1). The fake shuffle again leaves the
Figure 35.1. If the volunteer picks number 3, then the joker ends up in the third
position.
232 Conjuring with Computation
top cards alone, so given they were asked to pick a small number,
the shuffle should leave their card in the same position from the top.
You hand the pack to them, and they now do exactly as you did.
However, now counting four cards out actually brings the fourth card
(the joker) back to the top. It is the card they later turn over. Whatever
number they choose, they exactly undo what you did.
Your extra patter between you dealing and them dealing and also
between them dealing and then finally revealing the joker are important.
It takes the audience’s attention away from the cards dealt and from
the pack. The time delay it causes also helps, as this, combined with
all the extra things you are doing, increases the chance that they forget
exactly the detail of what you did at the start. This is time misdirection.
If you are not confident doing fake shuffles, then leave them out: the
trick still works as long as you have time misdirection, it is just slightly
less powerful.
11. Have them turn over the top card of the dealt pack.
12. Point out they brought the joker to the top of a shuffled pack so
are a natural at spotting troublemakers.
Conjuring
You test the psychic abilities of a volunteer and show that they
have great power. They successfully match pairs of cards that had
been mixed up, even though those cards were face down the whole
time.
Computation
Ideally, you do not want your software just to be usable, you want
the user to have a great experience. To give a good experience,
you need to engineer that experience.
The Trick
First, you need two sets of matching cards. You can do it with any
cards that can be paired. Zener cards, which you can buy from magic
shops, are ideal as they are the cards that scientists have used to test for
237
238 Conjuring with Computation
psychic powers (or at least to prove people aren’t psychic). They have
different patterns: a circle, a cross, wavy lines, a square and a star. You
can, however, do the trick with happy family cards or playing cards if you
have two identical packs. We will do the latter here. You also need four
“magical tokens”. Anything that looks a bit magical will do like gems,
toy crystals, tokens or just coins.
Beforehand, take five cards: if using normal cards, an Ace, a King,
a Queen, a Jack and a 10, and shuffle them. Then take another set of
identical cards and put them in the same order. Place one pile of five
cards on top of the other. So, for example, if the first five end up in the
order King, 10, Jack, Ace and Queen, then the whole sequence is King,
10, Jack, Ace, Queen, King, 10, Jack, Ace and Queen.
Do a Cyclic False Cut (Chapter 20) by showing a volunteer from
the audience the cards face up, fanned out in your hands so they can
see there is a mix of different cards. To then mix up the cards, turn the
cards face down and allow the volunteer to point to a card. Split the
pack at that point, putting the top part to the bottom. Keep doing this
until they are happy the cards are mixed. If doing the trick as close-up
magic with people gathered around the table, you can have different
people touch the cards.
Next deal the top five cards, one at a time, into a pile on the table.
Place the remaining undealt cards into a second pile beside them.
Tell the volunteer that, now the cards are mixed up, they must try to
end with two cards that match without seeing any of the cards. This will
prove whether or not they have psychic or magical powers. Explain that
they have four magic tokens to channel their powers. Each will stand for
a move. A move involves taking the top card from one of the piles and
placing it at the bottom of the same pile. They can put all four tokens
on one pile, and none on the other; two on each; or three on one and
one on the other. It is their choice, remembering that their aim is to
be left with two matching cards. You will do the number of moves on
each pile that corresponds to the number of tokens there. The volunteer
will therefore be the one deciding what happens by placing the magic
tokens on the piles. Once the tokens have been placed, you make that
many “moves” on each pile. A “move” just involves moving a card from
the top of the pile to the bottom. So if a pile has three tokens placed
Are You Psychic? User Experience and Engineering Delight 239
on it, you move three cards from top to bottom, one at a time (see
Figure 36.1).
Next, remove the top card from each pile and place them aside in
a pile together. Place one of the magic tokens on top of them to show
they are discarded. It does not matter what they are; it is the cards left
at the end that must match.
Now there are four cards in each pile. Give the volunteer the three
remaining tokens to repeat the process. Once the moves are done,
remove the top two cards from the piles again, placing a token on them
as before. There are now three cards left in each pile and two tokens. Do
it all again, again removing the top card from both piles. This leaves two
cards in each pile and a single magic token. This is their final chance to
get it right. One move remains, and one card can make all the difference.
They choose where to put the token, you make the last move, and the
top two cards from each pile are discarded.
Now it’s time to reveal the final two single cards left on the table.
No one has seen them through the whole process, they were mixed at
the start, and the number of cards moved was completely chosen by
the volunteer. Ask the volunteer again if they think they have psychic
powers. Tell the audience to cheer if the cards do match, but give a loud
groan if they don’t. Turn one card over, asking if they think the other
will be the same, then turn the final card over. They match!
The volunteer chose freely which cards to eliminate, so it was their
secret psychic powers that came through to ensure a match at the end.
Figure 36.1. Tokens are spread between the two piles. The number of tokens is
always one less than the cards in each pile (here five cards, so four tokens). The same
number of cards are moved from top to bottom as tokens are placed on a pile.
240 Conjuring with Computation
Give their jaw time to drop, then say that once you have found someone
with magical powers, it is worth checking how powerful they are. One
pair at a time, dramatically reveal that all the pairs of cards they removed
match too!
Point out that they should stop denying their powers, go and buy
a superhero costume, and start using their powers for the good of
humanity!
c. Remove the top card from each pile and place them aside
together with one of the tokens on top.
d. Give any remaining tokens back to the volunteer.
9. Turn over the two cards that remain to reveal that the volunteer
does have psychic powers! Take the applause.
10. Point out that once you have found someone with psychic
powers, it is worth seeing how powerful those powers are.
11. DO THE FOLLOWING FOUR TIMES.
a. Turn over one of the remaining pairs put aside during the trick
to show that they match.
12. Suggest the volunteer should stop denying their powers, buy a
superhero costume, and start using their powers for the good of
humanity!
How It Works
The trick works because of a combination of the starting order, the way
the cards are then mixed up, the way they are placed on the table at the
start, and then the fact that there is always one less token to place than
the number of cards remaining in each pile.
First, the cards start off in a pile with five cards followed by the
same five cards. The way the cards are mixed is then via a Cyclic False
Cut (see Chapter 20). Each time you cut the pack at the point of their
choosing, you are just rotating the order of the cards. At all times, there
are still five cards followed by the same five cards as at the start. The
sequence just starts with a different card. It is the cyclic order that
matters though, and that is unchanged.
Next, you count the first five cards out onto the table. Critically,
this reverses their order. The remaining cards are placed straight onto
the table. Their order is unchanged. That means the two piles hold the
same five cards but in opposite orders.
We finally always have one magic token less than the cards in
each pile. It turns out that when the cards are organised as they are
(an identical sequence except with the order reversed), how ever you
share the tokens between the piles, you will always end up with a pair of
matching cards on the top of the piles. That pair of matching cards is the
242 Conjuring with Computation
pair that is discarded. We will leave the details of why this happens to
the next chapter (see if you can work it out in the meantime). Here, the
important point is that on every round, you are actually guaranteed to
discard two cards that match. That means the final pair must too, and
it allows you to take the audience by surprise with your twist of a final
reveal.
Conjuring
We return to the trick that proves an audience member has
superhero powers and prove here it will always work.
Computation
When proving that loops do the correct thing, the idea of a
loop invariant provides the basis of logical reasoning: reasoning
by induction.
The Trick
Rather than showing you a new trick, we return to the previous one
(“Are You Psychic?”, Chapter 36) and look at why it always works.
The trick is made up of several parts, but the core part is the repeated
placing of tokens. Below, we give that key part of the algorithm that we
now focus on. The first part of the trick sets up a precondition: on the
table are two piles of five cards each, face down, with the same cards in
each but in the opposite order. At the end of this part of the algorithm,
it guarantees the postcondition that all pairs of cards match.
247
248 Conjuring with Computation
How It Works
It works because each round of placing tokens, moving cards and
discarding the top two as a pair guarantees that the pair discarded
matches. We have said this is so but how can we be sure it always works?
We need to do some logical thinking. It will be based on something called
a loop invariant and a form of reasoning mathematicians call inductive
proof.
Thinking Computationally
Loop invariants provide a way to reason about the correctness of loops.
They are properties that must always be true at the same point in
the instructions of a loop, usually the start. The property holds every
time the loop’s instructions are executed and get back to that point.
The situation is changed by the instructions in the loop so that the
property temporarily ceases to hold, but is then restored so the property
becomes true again. We use this property as a way to prove that at the
end a related desired property holds. In our trick, that final property is
that all pairs match. This property is the one the loop is being used to
Are You Psychic? (Continued): Loop Invariants and Inductive Proof 249
the invariant. However, we know several facts that do stay the same, so
can form our loop invariant:
does not rely at all on the actual number of cards involved, so the trick
will work technically (and the proof applies) whatever the number of
cards we start with.
Oddly, a property that stays the same is the key to understanding
how doing repetitive computation changes things in ways that make an
algorithm always work. To really be able to code with loops and be
sure the code is correct, you need to be instinctively doing this kind of
reasoning every time you write a loop.
Conjuring
You meld your mind with that of a volunteer so that you can
predict a fact about the way the red and black cards fall unseen.
Computation
Rigorous argument is one way to convince ourselves that a trick
or program works, but we can go a step further and use algebra
to do this. We describe the trick or program with maths, do some
algebra on the resulting “mathematical model” and prove desirable
properties hold. This approach is a vital way to check safety-critical
software is correct.
The Trick
Ask for a volunteer to take part in the mind meld. Shuffle a full pack
of 52 cards using a normal overhand shuffle (Chapter 15). Deal the
cards into two equal piles and hand one pile face down to the volunteer,
255
256 Conjuring with Computation
keeping the other. The volunteer should not look at their cards, but you
turn your pile face up. Now say you are beginning the mind meld from
you to the volunteer. You will control their actions even as they think
they are making their own decisions.
Select, at random, some BLACK cards from your pile and place
them in a face-up pile in front of you, saying how many BLACK cards
you have chosen. Ask the volunteer to select the same number of cards
(without looking!) from the top, middle or bottom of their pile. They
should do whichever feels right as it will be the mind-meld working!
They should place their chosen cards, face down, in a new pile in front
of your BLACK pile. Explain this pile will be influenced by your BLACK
pile.
Now repeat the process but take a random number of RED cards
from your hand and place them in a new face-up pile next to your black
pile. Tell the volunteer how many RED cards you have chosen. Again ask
the volunteer to blindly select the same number of cards from anywhere
in their pack. They should place them in front of your RED pile. Explain
that this new pile will be influenced by your RED pile.
Repeat the process, alternating between choosing BLACK cards and
choosing RED cards until you have run out of cards. If done correctly,
the volunteer will run out of cards at the same time.
There is now, on the table (see Figure 38.1):
Remind everyone that your mind meld influenced the volunteer’s choice
of random cards and you can prove it. Even though the pack was shuffled
and the volunteer chose cards at random, you used the mind meld to
The Red–Black Mind Meld: Abstraction, Algebra and Proof 257
Figure 38.1. The four resulting piles: Piles 1 and 2 face up with each card picked
by the magician to be of the right colour; Piles 3 and 4 face-down cards picked by
volunteer.
make sure they put exactly the same number of red cards in front of
your red pile as they put black cards in front of your black pile.
Ask the volunteer to first take their pile of face-down cards in front
of your RED pile and count aloud the number of RED cards in it onto
the table. Then ask them to take their face-down cards in front of
your BLACK pile, and similarly count aloud the number of BLACK
cards.
Through the mind meld, you caused the volunteer to select the
same number of RED cards in their RED-influenced pile as BLACK
cards in their BLACK-influenced pile. One card out and it wouldn’t have
worked!
258 Conjuring with Computation
How It Works
Of course, it’s not mind melding. It’s just computation and mathematics.
The trick is self-working. As usual, if you follow the steps, it is bound
The Red–Black Mind Meld: Abstraction, Algebra and Proof 259
Thinking Computationally
So how can we be sure that this trick always works? Rather than doing
it lots of times (testing) or just using informal but logical reasoning, we
are going to use algebra.
The first step is to create a mathematical model of the trick.
By that we just mean describe it mathematically. Doing so involves
abstraction: focussing only on the information about it that matters
while ignoring anything that doesn’t. A mathematical model is just an
abstraction of a part of the world we are interested in. This trick is about
the numbers of red and black cards. The actual values and suits of the
cards do not matter at all. We can ignore that (abstract away those
details) and just focus on the count of each colour that ends up in each
pile.
We, of course, have no idea how many reds or blacks ended up in any
of the piles as the pack was shuffled. We can’t actually put numbers to
anything, apart from the fact that there are no black cards in the face-up
red pile and no red cards in the face-up black pile at the end. Instead,
we just give a name to the unknown numbers of red and black cards in
each pile. Then we refer to those numbers by name and do not worry at
all about the actual values. Those values, the number of cards in each
pile, will be different every time we do the trick. A mathematician calls
these names, variables, though the word here means something slightly
different to the kind of variables programmers talk about, it is a similar
idea of giving values names.
First, we give names to the number of red and black cards in each of
the four piles. First, consider Pile 1 (your face-up RED pile). We will call
the number of red cards there, RED1, and the number of blacks there,
BLACK1. The number of red and black cards in Pile 2 (your face-up
BLACK pile) will be called RED2 and BLACK2, respectively. Similarly,
we will say that the face-down Pile 3 contains RED3 reds and BLACK3
blacks. The face-down Pile 4 contains RED4 reds and BLACK4 blacks
(see Figure 38.2).
260 Conjuring with Computation
Figure 38.2. The four resulting piles with names given to the number of red cards
and number of black cards in each.
cards in each pile as in Figure 38.2. We can do the same thing for the
black cards. We have to use the names here because we don’t know the
actual numbers.
We can, of course, make use of our first two facts, which do tell us two
of the numbers, to simplify Facts 3 and 4. Facts 1 and 2 say we can
replace RED2 with 0 and we can replace BLACK1 with 0 as they are
the same.
This gives new facts:
Just as we did with the first two facts substituting BLACK1 for 0 and
RED2 for 0, we can now do a similar bit of logical reasoning substituting
for the things we know about RED1 and BLACK2 from Facts 7 and 8.
We replace RED1 in Fact 5 with RED3 + BLACK3 as they are
the same value. In the same way, we replace BLACK2 with RED4 +
BLACK4 in Fact 6. Our new versions of those facts are:
Now we have two different things that we know equal 26 so the left-hand
sides of both those equal signs equal each other. Combining Facts 9 and
10, we get
We have two lots of RED3 on one side and two lots of BLACK4 on the
other. That can be written
Then finally, dividing both sides by two, we get a really simple fact
that is guaranteed to be true as it followed mathematically from our
original facts.
The Red–Black Mind Meld: Abstraction, Algebra and Proof 263
This tells us that the number RED3 will always be the same as the
number BLACK4 at the end of the trick.
Verifying programs
If we can prove that a magic trick always works, then we can prove
that a program does too in a similar way. We use abstraction to
create a mathematical model of what the program does. We then
use algebra to prove properties about the model. Those properties
tell us facts about what the program does in the real world. This is
called program verification. For example, we might prove that if the
program controlling a medical device was set to deliver a fixed number
of milligrams of a drug over a fixed number of hours, then that is what
it always does, whatever the actual numbers involved.
When lives depend on a program always working, it is no good
that we tested the program and decided we were 99% sure it won’t
kill anyone, especially if it is used by millions. Proofs can tell us that a
program always works (as well as showing that our tricks do).
264 Conjuring with Computation
Conjuring
The Doomsday Clock predicts the most important hour in your
future. Even with a free choice to start, its hour hand ends on a
predicted hour.
Computation
By abstracting away detail until we get to the very core of a trick,
or algorithm, so its most general version, we can then build it back
up into new tricks or algorithms.
The Trick
Before you start this trick, you need to create a big clock face with
numbers from 1 to 12. Make a single movable hour hand for the clock,
drawing a fixed minute hand pointing to 12 (see Figure 39.1). You also
need to draw a second smaller version of the clock on a piece of paper
with only the number 1 at the 1 o’clock position, and with a fixed hour
hand pointing at the 1 (see Figure 39.2). This is your prediction. Place
267
268 Conjuring with Computation
Figure 39.1. The Doomsday Clock. The minute hand is fixed. The hour hand moves.
Figure 39.2. Your prediction for the time the Doomsday Clock will stop.
It may be the hour they will get married, their first baby is born, or
perhaps the hour they will die. Who knows? What is for sure it will be
an hour in the future that will be really important to that person’s life.
First, ask for a volunteer. Explain that you will use the clock to find
the hour in their future that will be most important to them personally.
It will take only a few turns of the hour hand until the hour hand passes
12 once more. Ask them to choose a number from 1 to 12. Write it
down as a word (so if they choose 2, write T-W-O). Have them then
move the hand of the clock one place round the clock for each letter in
their chosen word. If they choose TWO, for example, then they move
T-W-O so three places on. Tell them they will be using the words of
numbers like this to jump the hand forwards three times. They are now
at a new position on the clock and at a new number. This gives a new
word to use to count further. They do the same again with that word.
They then do it again a final time at the new hour they land on, and
then stop the clock at that point.
At whatever place they end up, announce that their personal clock
stopped at that time. Point out it was their free choice of the original
number. Now, explain that, to show the power of the clock you already
used it to do the prediction for them. Turn the clock over revealing the
envelope with your prediction. Ask them to open the envelope and read
the prediction of their doomsday hour. Amazingly, it is the same as on
their clock. That is the power of the Doomsday Clock!
How It Works
This trick is based on the same property to do with the randomness of
the length of words from the Wizardly Book Magic trick of Chapter 6.
It means in this case that in three steps all 12 numbers you can choose
take you to the same place. The length of words does not actually vary
very much. The words in the clock all have lengths between 3 and 6
letters (see Figure 39.3). As it is the length of the word we use rather
than the word itself, that means the original 12 numbers to freely choose
from actually only give four possibilities: lengths of 3, 4, 5 or 6.
Since we always start at 12, after counting out the first word, there
are only four possible places we could be at after the first step: the hours
3 to 6 (see Figure 39.4). Now the length of THREE is 5, but the length
Figure 39.3. The lengths of words corresponding to numbers on the clock face.
The Doomsday Clock: Generalisation 271
of FOUR one place after it is only 4, so they will both count you to the
same next place: hour 8. The lengths of FIVE and SIX have the same
pattern, and both take you to the position of hour 9. There are now,
after 2 steps, only two places you could be. Those positions, 8 and 9,
have the same pattern of length, as the previous ones, with the second
being one less letter in length than the first. They therefore both jump to
the same place: 1. So after only three steps, all options have surprisingly
led to the same place. That is your prediction.
Rather than telling the volunteer to stop on the third step, you could
alternatively say they will stop when they have passed 12 once more as
this is equivalent.
272 Conjuring with Computation
Thinking Computationally
Once you understand the basic principle of a trick, you can reuse that
principle as the basis of different tricks. This trick uses exactly the same
principle as Wizardly Book Magic (Chapter 6). Rather than using words
in a book, here we are just using the words for the numbers on a clock.
If you have invented a trick like Wizardly Book Magic, then strip it
down to its core principle (abstracting away the details), and you have a
generalised version of the algorithm. You can then build a new trick by
providing a new context. Figure 39.5 gives a generalised version of our
two variations of this trick. It is just a description of the mathematical
principle behind the trick as an algorithm.
Programmers use generalisation like this a lot. Rather than writing
code or creating an algorithm just for one specific purpose, once we have
solved a problem in a new way, we generalise it so it can be used in lots
of situations.
If we have written code to search for names in an alphabetically
ordered list of names (e.g., in an online telephone directory), we might
generalise it so that it can also be used to search for other text that
is ordered alphabetically. We might generalise it further, modifying the
code so that it can be used to search for other data ordered in some way
(e.g., numbers in numeric order). The increasingly generalised versions
have a wider use and are more abstract versions of the original concrete
code. They all are based on the same underlying mathematical principle
just as in our trick.
Figure 39.5. A generalised version of the Doomsday Clock and Wizardly Book
Magic.
The Doomsday Clock: Generalisation 273
Free My Three
Divide and Conquer
Conjuring
Three cards of a kind chosen by a volunteer are lost in a pack.
After repeatedly discarding cards with no sign of the chosen ones,
they are eventually revealed as the last cards left.
Computation
Decomposition involves breaking a problem into smaller problems
that are easier to solve. Divide and conquer is a powerful version
of this where these smaller problems are the same problem as the
original (just smaller). Those smaller problems are solved in the
same way. This gives a way to create very fast algorithms.
The Trick
For this, you need a full, shuffled deck of 52 cards. Ask a volunteer to
find and remove three cards that are three of a kind, such as three of
the queens. Have them show the audience, but not you. Everyone must
remember those three cards.
275
276 Conjuring with Computation
Take the pack back, without those cards, give them a shuffle and
then casually make three piles. The piles must contain 10, 15 and
15 cards. It’s fairly easy to push off blocks of three cards at a time.
The first pile, with 10 cards is three blocks of three with one extra. The
15-card piles are simply five blocks of three. You will be left with nine
cards in your hand (but your audience won’t know that as it looks like
you have just built three random piles. Pop these left-over nine cards to
the side for the moment.
The three selected cards are put onto the three “random” piles and
“lost”, but in a special way (see Figure 40.1). The first card goes onto
the 10 pile. Next, ask a spectator to cut off a chunk of cards from
the adjacent pile, the 15 pile, and pop them on top of the chosen card
burying it.
The next chosen card goes on top of the pile they have just cut the
cards off. Again, once they place the chosen card on top, they bury it
by placing a chunk of cards from the remaining 15-card pile onto it.
Figure 40.1. How the three chosen cards are lost in the pack.
Free My Three: Divide and Conquer 277
The final chosen card is placed onto that last pile, and you then
drop the nine-card pile on top.
Next, assemble the deck, starting with that last pile you put the nine
cards onto. Put this pile on top of the pile next to it and then put this
bigger pile on top of the remaining pile. Square the cards.
Now, remind the audience what has happened: you made three
random piles, and each of the chosen cards was put into a pile and
buried under a random stack of cards from another pile, so no one has
any idea where any of the chosen cards are . . . But you are now going
to try to find them and free them from the pack.
You deal the cards into two piles. Start by dealing two piles of two
cards each face down, by alternating left, right, left and right. Then
stop and say that actually you should make the trick more dangerous by
dealing with one of the piles face up. Collect the four face-down cards
from the table and put them at the bottom of the pack. What you have
just done in this tester deal is shifted four cards from the top to the
bottom of the pack, but also built up the tension.
Start again by dealing a card face up. Then, deal the next face down,
telling your audience that the trick ends if any of the three selected cards
goes in the face-up pile. Deal through the whole deck like this. No chosen
cards will be seen.
Put the face-up pile aside, take the face-down pile, and deal it again:
first card face up, next face down and so on. Again no chosen card will
be seen. Repeat this procedure, dealing the first card face up each time,
until the face-down pile contains only three cards. Ask for the names of
the three chosen cards. On turning the last three cards over, the three
selections are found!
How It Works
You started with four piles of 10, 15, 15 and 9. The first card went into
position 11. Then some of the next 15 cards were added on top. This
appeared to add some randomness, but actually did nothing important
as the remaining cards were later just put back on top of them. That
meant there were actually exactly 15 cards separating the first secret
card from the next secret card, as it was added to the top of the second
pile. This was done again with the next pile meaning another 15 cards
separated the second and third secret cards and finally, another nine
were placed on top.
At this point, the secret cards, apparently buried randomly, are at
known positions from the top: positions 10, 26 and 42. However, in the
false start, we actually move the top four cards from top to bottom.
This shifts everything up four positions. So when you start the actual
deal, the secret cards are at positions 6, 22 and 38 from the top.
That whole palaver was actually a way to take a shuffled pack of
cards, but leave the three secret cards at those fixed positions while
making the audience think they had been buried randomly.
Why those positions? Well, each deal actually filters out every second
card. However, it also reverses the order. It turns out that if you do that
repeatedly you will end up with exactly the cards that started in positions
6, 22 and 38. We can see how by working through what happens . . .
We start with cards as the following where we just note their initial
positions:
1, 2, 3, 4, . . . , 50, 51, 52.
In the first deal, we start by placing a face-up card, and as we ultimately
throw away the face-up cards, we throw away every second card starting
with card 1. That means we lose all the odd-positioned cards. We also
reverse the order as we do so leaving a new pile:
52, 50, 48, 46, . . . , 8, 6, 4, 2.
280 Conjuring with Computation
We do this again, so again take out the current first, third, fifth, etc.
cards. This leaves, once reversed, the cards originally in positions:
2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50.
Another round of the deal takes out every second card and reverses
again, leaving:
46, 38, 30, 22, 14, 6.
Finally, taking out every second card one last time leaves us with the
cards that were originally in positions:
6, 22 and 38.
Those are exactly the positions we made sure the secret cards were at
before the deal started, ensuring the trick was guaranteed to work.
Thinking Computationally
The core of this trick involves the final series of deals, where we
repeatedly strip out every second card. This process guarantees we end
up with the given three cards. If we went a step further, we would
guarantee to have a single card. This magic algorithm is linked to an
important decomposition principle called divide and conquer. It is used
to create fast algorithms.
You can think about the trick as being an algorithm to find those
selected cards. Put that another way, it is about how to discard all the
cards aside from the selected ones.
We do that by discarding half the cards on each pass. It only takes
four passes to get down to the three secret cards. There is another
interesting feature of the way this is done. At the start, you are holding
a pack of cards and want to be left with the secret cards. Having done
one deal and thrown away half the cards, you are in the same situation:
you are holding a pack of cards and want to be left with the secret cards,
so you do exactly the same thing again. That leaves you in the same
position . . . so you do the same thing again. Only the number of cards
in the pack changes each time: the pack so the problem gets smaller.
This is a special way of decomposing a problem. It has been
decomposed into a smaller, simpler version of the same problem, not just
Free My Three: Divide and Conquer 281
(i.e., algorithms) to do it. One way, e.g., is to first scan down the pack
looking for the first card (the Ace of Hearts), then scan down it looking
for the next (the two of Hearts), and so on. This is very slow. You scan
down the remaining pack, on average looking halfway for each card, 51
times. You will likely check individual cards over 1,000 times.
Another way is to start by running through the pack, putting the
cards into piles (like in the trick) but one pile for each suit of Hearts,
Spades, Clubs and Diamonds. That takes a single scan. We are now left
with four versions of the same problem each a quarter of the size. We
have the start of a divide and conquer approach! To make it work though,
we must be able to do the same again. We must run through each pile
in turn and split them into four piles too, each leaving a problem of
sorting them. That’s easy: a pile for Ace to 3, a pile for 4 to 7, a pile
for 8 to 10, and a pile for the court cards. Each has three or four cards.
Now taking one of those piles, we do the same again, with three or four
piles, smallest to largest again, but this time we end up with piles of a
single card. That is trivial to sort! A pile of one card is already sorted.
Now we put those four cards on top of each other in the order of the
piles and now have those four cards sorted. We move back to the next
pile of four and sort it in the same way. It can be placed on top of the
first, and so on. Eventually, we have sorted all the Hearts. So we move
on to the Spades and do exactly the same, and so on for each suit.
We sorted each pile using the same general divide and conquer
method. Split it into four piles and sort them the same way. Once sorted,
pile them back up to give a bigger sorted pile. Get a pack of cards and
try it.
It probably still sounded slow, but it is actually much faster because
rather than working through all the cards lots of times, we have broken
them into smaller and smaller piles. The key is that we get down to a
single card in only three steps. This essentially means that instead of
scanning the whole pack 51 times, we only do so three times: once to
split them into suits, once to split them into groups of four, and once to
split them into single cards. It is therefore massively faster and involves
scanning cards only a few hundred times instead of over a thousand.
Free My Three: Divide and Conquer 283
Radix sorting
Imagine, now, you have not 52 cards but 5,000 cards (or perhaps, to
face a problem I have repeatedly faced in the past, they are exam scripts
that need sorting). Each has a four-digit number on it, like 1,234, and
you need them to be sorted in order of that number. We can use exactly
the same idea but use the different digits to organise each scan. On the
first scan, put cards into 10 piles based on the highest (thousands) digit.
Next, we take one of those piles and split them based on the hundreds
digit. Then do the same on each pile for the tens digit and finally for the
units. At that point, we have piles of one script, with the piles in order
so we just pile them on top of each other. We then move on to the next
pile and so on. This is called radix sorting. It is just one of many divide
and conquer solutions to the problem of sorting data into order.
Conjuring
An audience member constructs a trip-of-a-lifetime flight itinerary
that crisscrosses the world. They plan it by making a line of tickets.
Despite their freedom to choose any itinerary, once completed,
you reveal you bought a ticket in advance. Remarkably, it takes
them back to their chosen start destination from the place they
ended up.
Computation
Graphs are an important data representation used to store and
manipulate data when the links between different pieces of data
matter. They are also the basis of logical reasoning about whether
it is possible to write an algorithm to do certain tasks.
The Trick
Before doing the trick, you need to create a series of tickets. There are
two kinds of tickets: flights and hotels. Flight tickets show the start and
285
286 Conjuring with Computation
end destinations of flights; hotels just show a city name (see Figure 41.1).
The full set of flights and destinations is:
Hotel tickets:
London, Singapore, New York, Delhi, Sydney, St Petersburg, Beijing
Flight tickets:
London–Singapore New York–London
New York–Singapore Delhi–London
Delhi–Singapore Delhi–Singapore
Delhi–New York Sydney–London
Sydney–London Sydney–Singapore
Sydney–New York Sydney–New York
Sydney–Delhi St Petersburg–London
St Petersburg–London St Petersburg–Singapore
St Petersburg–New York St Petersburg–Delhi
St Petersburg–Sydney St Petersburg–London
Beijing–Singapore Beijing–New York
Beijing–Delhi Beijing–Sydney
Beijing–St Petersburg
As long as you have seven cities with a flight ticket for every pair and a
hotel for every city, you can actually use any city. Place one of the flight
tickets in an envelope, and you are ready to start the trick.
Give the envelope with the selected ticket to a member of the
audience to look after saying that it holds a special prize ticket. Ask
another volunteer to use the remaining tickets (flights and hotels) to
plan an itinerary crisscrossing the world. They should place them in a line
to show their planned route. Explain that it doesn’t matter where they
start and finish. Flights can be used to fly in either direction. Each ticket
The Double-Destination Deception 287
must be used exactly once. The volunteer should match up start and
end cities of adjacent tickets as they must also always arrive and depart
from the same destination: no overland treks from one city to another!
Hotel tickets can be used at any time when they are in that city, though
they can only stay in a hotel in each city once. At other times, they are
just in transit elsewhere.
Once they have created their itinerary, point out they had a
completely free choice about the order of the tickets including where
they started and ended. Have them write down their start and end
destinations so everyone can see. Point out that it is a bit of a shame to
do a round-the-world trip but not end up where you started. Luckily, you
thought of this for them. Have the person holding the envelope reveal
your pre-bought ticket. Amazingly, it is a ticket between the volunteer’s
start and end locations. It will complete their personal round-the-world
trip.
How It Works
An alternative way to do this trick is using a set of dominos. The trick is
based on a little-known mathematical fact about a full set of dominos.
You can place all the domino pieces in a circle, matching the numbers
on the tiles, and this line will in fact join up making a complete cycle
with all adjacent dominos matching. The actual order of the pieces may
vary, but if you have a full set, you can always find a chain that will
join with itself. The tickets in the trick correspond to dominos. A ticket
linking each pair of destinations (the flights) is like a normal domino with
different numbers. A hotel ticket corresponds to a “double” domino with
the same number twice.
Knowing this, it’s easy to see how the trick works. You have removed
one flight ticket so have removed one link in the cycle. The volunteer
won’t notice one piece is missing, but when they are challenged to put
all the tickets in a single chain, the end of their chain will be missing that
link that joins it back to the start. When the ticket is revealed, you are
just completing the cycle. The destinations will match your prediction.
Now you know the pattern that makes it work, you can of course
generalise it in your own way and so transfer this basic idea for a
prediction trick by using your own cycle of data. For example, rather
than destinations or the two numbers on a domino, one of the attributes
of your piece could be a colour and the other a letter say. Or a piece
could have just text on it like a singer’s name with one of their songs for
a music top 40-type presentation. Just make sure that at the beginning,
you write down the full cycle on paper before you transfer the numbers,
letters, songs or whatever, to individual pieces.
Thinking Computationally
We’ve said that this is a property of dominos, but why does that property
hold? To a computer scientist, this is a graph problem. Graphs are a
data representation, used as an abstraction of real-life things. They make
that original thing (whether dominos or flights) easier to compute with,
model or reason about. Road networks, communication networks and
social networks (and dominos) are all things that can be represented by
a graph. We have actually used graph data representations to explain
The Double-Destination Deception 289
tricks in this book already (see, for example, Figure 39.4 in Chapter 39,
the Doomsday Clock). A graph consists of points or nodes (think of
these as places) and lines or edges (links between those places). So our
cities can be represented by the nodes of a graph, and the tickets or
flights linking them can be represented by edges. Nodes don’t have to
be actual places though. Each number in a domino set (from 0 to 6)
can be represented by a node. Each domino in a set is then represented
by an edge linking two different numbers or a number to itself.
Now let’s start thinking about why our trick works by looking at a
simpler version with only three cities: London, Singapore and New York.
We then have six tickets as follows:
Hotels:
London, Singapore, New York
Flights:
London–Singapore
London–New York
Singapore–New York
Figure 41.2. The graph representing the tickets when there are three cities.
290 Conjuring with Computation
Figure 41.3. The graph representing the tickets when there are four cities.
Each node has two edges leading into it and two leading out. There is
an obvious cycle. Removing any flight would break the cycle, and the
ends of the chain would correspond to that flight. The trick would work
(though not be so magical).
However, if we now add an extra city with a hotel and a flight from
it to each other city, things go wrong (see Figure 41.3). The tickets are
now as follows:
Hotels:
London, Singapore, New York, Delhi
Flights:
London–Singapore London–New York Singapore–New York
Delhi–London Delhi–Singapore Delhi–New York
Figure 41.6. A node can only be in the middle of a path (or part of a cycle) if there
are an even number of edges to it so that for every way in there is a way out. Here
the extra edge makes an odd number, so if followed into the node, there is no way
left to leave that node.
Dominos
We noted that the trick also works with dominos instead of tickets.
Follow similar reasoning and draw a graph for the possibilities with
dominos. Exactly the same graphs result, so all our above logical
reasoning works exactly the same for a domino version too, as long
as you use a full set of dominos with tiles with 0 to 6 spots. Try it for
yourself.
294 Conjuring with Computation
Interlude: Dynamo
Magician, Dynamo (real name Steven Frayne), baffled the audience
on his TV show with a variation of this trick. He grew up in a
deprived area of Bradford. His grandfather taught him his first trick,
a way to make himself look bigger to deter other children from
bullying him. He is one of the first-generation Internet magicians:
posting online videos of his street magic helped launch his career.
His stage name, after Faraday’s invention of a device that produces
electricity, came from an audience member who shouted out that
Frayne was like a dynamo during a performance he gave when he
was still a teenager. It was part of celebrations for the legendary
magician, Houdini’s centenary.
Part 11
Conjuring
A volunteer chooses a card and holds it to their chest while
memorising it. They then place it back in the middle of the pack
of cards while you look away. You work through the pack, smelling
the backs until you find the one that they picked, detected due to
the trace smell left behind.
Computation
Steganography is a way of hiding secret messages in apparently
innocuous things. What looks like a normal message or image
disguises the real thing being communicated.
The Trick
You shuffle a pack of cards thoroughly using a normal overhand shuffle
(Chapter 15) in front of the audience. You then fan the pack and ask
a volunteer to pick a card. They pull it out, look at it to be sure what
it is, and then hold it against their heart for a few moments while
they memorise it. You then hold out the pack for them to reinsert it
297
298 Conjuring with Computation
in a random place. You shuffle the pack again, and then holding the
pack with the faces to the audience, work through them a few at a time
sniffing the backs, searching for the smell of their card. Triumphantly,
you pull out a card, and it is theirs.
How It Works
You need a special pack of cards for this trick that has a picture or
pattern on the backs that is not symmetrical. Normal packs of cards
have symmetrical patterns, but it is fairly easy to buy a souvenir pack
of cards that either has a picture on or some other pattern that is
not symmetrical. Magic shops also have cards with patterns that look
symmetrical at a casual glance but actually are slightly different at the
corners.
Before the trick, you ensure all the cards are the same way around.
Then, when the person takes the card and is holding it to their heart, you
surreptitiously turn the pack around in your hands. When they reinsert
the card, it becomes the only card the other way around. As you go
through the cards supposedly smelling them, you are actually looking
for the one card that has been turned around (Figure 42.1). When
revealing the card, put the rest of the pack face up on the table in
a neat pile, the right way around and keep the back of their chosen card
facing you so no one can spot what is going on.
Figure 42.1. (a) Cards with a non-symmetrical pattern look different when turned
around. (b) This allows a single card that is turned the other way to the rest to be
easily found.
300 Conjuring with Computation
6. They insert the card back somewhere in the middle of the pack.
7. DO an Overhand Shuffle of the pack of cards.
8. Hold the pack with the faces of the cards to the audience.
9. DO THE FOLLOWING UNTIL you come to a card that is turned
the wrong way around:
a. Look at the back of the card to see if it is turned the right
way around.
b. Sniff the back of the top card.
c. IF the top card is not the wrong way around.
THEN move it to the bottom.
10. Place the pack face up and neatly squared on the table apart
from the top card.
11. Reveal the top card as their chosen card.
Thinking Computationally
Steganography is a way of ensuring a message, and more generally
information, is secret by hiding it in some other innocuous thing. The
aim is that no one realises there is a secret message there at all. One
of the earliest versions from Ancient Greece involved a message being
tattooed on the shaved head of a messenger. They had to wait for their
hair to grow back before taking the message. Mary Queen of Scots also
used steganography. She famously plotted while under house arrest by
hiding messages in barrels of ale.
Information can be hidden in apparently innocuous messages in all
sorts of ways. Different patterns in an image could stand for different
letters, or a subtle difference in the way letters are written could stand
for 1s and 0s with a message then stored as binary (see Chapter 28).
Different groups of 1s and 0s then represent different letters in the secret
message. A Tudor master of spycraft, Sir Francis Bacon, came up with
the original version of this kind of steganography. His version used capital
and lower case letters for the two binary symbols in writing that was
apparently about something completely different.
Sniff Out that Card: Steganography 301
Classic Mentalism
Codes and Ciphers
Conjuring
One of two magicians leaves the room while a card is chosen. When
they return, they instantly announce the card that was chosen.
Computation
Cryptography, the use of codes and ciphers, to make messages
unreadable, is a core part of how computers are made secure.
Used by rulers, the military and spies for thousands of years to
keep their secrets safe, it is now the way banks can send digital
money around the world, how we keep documents, chat and video
meetings secure as well as the basis for preventing anyone logging
into our computers.
The Trick
In this trick, there are two magicians. One stays with the audience, while
the other, “the receiving magician”, leaves the room. A brand new pack
of cards is opened. A member of the audience chooses a card from the
pack. The chosen card is placed back in the pack. The pack is put back
303
304 Conjuring with Computation
in the box and placed back on the table. The receiver is called back.
When they return, they immediately tell the audience the chosen card.
How It Works
The secret to this trick is in the table. Once the card has been selected,
pack and box are placed on the table in a position that depends on the
card selected. The two magicians agree on a secret code of where the
box will be placed in advance such as (see Figure 43.1):
If the box is face up and its flap is closed: Clubs.
If the box is face up and its flap is open: Spades.
If the box is face down and its flap is closed: Diamonds.
If the box is face down and its flap is open: Hearts.
That’s the suits taken care of. Now for the value. The performers again
agree in advance, this time on how to chop up the card table into mental
zones. Horizontally, you have the top row, centre row and bottom row
of the table and far right, mid right, mid left and far left. That’s 3 × 4
unique locations. You have 12 places for 12 of the 13 card values. Ace
is top-far-left. Two is top-middle-left, and so on (see Figure 43.2). The
magician places the pack of cards in the correct pre-agreed position, box
face up or not, flap open or closed as needed (see Figure 43.3).
Figure 43.1. The suit is determined by whether the box is left face up or face down
and with the flap open or closed.
Classic Mentalism: Codes and Ciphers 305
Figure 43.2. Divide the table into mental areas like this to indicate what the card
value is. If the card is a King, put the box in the volunteer’s hand.
Figure 43.3. The cards are in the top row, far right (4), and the box is face up, with
flap open (SPADES), so card indicated is the 4 of spades.
306 Conjuring with Computation
What about the 13th possibility? That is when the card is the King.
If the card isn’t the King (most often), you leave the pack of cards on
the table. If it is the King, have your volunteer place their hand out flat
and leave the box of cards on it. Ask them to concentrate on the card
they chose while staring at the pack.
Thinking Computationally
This trick is using steganography again. However, it is also using a secret
code. In a simple version of steganography, a message is just hidden (like
growing hair over it or hiding a letter in a waterproof package inside a
barrel of ale). In more complex versions as in both the last trick and this
one, information is transformed into a different form that also hides it
using a secret code.
A code is just an algorithm that transforms information from one
form into another. Encoding transforms the information and decoding
transforms it back to the original form (see Chapter 25). The information
encoded can be in any form, whether spoken, written, gestures or implicit
in the form of objects. The decoded version of the information can also
have any form. For example, a semaphore encodes written or spoken
letters into the positions of a person’s arms holding flags. A secret code
is one where the code is being used in a secret way. Only some people
know the secret, know the code.
In the last trick, we had a very simple encoding of one piece of
information “It is this card” into the orientation of the card. In this trick,
308 Conjuring with Computation
we use a code again, but a more complicated one that encodes more
information: that of the details of one out of 52 cards. Separate codes
are used for the suit and for the value of the card. Codes like this are
the basis of a wide variety of mentalism acts. They only appear magical
because they are both secret and hidden. For example, in an act where
the magician takes an object from an audience member and their partner
blindfolded can say what it is, different words the magician says in asking
them what they are holding might mean, “It’s a watch”, “It’s a mirror”,
and so on . . .
Computer scientists also use a wide variety of codes to make
information easier to store and transmit. Most codes are not secret so
are not a secure way to keep information secret. However, secret codes
have also been used throughout history to keep information secure. This
is known as cryptography. Even if the message is intercepted, if the
enemy can not decode it, they cannot read it. They may though be
able to infer something useful just from the fact that it is written in
code, who it is from and who it is to. Magicians use codes in this way,
usually combined with steganography, explicitly to also hide the fact
that information is being passed. Many of the tricks in this book have
used information secretly both hidden and encoded one way or another.
The word code, in general use, means any kind of secret message.
In cryptography, it means a specific kind of encryption algorithm where
a word or phrase in the message is allocated a code word, symbol or
phrase. Mentioning, as part of a conversation, the code word “Phoenix”
might mean “Attack at dawn”, whereas “Vulture” means “The attack is
aborted”. That is what our trick did. The word “Clubs” is given a code
word “face-up-flap-open” which is then translated into the corresponding
physical positioning: the box is placed face up with its flap open. What
we do not do is give the separate letters C-L-U-B-S their own code word
and try and spell them out in code. Native American languages were
used as codes in World War II as very few people spoke them. Navajo
words were used as code words for their English equivalent.
A cipher, by contrast, works on individual letters (or syllables).
A simple cipher encrypts each letter to a new symbol or symbols to
encrypt a message. Modern encryption generally uses ciphers rather than
codes as it is more flexible. The Caesar cipher is a simple example
Classic Mentalism: Codes and Ciphers 309
Conjuring
A friend picks a card from a normal pack of cards. You phone the
mysterious “Clairvoyant”. The “Clairvoyant” is able to name the
chosen card.
Computation
Computer systems are kept secure by having separate areas with
different security levels. Nothing from a high-security area should
be able to leak to a lower-security area. Hiding information
(steganography) in a legitimate channel (an allowed way to com-
municate) is one-way information that might be leaked. Hackers
often exploit legitimate channels in computer systems. Magicians
build tricks around them too.
The Trick
This is a trick that could be done with an audience or just with a friend.
You announce that you know an amazing person who goes by the name
311
312 Conjuring with Computation
“The Clairvoyant” who can do amazing things and if you like you will
ask them to do a demonstration of their powers. Close any doors and
curtains of the room you are in so no one outside can see in. Next,
ask the friend (or a volunteer) to choose any card from a face-up pack,
allowing everyone, you included, to see the card they choose. Next phone
the Clairvoyant. When they answer you simply say, “I have a call for the
Clairvoyant”. After listening for a moment, you say “Thanks”. After a
further pause during which you say nothing, you finally say, “Let me pass
you over.” You hand the phone over to the friend/volunteer (and/or put
the phone on speaker phone so the audience can hear) telling them
to just say “Hello” to the Clairvoyant and then listen. They speak to
the Clairvoyant. Despite the Clairvoyant not being in the room, having
apparently no way to know what happened, and you saying nothing to
indicate what the card was, they dramatically tell your friend the card
they chose . . . and they are right.
How It Works
When you first phone, your accomplice, the Clairvoyant, just names the
card suits, saying: “Clubs . . . Spades . . . Diamonds . . . Hearts”. You say
“Thanks” when they reach the suit of the chosen card (which you, but
not they, have seen). Your phoney Clairvoyant accomplice now knows
the suit and starts counting out the values, Ace to King. Again you stay
silent until they get to the chosen card, at which point you this time
say: “Let me pass you over.” Figure 44.1 shows an example of how the
5 of spades is communicated. Your accomplice now knows both suit
and value. Once they are passed to the person who chose the card, they
know the suit and value and so can surprise them by immediately telling
them what it was.
Thinking Computationally
This trick requires a shared understanding that the two algorithms will
be followed. Such a pair of algorithms used to pass information is called
a communication protocol. Even though you apparently say nothing to
give away what the card was (and in fact say the same thing every time
you do the trick), you do pass the information on to your accomplice.
You do it not by what you say but in the timing of when you say it.
Your silence passes information!
Computers that are intended to be secure use secure operating
systems: software that controls the computer but does so in a way
that is supposed to not allow information to be passed around. For
example, it may be divided into high-security “Top Secret” and low-
security “Unclassified” areas. Top-secret information is stored in the
high-security areas which only certain people can access. Meanwhile,
everyone else has access to the “Unclassified” areas where only non-
confidential material resides.
In our trick, think of the room where you, the magician and your
friend are, as being the top-secret area. That is where the chosen card
resides. You are both there so know the secret information, that is the
chosen card. The Clairvoyant, on the other hand, is elsewhere. They are
in an unclassified area.
An important part of the setup for the trick is that they should
clearly not be able to just see the information (the card) that resides in
the top-secret area. This is clear because they are not in the room, just
on the end of the phone, and you have closed all doors and curtains.
Your accomplice cannot see in! Let us call that BELIEF 1.
Legitimate channels
The only way information can pass out of the top-secret area to the
unclassified area is therefore via the phone. This is what a computer
Call the Clairvoyant: Legitimate Channels 315
in 2013. These revealed to the public the massive extent to which the
US was spying on them, in breach of the US constitution.
Snowden took the information with him which is a bit like our
magician leaving the room having seen the card (not very magical).
In the trick, we are using steganography to hide the information being
leaked in plain sight through the legitimate channel. The message
about which card is chosen is passed in an apparently run-of-the-mill
conversation. It is not what is said that communicates the information
but the timing of when it is said that matters.
In fact, in a more sophisticated variation of the trick, the magician
and the Clairvoyant could pass the information without the Clairvoyant
saying anything or saying something innocuous, and instead just timing
the gap between the magician speaking. This would work as long as the
pair had agreed in advance the timings of each suit and face values. So
a pause of 5 seconds from saying “I have a call for the Clairvoyant” to
saying “Thanks” means Clubs. A pause of 10 seconds means Spades,
and so on. Then the whole conversation could be on speakerphone the
whole time, and the information still passed.
Timing can similarly be used by a program to communicate covertly
out of a secure network. Information might be communicated by the time
a message is sent rather than its contents, for example. Alternatively,
information might be passed based on whether a particular encrypted file
exists or not. They cannot be read, but they can still leak information as
a program on the inside could create and delete files to communicate.
Making a system really secure is actually quite tricky!
Call the Clairvoyant: Legitimate Channels 317
Conjuring
The Clairvoyant is called once more, this time able to tell exactly
which object from a table full of objects a member of the audience
chose even though called on a speakerphone, so all could hear
everything that was said.
Computation
Another way hackers do damage is to set up covert channels: ways
of communicating that are not supposed to be there. Magicians
build tricks around covert channels too.
319
320 Conjuring with Computation
How It Works
You simply set up a small but hidden webcam somewhere in the room
where it has a clear view of the objects on the table, or of where the
volunteer will stand holding the chosen object (perhaps poking through
curtains at the side of the stage). The webcam livestreams the images
so that the person at the other end can see everything. This could just
be done by setting up a video call to the computer the webcam is linked
to. The Clairvoyant watches the show via the video call (with the sound
off!) and so sees the object taken.
When internet-based video calls were new, famous magicians used
such technological variations of this. Given such video surveillance
is now totally pervasive, making this magical today will depend on
your presentation skills distracting the audience from thinking it is a
possibility!
Thinking Computationally
Covert channels
The trick is similar to the last though done in a different hi-tech way,
using the hidden webcam in the room that was positioned to be able to
see the object so the Clairvoyant can watch proceedings via the camera
streaming images from the room. For the trick to work, the presence
of the hidden camera needs to be unknown to the audience (unlike the
phone connection which everyone knows exists). The phone connection
to the Clairvoyant is a legitimate channel. In this trick, by contrast, we
322 Conjuring with Computation
Surveillance society
The trick depends on camera surveillance. This has now become
ubiquitous in many western countries. As you walk through the centre
of a city like London, cameras are everywhere mounted on buildings.
You are potentially constantly being watched. As you drive around the
motorway system, your number plate is constantly being logged. As
you move anywhere, your phone logs your position as you connect to
different phone masts. This is an example of technology being used for
good purposes that could easily be subverted, whether by individuals
such as stalkers, companies or states. More recently, companies selling
hacking tools to countries have provided them with ways of taking over
a person’s phone including the microphone and camera, so they can
see and hear everything you can. Supposedly only used for legitimate
policing, it has been used repeatedly to monitor political opponents
of oppressive states, legitimate journalists, lawyers and more, in clearly
unethical ways.
Privacy can no longer be taken for granted.
Call the Clairvoyant a Second Time 323
Conjuring
You make a mug of hot chocolate. The person whose personality
you will read stirs it five times and then drinks. You stare into the
sludge of chocolate left behind and from it read the personality of
the volunteer.
Computation
Tech companies collect data about everyone who uses their
services so they can use the information to sell advertising and
target services at you. They build up a very detailed picture of
each person, their personality and their lives. Their aim is to know
you better than you know yourself.
The Trick
You explain that some fortune tellers can read people by looking in a
crystal ball, others in tea leaves. You, however, having spent so much
325
326 Conjuring with Computation
At times, you are overly critical of yourself, but you also have a
deep desire for others to appreciate you. Sometimes, you are very
confident but there are times when you are less sure of yourself.
You often worry about whether you have made the right decision
and some of the things you try to do do not work out. You like
variety and dislike it when prevented from doing things because
of rules imposed by others. Sometimes you are sociable, but you
can also be much more reserved. You have occasionally found
it to be a bad idea to reveal too much of yourself to others too
freely. In general, you are an independent thinker who doesn’t
just believe things you are told without some kind of evidence.
Most of the time, you can overcome the flaws in your personality.
Ask them to rate how accurate your description is, and whether they
recognise themselves.
How It Works
You just say the same thing whoever the person and whatever the state
of the chocolate mug, occasionally poking at the chocolate as though it
is revealing new facts.
The effect used in the trick is called the Barnum–Forer effect after
showman Phineas T. Barnum and psychologist Bertram R. Forer. It is
relied on by the likes of astrologers and fortune tellers.
The personality prediction is full of the same kind of statements
you find in horoscopes, called Barnum statements. Those above are
Reading a Personality with Hot Chocolate: Big Data and Privacy 327
TO DO Reading a Personality:
1. Tell the person you can read their personality in the chocolate
dregs of a mug of hot chocolate.
2. Make them a mug of hot chocolate.
3. They stir the drink five times.
4. They drink the hot chocolate.
5. You stare into the chocolate sludge left behind.
328 Conjuring with Computation
6. You “describe their personality” while staring into the mug, poking
at the sludge with a teaspoon occasionally and saying a series of
Barnum statements.
7. Ask them to rate your accuracy on a scale of 1–5 with 5 meaning
fully accurate.
Thinking Computationally
Fortune tellers have long used this kind of fraudulent reading to take
people’s money. Oppressive regimes have also long collected information
dossiers on their citizens so they know who is likely to oppose them.
Hackers and fraudsters also use social engineering to get people
unwittingly to give up personal information, pretending to be people
they aren’t for example and asking for passwords and credit card details.
Magicians have also used a version where they just ask for information
for mentalism and spiritualism acts. One version involves collecting
information about their audience members before the show to use to
make predictions seem more realistic.
Now technology allows it to be done for real on a mass scale. We
blithely give away lots of information about ourselves to technology
companies in exchange for their services in a way that allows them to
both know and predict a lot about us. The way we follow other people on
social media combined with our “likes” can be used to fairly accurately
tell things like our ethnicity and gender and who we are likely to vote for,
for example. Companies also collect your browsing history, what kind of
music you listen to and films you watch as well as what you buy or think
of buying. Technology companies sell the data to marketing companies
who then compile very detailed sets of statements about every one of
us based on the mass of information collected and combined. These are
no longer Barnum statements but accurate guesses about very intimate
aspects of your personality: interests, habits and life.
All this, once private, information can be used for good or bad.
It can be used to make life more convenient but also to manipulate
us. Advertisers use the information to sell you things you probably don’t
need or really want, political parties use it to target messages suggesting
they support the things that matter to you to change the way you vote,
Reading a Personality with Hot Chocolate: Big Data and Privacy 329
gives away similar information about lots of people who refuse to fill in
the surveys because they prefer to be private. That freedom of privacy
is effectively removed from them by the choices of others.
This kind of manipulation of messaging can be helpful, for example,
if it helps people understand the true beliefs of the politicians being
promoted. On the other hand, if it is used to propagate lies and half-
truths, it undermines democracy.
Computer science is not just about technology but also about its
place in society. New technology changes the way we do things, but
also can completely change the way the world works, for good or
bad. Understanding the effects of changes on society is important, and
underlying this is also an understanding of, and commitment to, using
technology for good, not just personal gain.
Conjuring
A volunteer holds a pendulum over a circle with YES and NO
written on it. They use it to “talk to the spirit world” and ask
questions about their future.
Computation
Professional ethics is very important for all computer scientists.
A person with computational thinking skills and computing knowl-
edge can use it for good or bad: as a black hat hacker breaking
into systems stealing data or as a white hat hacker helping protect
people’s privacy, for example. Big data can be used to spy on
people or to improve their lives. Behaving ethically matters.
The Trick
As part of a magic show, ask for a volunteer who is open to the idea that
the spirit world might exist. Tell them they will take part in a seance
where they will talk to a supernatural Victorian spirit you have made
331
332 Conjuring with Computation
contact with, called Alice. You know Alice likes to communicate in this
way and likes to talk about relationships whether past, present or future.
Give the volunteer a simple pendulum made from a large, heavy, old-
style key hanging on the end of a string 40 centimetres long. Explain that
the key is so old and has been around humans and their lives so long,
and it has become a link to the spirit world. Have the volunteer hold it
over a large circle split into quarters with YES written in two opposite
quarters and NO written in the other two quarters (see Figure 47.1).
You may wish also to decorate it with runes or other ghostly images.
The volunteer should be standing and keep their arm held out straight
and unsupported. They should hold it so it hangs over the middle of the
circle. Tell them they must concentrate on the pendulum at all times,
but not try to consciously move it themselves. They should allow Alice
to work through them.
You then ask a series of questions, starting with:
Follow this by the first question. If after a short wait the pendulum starts
to swing to yes or no, then announce the answer given. Then ask the
person holding the pendulum (or their friends) to come up with further
questions for Alice to answer that they would like to know the answer
to, whether about past, present or future relationships. If they are open
to the influence of spirits, you say, then the pendulum will swing to give
answers to the questions.
How It Works
This illusion is based on an effect called an ideomotor response
and named after Michel Eugene Chevreul, a French scientist who did
experiments about the effect in the 1830s. The same effect leads to
the movement of tables and Ouija boards in seances. The movements
happen because ideas can lead to subconscious physical changes. An
example is the way your mouth waters when you think of something
you like such as chocolate (for me at least). In the trick, thinking
about questions leads to very small movements of the hand of the
person holding the pendulum. The pendulum magnifies the movements
particularly if the arm is moving backwards and forwards in a way that
matches the natural frequency of the pendulum swinging (which depends
on its length). Staring at the pendulum increases the effect as the person
thinking strongly about the pendulum swinging one way or the other can
trigger the subconscious movements that cause it to happen.
Thinking Computationally
Whilst it is fine to do a trick like this as part of a magic show where
it is clearly intended to be just for fun, it would be unethical to use it
to really claim to talk to the dead relatives of people as you are not. It
would be even more unethical to charge people to do so.
Spiritualism became really popular in Victorian times where seances
were held to allow people to supposedly speak to the dead. Whilst a
few may have truly believed they were helping people talk to their dead
relatives, most were just frauds, unethically preying on people who had
lost loved ones. Their aim was to swindle money from them using their
false claims. Other people use the same sort of tricks as magicians for
a variety of unethical reasons: to cheat when gambling, to claim to do
faith healing and to con people out of money.
Behaving ethically is very important to most magicians. They make
a clear distinction that what they are doing is a performance for
entertainment. Some go out of their way to expose frauds who use the
kind of illusions and conjuring tricks we’ve seen in this book to fleece
people by claiming them to be something other than mere tricks that
are part of a conjuring show.
The Chevreul Pendulum: Professional Ethics 335
Advanced Technology
Conjuring
You show the audience a faerie you have trapped in a magical
cage.
Computation
Virtual reality involves creating online worlds that people can
move digital versions of themselves around in. Augmented reality
involves superimposing virtual elements on the real world so that
you can move around both real and virtual worlds together and
so that nonexistent things appear to be there in the real world.
Applications range from games to the head-up displays in fighter
aircraft.
The Trick
Explain that faeries do exist and you have trapped one inside a special
box lined with rowan wood, which acts as a barrier to a faerie. You
reveal your magic cage. Inside, flying around, for all to see when they
peer through the window in the box, is a very angry faerie!
339
340 Conjuring with Computation
How It Works
This illusion is based on a Victorian stage illusion called Pepper’s
Ghost. In the original trick, a large glass sheet lay at a 45-degree angle
between the audience and the stage. Some actors acted just as normal
on the stage. Others lay on trolleys on the floor, out of sight, but on
the audience side of the glass. When powerful stage lights were shone
on them, they appeared (and disappeared) as ghostly figures on the
stage. This works because the light reflected on them bounces onto the
glass and is reflected out to the audience, making those actors appear
as though they are on the stage.
What is happening is the glass acts as a half mirror. Light from
behind it can pass through, but also light from below is reflected at 45
degrees out to the viewer too. Because our brains assume light travels
in a straight line, the reflected image appears to be in front of the back
wall. You can see a weaker version of the same effect at night just by
looking out the window. You see a reflection of the room superimposed
on the garden next to lit objects actually in the garden.
Trapping a faerie
To create your own (small) version of the illusion, you need to make
your own Pepper’s Ghost box: the faerie cage. For the faerie itself, you
need a smartphone with an animation of a faerie. It doesn’t have to be
a faerie, of course, you can “trap” any creature that you can find or
create an animation of. You could even film your brother or sister lit by
a lamp shining on them in a dark room (if you prefer, your story to be
about shrinking and trapping your irritating sibling). If you don’t have
an animation or film, then you can just use a still image on your phone
(and explain the faerie is trapped frozen in suspended animation). The
phone ensures you have a brightly lit image, equivalent to the bright
stage lights of the original effect, which is important if it is to work.
Cut a large rectangle out of one side (the lid of the box), leaving a rim
all around the box. That is the window through which your audience
members can view your trapped faerie. Add bars if you want it to look
more like a cage.
Now cut a sheet of stiff Perspex (from a DIY or craft shop) to fit
diagonally inside the box at 45 degrees. The top of the Perspex should
be towards the audience, above the hole in the box, and the bottom of
the Perspex should be at the back of the box. It splits the box into two
diagonally. Your viewers can see the back wall of the box through the
window and through the transparent Perspex (Figure 48.1).
Now for the secret part. You place your smartphone (ideally, black
or covered so only the screen is visible) on the floor of the box, under the
Perspex. It should be playing the animation of the faerie on a loop. The
phone itself must be out of sight of the viewers who look at the back
wall of the box through the window. An overhanging lip at the bottom
of the window may help hide it from someone viewing from the front.
If you play an animation of an angry faerie on a black background,
then the light from the faerie will bounce off the Perspex and appear
to be flying, trapped, inside the cage. The image from the smartphone
Figure 48.1. A Pepper’s Ghost box. Because our brains assume that light travels in
a straight line, the image from the hidden camera that bounces off the half mirror
appears to come from the back of the box.
342 Conjuring with Computation
Thinking Computationally
Essentially the same illusion as Pepper’s Ghost is used for head-up
displays, where a virtual computer display is superimposed over the real
world. For example, my car’s Sat Nav projects arrow directions in this
way so they float out in front of the car as I drive. The arrows appear
at the right depth so I can focus on them while still paying attention
to the road. Fighter pilots have more complicated versions so they can
see information while still looking out the cockpit window. The virtual
band, Gorillaz, performed, apparently live on stage, interacting with real
singers like Madonna in the same way. You can also see the illusion in
museums when ghostly characters appear in dioramas and talk to you
about the past.
The same idea of combining the real world with the virtual world
forms the basis of augmented reality, though modern versions may
use different technology to achieve the same effect. Programmers not
only can now create whole virtual worlds, but they can also superimpose
a virtual world onto the real one using programs that merge live film
with the computer-generated imagery. Another way is to have smart
The Faerie Cage: Augmented Reality 343
glasses that allow you to see the real world but where the programmers
superimpose imagery, like Pepper’s Ghost, onto the lenses so appearing
out in the world.
Conjuring
A good reveal can make a trick and is also where you can have
some creative fun. Here, despite a volunteer having a free choice
of where to split the pack, the special Down-Under Deal leads to
a final selected card that no one could have predicted. It turns out
that the card was forced by the magical influence of your robot
assistant down under the table.
Computation
Robots have long been the subject of science fiction but are now
reality. Industrial robots work on production lines, building cars,
and anyone can buy a robot vacuum cleaner to clean their house
while they are out. Robotics researchers are building robots that
mimic all kinds of life and, using soft robotics; they are no longer
the hard metallic machines of science fiction.
345
346 Conjuring with Computation
The Trick
You explain that you are going to show a special magical deal called the
Down-Under Deal taught to you by an Australian magician (or European
one if you are from, for example, Australia or New Zealand). You shuffle
a pack of cards and then spread the cards in a line, face up on the table.
Point out that it is a normal pack of cards. Saying that the deal will
take too long with this many cards, ask a volunteer to point to a card
“somewhere in the middle” of the spread deck and then discard half the
pack beyond that position. Note that it was their free choice as to the
cards discarded as you do so.
Take the remaining cards and, forming them back into a pack,
explain you are now going to do the special Down-Under Deal that
will leave you with a single chosen card no one could have predicted.
With the pack face down in your hands, deal cards into two piles
saying “Down” as you put a card on the first, face-down, pile and
“Under” as you place the next on the second face-up pile. When all
cards are dealt, discard the face-down pile. Explain that the audience
should now be able to see why you wanted fewer cards as it is a very
slow deal. Deal out all the remaining cards from the “under” pile, into
two piles in the same way, chanting down-under-down-under . . . as you
place the cards, then again discard the down pile. Keep doing this until
only one face-up card is left in the un-discarded “under” pile.
Declare this to be the final chosen card that no one could have
predicted. Pick up the card and show everyone what it is. Point out
again that the volunteer had a free choice of where to split the pack
and turn over the pile of discarded cards, showing the top few cards to
the audience. Note that if the pack had been split one place further, it
would have been a different card chosen.
Finally, you explain that the deal is called the Down-Under Deal,
not just because of the way you do the deal, but because the final result
is influenced by whatever is down under the table, and throughout the
whole trick, your assistant has been down under the table, holding a
specific card to try and influence the final choice. You ask your assistant
to reveal that card . . . and at that moment, a robot pushes itself through
the table cloth draped over the table and lifts up a large card to
The Down-Under Deal and Robotic Reveal: Robots 347
show everyone. It is the same as the final card left on the table. The
card chosen was magically determined by the influence of the card your
robot helper was holding under the table during the trick.
How It Works
Before the trick, place the card you intend to predict (for example, the
8 of Hearts) in the 16th position from the top of the pack. Place an
identical card in the grip of the robot and place it under the table. Have
a hidden button that activates the robot somewhere easy to press such
as at the back of the table.
The shuffle at the start is just a False Top Overhand Shuffle
(Chapter 18). This leaves the top 20 or so cards in the same position,
so leaving the 16th card in that position. If you are unsure of doing this
successfully, then you can miss out the initial shuffle.
The mathematics behind this trick is a simpler version of that used
in the Free My Three trick of Chapter 40. By discarding every second
card repeatedly, you will end up with a card that started in a specific
position. Starting with 52 cards, so positions 1–52 as below:
1, 2, 3, 4, . . . , 50, 51, 52.
we first split the pack somewhere in the middle. It is important that
the split happens somewhere between the 16th and 32nd cards, leaving
the 16th but discarding the 32nd. If the volunteer points somewhere
in the middle, then you will be in this range. We therefore end up, after
the discarding part of the pack, with cards such as:
1, 2, 3, 4, . . . , 14, 16, 18, . . . , 30, 31.
The precise last card is unknown, but the above is the worst case that
could happen. Note that the critical 16th card is still in the 16th position
from the top of the pack.
Deal the odd-position cards into one pile (face down) and the
even-position cards into another (face up). Discard the face-down pile
containing those in the odd positions. This leaves cards from the original
even positions only:
2, 4, 6, 8, . . . , 14, 16, 18, . . . , 30.
348 Conjuring with Computation
Thinking Computationally
Robert-Houdin’s automata that mimicked life were sometimes pre-
sented as being magical in their own right: how could a machine possibly
do what they did? Other automata were mixed with illusion in more
complex tricks. Given humanity’s long obsession with creating machines
that mimic life, it is natural, once computers existed, we would want to
give these machine brains bodies too.
The science-fiction versions of robots are often either clearly
machines or humanoids, sometimes both. Modern robotics is far more
varied. Real robots come in all shapes and sizes. While many of the
robots now used day to day are very much functional machines with
functional forms: working in factories manufacturing cars, in warehouses
collecting the items you have bought online, or scuttling around the floor,
vacuuming the carpets, many others mimic life, as did the automata.
Researchers have created cockroach-like and flea-inspired robots, fish-
shaped robots, snake-like ones, and robots modelled on octopuses, to
give just a few examples. Animals need to fit many life niches and
consequently have evolved elegant solutions to wide-ranging problems
that work far better than human-engineered solutions. This has led to
the growth of biologically inspired computing, including in the area of
robotics. Just as automata mimicked many different forms of life, robots
do too. If you want a robot that can travel over difficult terrain, then
building a variation of a car or a human may not be the best solution.
Animals travel across terrain they are adapted to in a multitude of more
flexible ways than our vehicles; think of goats on cliffs, for example,
or gibbons swinging through the forest. If you wish to move through
water, then there are fishes that are far more manoeuvrable than boats
The Down-Under Deal and Robotic Reveal: Robots 351
and submarines. If you want a machine that can get in and out of
tight spaces, then just look at the octopus, which does so in ways that
would be a boon for many a magic trick if only a human assistant
could do it. Animals provide many wildly different solutions to problems
like locomotion, so learning from them can lead to novel engineered
solutions.
One area of biologically inspired robotics is soft robotics. This is
about making robots that are soft, not hard, fixed and rigid, and can
also change shape and size. As a result, they can be much more flexible
and safer around humans than stereotypical hard robots made of metal.
There are many sources of inspiration for soft robotics including the
octopus, spiders and plants too. Plants use changing pressures of liquids
in cells, induced, for example, by light levels, to change their form to fit
needs. It is a mechanism like this that allows them to follow the Sun.
Soft robots can use similar systems to move using soft materials.
It is not just the form of animals that researchers copy but also
their behaviour. For example, swarm computing aims to copy the way
birds fly in massive, swirling flocks each following simple rules that
maintain the flock. Flocks of drones might follow similar rules to help
maintain coverage of an area being monitored. A more futuristic idea is
for flocks of tiny, microscopic robots to be injected into the body and
work together in swarms to attack cancer cells.
Other researchers explore different uses of robots, creating actor
and stand-up comedian robots to explore the art of performance. Even
here, Robert-Houdin was there first, having created an automaton to do
the cups and balls magic trick. Robots with general-purpose computer
brains and fine dexterity as well as programmed showmanship offer the
possibility of flexible magician robots able to do a wide range of tricks.
A computer brain might even make tricks possible for a robot magician
that no human would be capable of. Showmanship and illusion would
be needed to make it magical though.
By copying and drawing inspiration from animals, computer sci-
entists have found innovative ways to solve problems. Whilst seeing
machines mimic life perhaps does not seem like magic itself anymore,
we still find such machines and their abilities intriguing. There is also
scope for mixing robots with illusion to make wonderful tricks if you have
352 Conjuring with Computation
Conjuring
We close with one last trick. Are you the one true Sovereign of
the British Isles: can you remove Excalibur from the anvil?
Computation
We overview the main themes of the book, but also explore one
last link to computing: how can you be sure someone is who they
claim to be?
The Trick
Tell the story of Excalibur, the magical sword set in an anvil, on top of
a stone, part of the legend of King Arthur. Merlin predicted that only
the true sovereign of Britain, descended from Uther Pendragon, would
be able to pull the sword from the anvil. Merlin’s magical test showed
that the teenage Arthur was the rightful King.
Explain that you have found a new Excalibur, a sword in an anvil.
Pull back the curtains to reveal a sword protruding from an anvil,
complete with plaque stating that only the heir of Uther Pendragon,
353
354 Conjuring with Computation
and so the one true Sovereign of the British Isles will be able to remove
the sword from the anvil.
Allow anyone who wishes to show their strength to try to pull the
sword from the anvil. When they fail, apologise that they are clearly not
royalty. Once everyone who wants to try has failed, say that perhaps
you should try too. Without any effort at all, you pull the sword free.
Proclaim yourself heir to the throne. If anyone is sceptical, replace the
sword and let them try again.
How It Works
We will leave you to work out your own way to make this trick work
for now, though building it will take ingenuity, work and lots of both
engineering and computing skills.
Thinking Computationally
Algorithms
This book has drawn out the many links between conjuring and com-
puting. Self-working tricks are essentially algorithms for doing magic.
The One True Sovereign: Curtain Call and Creativity 355
Data representation
Another important aspect of developing software is the appropriate
choice of data representation: how is information organised? It needs
to be done in a way that facilitates the task. This is also important
in many tricks, with different representations used as the basis for
organising information in tricks. Whether it is in the organisation of
356 Conjuring with Computation
Precise languages
Unlike magicians, computers follow instructions in a programming lan-
guage, a precise mathematical language. A large part of programming
is in writing the steps in the constructs that are available in the language
being used.
Magicians do not rely on special languages the same way, so
the important programming step of writing the instructions in such a
mathematically precise language is omitted. Humans work out tricks
based on natural language explanations and fill in the gaps themselves if
necessary. If a robot were to become a magician, then the tricks would
need to be set out precisely in an appropriately precise language. An
The One True Sovereign: Curtain Call and Creativity 359
Scale
There is also a massive difference in scale between tricks and programs.
Programs can be millions of lines of code, millions of steps long, whereas
even the most complex of magic tricks is likely to be only tens or
hundreds of steps; after all, you do not want to bore your audience,
while they wait for the pay-off of the trick! Computational thinking
techniques that are used in simple ways with tricks are absolutely vital
to manage the complexity of large programs.
Showmanship
Showmanship is all-important in magic, but less so in computing. It is
the ability to engage and entertain the audience, to draw them into the
world you are creating. It is more than just following the core algorithm
of a trick. It is about the presentation around that: the way a trick is
weaved into a story, and then the way that story is told, the timing, the
flair. Showmanship makes the performance of a trick extra special.
The equivalent to this in computing is user experience (UX)
design. Something akin to showmanship is just one aspect of this.
Similar techniques may draw people into software that aims to entertain
like games. Those using the software expect to have an enjoyable,
entertaining experience in a similar way as a magic trick. Educational
software is often gamified, for this reason: trying to make it entertaining
so children will want to use it repeatedly, rather than it being a chore.
Just as magicians want to engineer delight, often software engineers do
too. After all, if a shopping website or app is a delight to use, you are
more likely to return and so buy more.
However, user experience design is about more than this kind of
showmanship. It depends on the specific task at hand. For some software,
a good experience is about being able to do the job quickly and easily
with the minimum of fuss; about the software being very easy to use,
without frustration. A good experience may even be an experience where
360 Conjuring with Computation
you do not notice you have interacted with software at all. For example,
to open my car, I just grip the door handle, and it recognises me and
unlocks automatically. That is far better than having to dig around in
my pockets for keys.
built into clothes, but magicians have been doing this for years. What
tricks can you imagine doing with electronics, whether hidden controls,
sensors or displays built into your stage clothes? What might you do with
body-centric networks that use a person’s body as a network to pass
signals between different gadgets detecting things you touch? How about
buttons for controlling things built into your skin, or brain–computer
interfaces that can pick up a person’s simple thoughts and pass them
to a computer. A magician who is ahead of the game with mathematics,
science and technology can always invent new tricks and new ways of
doing old ones that baffle and entertain.
Creativity
Being a magician, inventing new tricks and adding new twists to old
ones, is not just about being on top of technology but all about
creativity too. Computer science is a similarly creative subject. In fact,
some computer scientists have gone a step further and see creativity as
something to investigate algorithmically. Machine creativity, computer
programs creating poems, jokes, songs, stories, films, and so on, is a
major area of research.
Machines have long ago been created to perform tricks but in future
they could create new ones too. Researchers have started to explore this
with artificial intelligence programs that develop the most magical
version of a trick. PhD student Howard Williams developed a program
modelled on evolution that created its own version of a jigsaw trick: the
teleporting robot trick (Chapter 11). It used a kind of algorithm called
a genetic algorithm to “breed” better and better versions of the trick,
selecting for ones that would appear most magical, exploring many more
possibilities than any human magician trying to develop a trick could.
True computer magicians are still in the future, but one day they may
be inventing their own completely novel tricks.
365
366 Conjuring with Computation
Computer Science
The Power of Computational Thinking: Games, Magic and Puzzles to
Help You Become a Computational Thinker, Paul Curzon and Peter W.
McOwan, World Scientific, 2017.
369
This page intentionally left blank
Index of Computing Terms
371
372 Conjuring with Computation
computation, 1–2, 4, 11, 16, 28, 36, data representation, 163–199, 285–294,
148, 169, 226, 253, 258, 281, 356 297–317, 355
computational agent, 11–12, 358 data representation, choice of,
computational thinking, 1, 44, 99, 104, 163–199, 285–294, 297–317, 355
119, 124, 165, 168, 265, 331, 355, data, store, 163, 180, 186, 189, 191,
359 195, 273, 285, 300–301, 308, 314,
computer, 1, 4–5, 11, 15, 24, 33–34, 323, 329
37, 49, 51, 77, 98, 125, 134, 142, data structure, 131, 143, 147, 151,
148, 149, 169, 174, 179–186, 189, 285–293
191, 207–208, 226, 235, 264, 273, data structure, graph, 285–293
281, 303, 314, 320, 322, 335, 343, data structure, list, 109, 114, 134, 147,
348–351, 356, 358, 361–362 169, 189, 272, 356
computer game (see game, computer), data, transmit, 191, 195, 197, 308,
50, 61, 134, 339, 348, 359, 363 317, 323
computer memory (see memory, debug, 48–49
computer), 62, 130, 179, 273 decimal number (see number system,
computer science, 1–2, 28, 30, 62, 84, decimal), 12, 175–181, 184–186
104, 161, 197, 214, 273, 291, 330, decision instruction (see selection),
337, 348, 361–362 19–25, 28, 62–63, 65–67, 138–139,
computer system, 2, 199, 208, 215, 184, 194, 333
218–219, 221, 234, 264, 295, 311, decode, 306–308
315, 357 decomposition, 63, 87, 95–101,
conceptual model, 221–227, 357 107–110, 119, 121–125, 134,
consistency, 357 143–160, 265, 275–283, 355
control structure, 15, 24–25, 28, 62 decomposition, parameterised,
control unit, 273 121–125, 150, 153
correctness, program, 15, 39, 41–51, delight, engineering, 99, 217, 237–244,
53–62, 111, 247–253, 255–264 357, 359
counter, 28–29 design metaphor, 132, 221, 225–227
counter-controlled loop, 28–29 design model, 224, 226
covert channel, 295, 316, 319–323, 360 design principle, 79, 84, 215, 217, 219,
creativity, 3, 68, 92, 115, 120, 125, 144, 226, 357
146, 345, 348, 352–353, 361–363 Difference Engine, 180
creativity, machine, 361 digit keypad, 208
critical system, 51, 62, 114, 141, 161, digital computer, 134, 142, 181
255 digital data, 197
cryptography, 303–309 digital image, 301
cursor, 208 digital money, 303
cursor-based number entry, 208 digital object, 187
cut and paste, 235 digital person (avatar), 339
digital world, 187, 189
D direct current (DC), 115
data abstraction (see abstraction, disaster, airline, 218
data), 163 disaster, rocket, 5, 61
data, preprocessing, 143, 147 divide and conquer, 275–283
374 Conjuring with Computation
F H
facial recognition, 363 hacker, 30, 114, 295, 311, 319, 322,
false value (see boolean value), 22–24, 328, 331, 335
28 hacker, black hat, 331
feature bloat, 79, 84 hacker, white hat, 331, 335
feedback, 217, 226, 357 hand dryer, 225
fingerprint recognition, 363 headlights, 233
flow of control, 15–16, 23–24, head-up display, 339, 342
28–29 high-level language (see programming
flowchart, 16, 23, 161 language, high-level), 37
focus of attention, 201–209, 211, 214 Hindu-Arabic numeral system (see
font, 94, 302 number system, Hindu-Arabic), 12,
formal logic (see logic, formal), 186 175, 179–180
Index of Computing Terms 375
usability principle (see design principle), vector graphics (see graphics, vector),
79, 84, 215, 217, 219, 226, 357 189
usable, 73, 84–85, 199, 208, 219, 237, verification, program, 161, 263
357 virtual world, 339–343
used, 207, 357 visibility, 215–219
user-centred design, 219 visibility of system state/status,
user experience, 199, 237–244, 357, 215–219
359 vision system, 71–74, 211–214
user experience design, 199, 237–244, visual design language, 94
357, 359 visual perception, 71–74, 211–214
user interface, 73, 77, 93–94, 207, 235 visual salience, 211–214
user interface designer, 207 voice authentication (see
user interface, graphical (GUI), 73, 77, authentication, voice), 363
93–94, 235 von Neumann architecture, 273
user model (see model, user),
224–225 W
UX design (see user experience design), wearable computing, 360
199, 237–244, 357, 359 white hat hacker (see hacker), 331, 335
wireless communications, 115, 323
V wireless control, 115, 323
valve, 142 wizard, programming, 12
variable, 124, 259, 263 word processor, 208
This page intentionally left blank
Index of Tricks, Illusions and
Conjuring Techniques
381
382 Conjuring with Computation
False Cut (*) (see Cyclic False Cut, Misdirection, 203–206, 209, 222,
Three Way False Cut), 90, 131–134, 232–233
141, 221–227, 238–241, 250, 252 Möbius Rings (*), 13–17
False Shuffle (*) (see False Cut), 105, Most Expensive Trick in the World, 30
111–130, 132, 141, 146, 250
False Top Overhand Shuffle (*), N
121–125, 129, 146, 230, 232, 347, Naming the dead (*), 170
349 Noah’s ark, 253
Find the Lady, 63 Numbers Game (*), 215–219
Floating lightbulb, 17
Forced Wizardly Book Magic (*), O
149–152
Forcing Matrix (*), 143–148 On the Power of Numbers (*), 187–190
Four Aces (*), 201–209 Op Art, 74
Free My Three (*), 275–283, 347 One True Sovereign (*), 353–363
optical illusion, 71–74, 170
G Out-of-Body Experience (*),
191–197
ghostly figures or images (*), 330, 332,
Overhand Shuffle (*), 107–110, 255,
339–343
258, 278, 297–298, 300
Overhand Shuffle (variations),
I
111–130, 146, 230, 232, 347, 349
Invisible Palming (*), 7–12, 101–103,
153–161 P
Paranormal, 335
J
Passing a bottle through a table,
Joker in the Pack (*), 229–235 244
performance (see showmanship), 1,
L 96–100, 152, 204, 230, 294, 334,
Levitation, 130 351–352, 355, 359, 363
Lottery Trick (*), 175–179 Pledge, The, 99–100
low gravity magic, 363 Pointillism, 74, 94
Pre-booked Picture Magic (*), 89–94
M Prestige, The, 99–100
magical objects, 13–17, 19–24, 41–45, Prestige, The (film), 115
71–77, 79–85, 95–104, 115, 149–154, Prestige, The (novel), 99
237–244, 267–273, 319–323, Psychometry, 170
331–335, 339–343, 348, 353–363 Pulling rabbits out of a top hat, 152
Magically Weighted Boxes (*), 75–77
Make coins appear, 152 R
Mechanical Turk, 348 Red-Black Mind Meld (*), 255–264
Mental Monte and the Trains of Riffle shuffle, 110, 358
Thought (*), 63–68 Rising card, 120
Mentalism, 170, 174, 303–330, 360 Roman Maths Challenge (*),
Mind reading, 53–62, 89–94, 181–186 171–174
Index of Tricks, Illusions and Conjuring Techniques 383
S T
Seance, 100, 130, 331, 334, 343 21-Card Trick (*), 53–62
self-working trick, 10–12, 15, 45, 83, Telepathy, 317
244, 258, 354, 358 Teleporting Robot (*), 79–85,
showmanship, 351, 355, 359, 363 361
shuffling (*), 107–130, 146, 230, 232, Teleporting Top Hat (*), 101–104,
255, 258, 278, 297–298, 300, 347, 153–154
349 Three Card Monte, 63
Side Jogging (*), 127–130 Three Way False Cut (*),
Six Magical Objects (*), 19–24 221–227
Sniff Out that Card (*), 297–302 Time misdirection, 204, 222,
spiritualism, 100, 130, 191–197, 328, 232–233
330–335, 343 Trained Rice (*), 95–100
street magic, 30, 152, 244, 294 Trick gone wrong ending, 26, 30
supernatural powers, 100, 130, Turn, The, 99–100
211–214, 237–264, 334–335 Turning the Key (*), 25–30
Supernatural Suggestion (*), 211–214
Superpower strength, 77 W
Swapped Ends Overhand Shuffle (*), Wizardly Book Magic (*), 41–51, 56,
111–115, 117–118 149–152, 270, 272
This page intentionally left blank
Index of People
385
386 Conjuring with Computation
N V
von Neumann, John, 273 Verma, Milan, 214
Nielsen, Jakob, 219 da Vinci, Leonardo, 190
Nolan, Christopher, 115
Norman, Don, 219 W
Wallace, Alfred Russel, 130
O Wheeler, David, 134
Ose, Jay, 227 Wilkes, Maurice, 134
Ôuchi, Hajime, 72, 74 Wing, Jeanette, 104
P X
Pacioli, Luca, 190 Xerox PARC Team, 235