Johnny Ball’s ‘Two Wrongs Do Make a Right’ Trick

(or how to stack properly)

We present a CS4FN exclusive (from our archive): a mathematical card trick contributed by TV celebrity mathematician, Johnny Ball. The BAFTA-winning father of radio DJ Zoë Ball is famous for his TV shows like Think of a Number, Think Again and Johnny Ball Explains All, plus numerous brilliant books on maths and science.

The trick is as follows, in Johnny’s words…

There is a very simple card trick that I often use, as I can get to the explanation very quickly. In fact, for a mathematically minded audience, they are already some way to working out why it works, as soon as it does work. It’s called the Two Wrongs Do Make a Right Trick.

Just memorize the top card of a pack; let’s assume it’s the 4 of Hearts. Hand the pack to someone and ask them to choose any smallish number (eg between 1 and 10), then to deal that number of cards face down. Then announce the next card dealt will be the 4 of Hearts. Oops – it isn’t? Get them to place all the dealt cards on top of the pack again. Now ask them to choose a number bigger than the first one, but not so big that we run out of cards (eg between 10 and 20). They now deal that number face down, and the next card dealt will be the 4 of Hearts. Oops – wrong again. Let’s now see if we can make two wrongs equal a right.

Place the cards back on top. Now ask for a last chance. Take the smaller number from the larger. They now deal that number face down, and the next card dealt is the 4 of Hearts. Success!

Why does it work?

Hidden in this distracting presentation, the tale of your mistakes played up for laughs, is some clever mathematics. It uses a magic technique called a stack, which is a set up sequence of cards in a known order. Sometimes magicians will put a pre-defined stack of cards, for example four aces, on top of the deck and do a false shuffle, and then be able to deal a wining poker hand because they know exactly what cards will be dealt. In this trick your ‘mistakes’ create the stack for you.

X stacks the deck

Starting with your known card on top (that’s your secret card), your spectator chooses a number from 1 to 10. Let’s call that number X. You deal down X cards so that the first card down is the secret card, next down is the second from the original top of the pile and so on. At the end of your count of X there are X cards on the table, with the secret card on the bottom. Oops, mistake number one. Don’t worry, you can regroup. Just place the mistaken card back on top of the pile of undealt cards, then stack those X dealt cards back on top, and your deck now looks like this from the top down: (X-1) indifferent cards, the secret card, and the rest of the pack.

Y marks the spot

Second try – this time you ask for a bigger number, a number between 10 and 20. Let’s call that new number Y. Counting and dealing down Y cards, where number Y is bigger than number X, means that once Y cards are on the table you can reveal mistake two, oops again. But let’s ‘think of the numbers’ of the cards on the table. That pile has Y cards, and because we stacked the deck in the first phase the secret card is now at position X from the bottom of these Y cards, meaning it’s at position Y-X. Exactly the position in the stack to be revealed by the finale where you subtract X from Y and count down (oh wait, that’s another mathematical TV programme).

Think again – with some real numbers!

The algebra of Xs and Ys says it works, but to see this is true let’s test it and think of some specific numbers, say X=5 and Y=15. The first deal stacks the secret card at position 5 from the top. We then do the second deal of 15 cards, reversing as we go. Onto the table goes 1, 2, 3, 4, SECRET, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15. Pick up this pile and put it on top of the pack. The secret card is now in a new stack. From the top down it’s at 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, SECRET, rest of pack. Now count down 10 cards from the top of this new stack (that’s X-Y = 15-5 = 10), dealing 10 cards from the final stack gives us 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, and the next card is the SECRET card! Magic.

Proving algorithms work

A magic trick like this is what computer scientists call an ‘algorithm’: steps that if you follow them guarantee some result – here that your secret card is revealed. What we showed above is a logical, mathematical proof that the trick works. Now, programs are just algorithms (written in a programming language) so in the same way as we proved our trick works we can prove logically that a program we write always works too!

Even if you do do such a proof, it is also a good idea with software to do lots of testing too – putting in some values to check it works for specific cases too (lots more than the one example we checked). Doing that first can also help you come up with the proof.

Finale the stack pays off

Stacks, in this case the series of cards in a given order, that you deal from the top of the pack, are also used in computer science, though the meaning is slightly different. It is a data structure, a place to store information, that works like a stack of chairs. You can add a chair or take one from the top of a stack of chairs but not from the middle, and its the same with adding and removing data in a computer science stack.

Computer scientists like to have ways of thinking of data, normally numbers in the computer memory, in such a way that they know where things are so they can use them when they need them. A stack is an example of what computer scientists call an abstract data type, a data structure with rules of how you add and remove data to and from it. The stack is a ‘container’ of data (or nodes if the things stored are more complex than just plain numbers, and link off to other parts of memory). Computer scientists do two basic things to stacks: they ‘push’ and they ‘pop’. In a push a new element is added to the top of the stack, and all those elements under effectively move down a place. It’s like adding those cards on top of the secret card. In a pop the top element is removed from the stack, and up jumps all the elements underneath, like dealing a card from the top of the deck. Your laptop and your mobile phone will have a stack somewhere in them. The software will be pushing and popping and doing lots of other clever computer science tricks to keep track of the information and how it’s moving around. The programmer will probably use even smarter things like stack pointers. These are a note for the computer to tell it what was last changed and where in the stack it was.

One too many?

One of the nastiest (and easiest to make) errors in computer software can come from something called a stack overflow. A program pops from or pushes to the top of the stack. That position in memory is kept track of using the ‘stack pointer’, which is just like a sign pointing to where the top of the stack is. So we push and pop from where the stack pointer is pointing changing the position of the pointer as we do. A stack normally has a fixed maximum size (like hitting the ceiling with a stack of chairs). A stack overflow is where you shoot off the end of the stack, so where you try to push a value on the stack when it is already full. It is like a magician counting past 52 cards. It can only end in tears. Sometimes this nasty trick is used by computer hackers to gain control of a computer system. Once the stack has been overshot, the computer surrenders. The software has literally lost its place, and the hackers can move in and take over.

Fortunately maths can come to the rescue by creating tests to ensure that the stack doesn’t pop its clogs or push up the daisies. Even better you can prove that the program controlling the stack (just an algorithm) never lets you overflow the stack in the same way that we proved our trick always works.

Let’s pop over and give the last word to Johnny

Ever ready to make things simple to understand and easy to do, Johnny points out:

You can reduce the trick to choosing just 1 and then 2 with just three cards, and show that the manoevring moves the top card down by the difference in numbers chosen.

Now why didn’t we think of that?

Peter W McOwan, Paul Curzon, Queen Mary University of London

but critically of course (and also inspiring us about Maths since we were kids), Johnny Ball

More on …

Subscribe to be notified whenever we publish a new post to the CS4FN blog.


The Proof of Love

A pseudocode poem in a red heart:
Violets are violet
if roses are red and violets are blue
then
    Life is sweet
else
    I love you
Image by CS4FN

For Valentine’s day we created this card with a pseudocode love poem. But what does it mean? Is it a statement of love or not? Now that Valentines Day is gone and logic and rational thinking are starting to reassert themselves, here is a logical argument of what it means: an informal proof of love.

We are using our own brand of poetic pseudocode here, and what matters is the formal semantics of the language (ie mathematical meaning).

What we will do is simplify the code to code that is mathematically equivalent but far simpler, explaining the semantics as we do. We will by reasoning in a similar way to doing algebra, just replacing equals with equals.

First let’s write the poem out in more program looking notation, making clearer what are statements (actions) and expressions (things that evaluate to a value. In the English reading version we are using the verb TO BE in both ways which confuses things a little.

Let’s make clear the difference between variables (that hold values and values). The colours are all values (data that can be stored) – so we will write them in capitals: RED, BLUE, VIOLET. The flowers are variables (places to store values) so we will leave them as lowercase: violets, roses. Then the title becomes an assignment (written more formally as :=). It sets the value of variable violets to value VIOLET. (We are pedantic and believe the colour of violet flowers is violet not blue!)

What comes after the keyword IF is an expression – it evaluates to a boolean (TRUE value or FALSE value) so is referred to as a boolean expression. You can think of boolean expressions as tests – true or false questions. We will use == to indicate a true/false question about whether two things are the same value.

The other two lines are statements – think of them as print statements that print a message as the action they do.

The algorithm of the poem then is:

violets := VIOLET;
IF ((roses == RED) AND (violets == BLUE))
THEN
PRINT "Life is Sweet"
ELSE
PRINT "I love you"


Now let us simplify the algorithm to an equivalent version a step at a time. In the assignment of the first line, the variable violets is set to value VIOLET and then not changed, so we can substitute the value (a colour in uppercase) VIOLET for variable violets (in lowercase) where it appears, and remove the assignment. This gives a simpler version of the algorithm that does exactly the same:

IF ((roses == RED) AND (VIOLET == BLUE))
THEN
PRINT "Life is Sweet"
ELSE
PRINT "I love you"

Now in the boolean expression, we have the subexpression

(VIOLET==BLUE)

but these are two different values and this is asking whether they are the same or not. It evaluates to true if they are the same and FALSE if they are different. It is therefore equivalent to FALSE so the whole expression becomes:

(roses==RED) AND FALSE

The original algorithm is equivalent to

IF ((roses == RED) AND FALSE)
THEN
PRINT "Life is Sweet"
ELSE
PRINT "I love you"

Now whatever X is a boolean expression (X & FALSE) will always evaluate to FALSE because it needs both to be TRUE for the whole to be TRUE. The whole boolean expression therefore simplifies to FALSE and the algorithm to:

IF (FALSE)
THEN
PRINT "Life is Sweet"
ELSE
PRINT "I love you"

Notice how this means it does not matter what colour the roses are at all. Whether roses are red, white, blue or something else, the algorithm result will not be affected.

The semantics of an IF statement is that it evaluates exactly one of its two statements. Where its test (boolean expression) evaluates to TRUE, it executes the first THEN branch (here the Life is Sweet branch) and ignores the other ELSE branch (the I love you branch). Where its test evaluates to FALSE it instead ignores the THEN branch completely and executes the ELSE branch.

Here the test is FALSE, so it ignores the THEN branch and is equivalent to the ELSE branch. The algorithm as a whole is exactly equivalent to the far simpler algorithm:

PRINT "I love you"


Going back to the poem, it is therefore logically completely equivalent to a poem
I Love You

We have given a mathematical proof of love! The idea though is that only computer scientists with a formal semantics (ie maths meaning) of the pseudocode language used would see it!

More on …

Paul Curzon, Queen Mary University of London

Subscribe to be notified whenever we publish a new post to the CS4FN blog.


Conjuring with logic: the remote control red-black mind meld

Magic tricks are just algorithms – they involve a magician following the steps of the trick precisely. But how can a magician be sure a trick will definitely work when they do it in front of an audience? Just like computer scientists who can prove their algorithms always work, a magician can use logic and deduction to be sure their tricks always work too.

A mixture of spread red and black playing cards
Image from Pixabay

Try the following trick on your own first, before reading about how it works or trying it with a volunteer. Take the role of both the magician and volunteer. I will do my best to remote-control the magic via my own mind-meld with you as you read my words, to try and make sure it works for you!

The magical effect

Take a full pack of playing cards and give them a good shuffle. Fan the cards face down. Now ask a volunteer to think of the colour RED and point to a card. Cut the pack at that point and count the top eight cards into a pile face down. Now ask them to think BLACK and touch another card. Cut the pack there and count out 7 cards onto the pile. Repeat this with 6 cards and then 5 cards having them think RED and then BLACK. Have the volunteer shuffle the selected cards then turn the pile face up.

4 piles of cards with matching "RED" and "BLACK" piles
Image by Paul Curzon

You take the cards that are left and shuffle them, keeping them face down. Have the volunteer now run through their cards taking out groups of cards that are together and of the same colour. They should put them either in a “RED” pile in front of them if they are a group of red cards, or in a “BLACK” pile if they are black, telling you how many cards they have placed in that pile. As they take each group of cards, you, focussing hard on your cards, but without looking at what they are, pick out the same number of cards from points through the pack as the volunteer put down, and put them in your own corresponding “RED” or “BLACK” pile in front of you, Place your cards face down. In this way, they end up with a pile of red cards and a pile of black cards, both face up in front of them. You end up with two corresponding face down piles in front of you. They have seen their cards but yours contain cards that you have not seen. Once they have run out of cards and you have placed your matching cards, stop and think for a moment, then go through your two piles and swap two cards without looking at them, saying you believe you made a couple of mistakes and those two ended up in the wrong piles.

Now, looking happy, tell the volunteer that through a mind-meld between you, them and the red and black cards you have, you believe, succeeded in your aim. Remind them that the piles were shuffled, they were responsible for the way the cards were split which was at random, and you haven’t seen any of your cards. One card out at any point and the trick would not have worked. Despite this, announce that you have managed to place the same number of red cards in your red pile as you have placed black cards in your black pile. Pass them the two piles in turn and ask them to check by first counting the red cards in your face down “RED” pile onto the table, and then do the same with the black cards that are in your “BLACK” pile.

Amazingly you have succeeded, the number of red cards in the red pile is the same as the number of black cards in the black pile.

Proving it always works

Did I mind meld with you to make it work? Or is something else going on? We can actually use logic and deduction (with some algebra) to show it works.

First we need to make a mathematical model of the situation – essentially describe the situation, or at least the parts of it that matter, in maths. That sounds a bit scary but it isn’t really. Its just giving names to some piles of cards! Let’s go through it…

In the trick, we end up with four piles of cards. A RED pile and a BLACK pile, both face up, in front of the volunteer and a RED pile and a BLACK pile, both face down, in front of the magician (see the picture above). We do not know how many cards are in each pile and it will be different each time we do the trick anyway because of all the shuffling. Luckily the actual numbers don’t matter. In the trick we care about the numbers of red cards and the numbers of black cards, Let us therefore use mathematical variables to represent these different numbers (that just means give them names!). Mathematicians often use names like x and y for variables, but to help us remember what they stand for we will use variations on R and B to mean numbers of red and black cards respectively.

So, we will use R to stand for the number of red cards in the volunteer’s face up “RED” pile (which are all red) and B to mean the number of black cards in the volunteers face up “BLACK” pile (which are all black).

Now for the magician’s face down piles. They have both red and black cards in each pile so we will write R1 to mean the number of red cards in the magician’s face down “RED” pile and B1 to mean the number of black cards in the magician’s face down “RED” pile. Similarly, we will write R2 to mean the number of red cards in the magician’s face down “BLACK” pile and B2 to mean the number of black cards in the magician’s face down “BLACK” pile. We have the situation as shown below in the picture..

4 piles of cards showing them labelled with variables R, B, B1, R1, R2 and B2

Image by Paul Curzon

In doing this we are doing a form of abstraction – hiding of detail – we have hidden the actual numbers of red and black cards in each pile within our description, describing them with variables as the precise numbers do not matter (and are different every time we do the trick anyway).

So far so good. Now, we want to try and prove that the trick always works. But how to start? First, just think of any facts you know about the situation (and especially anything you know about red and black cards). It may not be obvious how it can help, but write it down anyway!

Well the first thing we know is that a full pack of cards was used, and there are 52 cards in a pack, half (26) red and half (26) black. All these cards ended up on the table in one pile or another. So that means if we add up all the red cards in the four piles it will equal 26. How can we write this in terms of our variables? It is just:

R + R1 + R2 = 26

We add the three variables that stand for numbers of red cards and it equals 26. There are only three things to add for the four piles as one pile is all black. We can say a similar thing about the black cards:

B + B1 + B2 = 26

Now we have two different sums that equal the same thing (26) so we can put them together as equalling each other to get a combined equation which we will call EQUATION 1.

R + R1 + R2 = B + B1 + B2                                         (EQUATION 1)

That doesn’t seem to tell us much useful, but don’t worry, we are just gathering facts for now. What else do we know about how the trick worked? Well, every time the volunteer put some number of cards in their “RED” pile, the magician puts the same number of cards in their “RED” pile (though those cards may be red or black). That means that, throughout the two “RED” piles hold the same number of cards. The number of cards in one “RED” pile is R and the number of cards in the other “RED” pile is R1 + B1 (the reds plus the blacks in that pile). We can write this out as an equation:

R = R1 + B1

Now, this tells us that R is exactly the same as R1 + B1 so anywhere we see R we can replace it with R1 + B1. In particular, we can make this switch in EQUATION 1 to give:

(R1 + B1) + R1 + R2 = B + B1 + B2                                (EQUATION 2)

With the same logical reasoning, we can deduce an equation for the “BLACK” piles too. The number of cards in one “BLACK” pile is B and the number of cards in the other “BLACK” pile is R2 + B2 (the reds plus the blacks in that pile). We can write this out as an equation:

B = R2 + B2

Substituting R2 + B2 for B into EQUATION 2 we get EQUATION 3

(R1 + B1) + R1 + R2 = (R2 + B2) + B1 + B2                     (EQUATION 3)

Now, this seems to have just made things more complicated and not got us anywhere much, but we can now simplify it. Notice that there are two R1s on one side and two B2s on the other, each added, so we can group them together to get:

2R1 + B1 + R2 = R2 + 2B2 + B1

Also, on each side there is a B1 and that pair can cancel out (just subtract B1 from both sides and the equality holds but it is simpler), and on each side there is an R2 which can cancel in the same way. This leaves us with:

2R1 = 2B2

Of course the multiplication by 2 on both sides cancels too (just divide both sides by 2 and they will still be equal). We get:

R1 = B2

That is a very simple equation. It basically tells us that if you follow the steps of this trick that, whatever numbers of reds and blacks end up in each pile, R1 is guaranteed at the end to be the same number as B2. So what does that mean back in the real world? R1 just stands for the number of red cards in the magician’s “RED” pile. B2 just stands for the number of black cards in the magician’s “BLACK” pile. The equation we are left with just tells us that as long as we follow the steps (actually as long as we make the number of cards in each pair of piles match) then at the end the number of red cards in the magician’s “RED” pile will be the same as the number of black cards in the magician’s “BLACK” pile…and that is the “magical” result we predicted!

The trick will always work if you follow the steps!

Proving software and hardware always works

A magic trick involves the magician following steps precisely – following an algorithm. That is all computer hardware and software do – they follow algorithms to achieve some desired result (a magical effect). Just as we proved the trick always works, we can prove that other algorithms (whether implemented as a program or hardware) work using logical deduction too. That is one of the reasons why logic and deduction are so important to computer scientists. You do not want a trick to go wrong in front of an audience, so it is worth proving it always works. How much more important is it that all that software we now rely on for everyday life is error free. And what about a medical device keeping someone alive, or the software flying a plane. We want to be sure they always work. If they contain mistakes, people die. Logic and proof can therefore save lives, not just magic shows.

by Paul Curzon, Queen Mary University of London

More on …


Subscribe to be notified whenever we publish a new post to the CS4FN blog.



EPSRC supports this blog through research grant EP/W033615/1. 

The invisible dice mystery – a magic trick underpinned by computing and maths

Red dice image by Deniz Avsar from Pixabay

The Ancient Egyptians, Romans and Greeks used dice with various shapes and markings; some even believed they could be used to predict the future. Using just a few invisible dice, which you can easily make at home, you can amaze your friends with a transparent feat of magical prediction.

The presentation

You can’t really predict the future with dice, but you can do some clever magic tricks with them. For this trick first you need some invisible dice, they are easy to make, it’s all in the imagination. You take your empty hand and state to your friend that it contains two invisible dice. Of course it doesn’t, but that’s where the performance come in. You set up the story of ancient ways to predict the future. You can have lots of fun as you hand the ‘dice’ over and get your friend to do some test rolls to check the dice aren’t loaded. On the test rolls ask them what numbers the dice are showing (remember a dice can only show numbers 1 through 6), this gets them used to things. Then on the final throw, tell them to decide what numbers are showing, but not to tell you! You are going to play a game where you use these numbers to create a large ‘mystical’ number.

To start, they choose one of the dice and move it closer to them, remembering the number on this die. You may want to have them whisper the numbers to another friend in case they forget, as that sort of ruins the trick ending!

Next you take two more ‘invisible dice’ from your pocket; these will be your dice. You roll them a bit, giving random answers and then finally say that they have come up as a 5 and a 5. Push one of the 5s next to the dice your friend selected, and tell them to secretly add these numbers together, i.e. their number plus 5. Then push your second 5 over and suggest, to make it even harder, to multiply their current number by 5+5 (i.e. 10 – that’s a nice easy multiplication to do) and remember that new number. Then finally turn attention to your friend’s remaining unused die, and get them to add that last number to give a grand total. Ask them now to tell you that grand total. Almost instantly you can predict exactly the unspoken numbers on each of their two invisible dice. If they ask how it you did it, say it was easy – they left the dice in plain sight on the table. You just needed to look at them.

The computing behind

This trick works by hiding some simple algebra in the presentation. You have no idea what two numbers your friend has chosen, but let’s call the number on the die they select A and the other number B. If we call the running total X then as the trick progresses the following happens: to begin with X=0, but then we add 5 to their secret number A, so X= A+5. We then get the volunteer to multiply this total by 5+5 (i.e. 10) so now X=10*(A+5). Then we finally add the second secret number B to give X=10(A+5)+B. If we expand this out, X= 10A+50+B. We know that A and B will be in the range 1-6 so this means that when your friend announces the grand total all you need to do is subtract 50 from that number. The number left (10*A+B) means that the value in the 10s column is the number A and the units column is B, and we can announce these out loud. For example if A=2 and B=4, we have the grand total as 10(2+5)+4 = 74, and 74 – 50= is 24, so A is 2, and B is 4.

In what are called procedural computer languages this idea of having a running total that changes as we go through well-defined steps in a computer program is a key element. The running total X is called a variable, to start in the trick, as in a program, we need to initialise this variable, that is we need to know what it is right at the start, in this case X=0. At each stage of the trick (program) we do something to change the ‘state’ of this variable X, ie there are rules to decide what it changes to and when, like adding 5 to the first secret number changes X from 0 to X=(A+5). A here isn’t a variable because your friend knows exactly what it is, A is 2 in the example above, and it won’t change at any time during the trick so it’s called a constant (even if we as the magician don’t know what that constant is). When the final value of the variable X is announced, we can use the algebra of the trick to recover the two constants A and B.

Other ways to do the trick

Of course there are other ways you could perform the trick using different ways to combine the numbers, as long as you end up with A being multiplied by 10 and B just being added. But you want to hide that fact as much as possible. For example you could use three ‘invisible dice’ yourself showing 5, 2 and 5 and go for 5*(A*2+5) + B if you feel confident your friend can quickly multiply by 5. Then you just need to subtract 25 from their grand total (10A+25+B), and you have their numbers. The secret here is to play with the presentation to get one that suits you and your audience, while not putting too much of a mental strain on you or your friend to have to do difficult maths in their head as they calculate the state changes of that ever-growing variable X.

Paul Curzon, Queen Mary University of London


More on …


Related Magazine …


Subscribe to be notified whenever we publish a new post to the CS4FN blog.


This blog is funded by EPSRC on research agreement EP/W033615/1.

QMUL CS4FN EPSRC logos