Hear and … their magic square

A magic three by three square with the numbers 2, 9 and 4 in the top row, 7, 5 and 3 in the middle row and 6, 1 and 8 in the bottom row. Each row, column and the two diagnonals add up to 15.
Image by CS4FN

Victorian Computer Scientists, Ada Lovelace and Charles Babbage were interested in Magic Squares. We know this because a scrap of paper with mathematical doodles and scribbles on it in their handwriting has been discovered, and one of the doodles is a magic square like this one. In a magic square all the rows, columns and diagonals magically add to the same number. At some point, Ada and Charles were playing with magic squares together. Creating magic squares sounds hard, but perhaps not with a bit of algorithmic magic.

The magical effect

For this trick you ask a volunteer to pick a number. Instantly, on hearing it, you write out a personal four by four magic square for them based on that number. When finished the square contents adds to their chosen number in all the usual ways magic squares do. An impressive feat of superhuman mathematical skills that you can learn to do most instantly.

Making the magic

To perform this trick, first get your audience member to select a large two digit number. It helps if it is a reasonably large number, greater than 20, as you’re going to need to subtract 20 from it in a moment. Once you have the number you need to do a bit of mental arithmetic. You need an algorithm – a sequence of steps – to follow that given that number guarantees that you will get a correct magic square.

For our example, we will suppose the number you are given is 45, though it works with any number.

Let’s call the chosen number N (in our example: N is 45). You are going to calculate the following four numbers from it: N-21, N-20, N-19 and N-18, then put them in to a special, precomputed magic square pattern.

The magic algorithm

Sums like that aren’t too hard, but as you’ve got to do all this in your head, you need a special algorithm that makes it really easy. So here is an easy algorithm for working out those numbers.

This four by four magic square contains the calculations needed to install the numbers in the correct positions so that the magic square will work with any large two digit number
Image by CS4FN.
  1. Start by working out N – 20. Subtracting 20 is quite easy. For our example number of 45, that is 25. This is our ‘ROOT’ value that we will build the rest from.
  2. N-19. Just add 1 to the root value (ROOT + 1). So 25 + 1 gives 26 for our example.
  3. N-18. Add 2 to the root value (ROOT + 2). So 25 + 2 gives 27.
  4. N-21. Subtract 1 from the root value (ROOT – 1). So 25 – 1 gives 24.
  5. Having worked out the 4 numbers created form the original chosen number, N, you need to stick them in the right place in a blank magic square, along with some other numbers you need to remember. It is the pattern you use to build your magic square from. It looks like the one to the right. To make this step easy, write this pattern on the piece of paper you write the final square on. Write the numbers in light pencil, over-writing the pencil as you do the trick so no-one knows at the end what you were doing.

A square grid of numbers like this is an example of what computer scientists call a data structure: a way to store data elements that makes it easy to do something useful: in this case making your friends think you are a maths superhero.

When you perform this trick, fill in the numbers in the 4 by 4 grid in a random, haphazard way, making it look like you are doing lots of complicated calculations quickly in your head.

Finally, to prove to everyone it is a magic square with the right properties, go through each row, column and diagonal, adding them up and writing in the answers around the edge of the square, so that everyone can see it works.

The final magic square for chosen number 45

So, for our example, we would get the following square, where all the rows, columns and diagonals add to our audience selected number of 45.

This four by four magic square is the result of taking the chosen number 45 and performing the sequence of calculations (the algorithm) using it as 'N'.
Image by CS4FN.

Why does it work?

If you look at the preset numbers in each row, column and diagonal of the pattern, they have been carefully chosen in advance to add up to the number being subtracted from N on those lines. Try it! Along the top row 1 + 12 + 7 = 20. Down the right side 11 + 5 + 4 = 20.

Do it again?

Of course you shouldn’t do it twice with the same people as they might spot the pattern of all the common numbers…unless, now you know the secret, perhaps you can work out your own versions each with a slightly different root number, calculated first and so a different template written lightly on different pieces of paper.

Peter McOwan and Paul Curzon, Queen Mary University of London


Related Magazine …


Download our free magic booklets


Cartoons, comics and computer games – Ada Lovelace’s graphic novel

by Jane Waite, Queen Mary University of London

In 2009 for Ada Lovelace day, a comic strip about Ada and Babbage was created, not quite 100% historically accurate but certainly in the spirit of Lovelace’s love of science and mathematics. Her thrilling adventures in Victorian London have now become a graphic novel.

Image by Andrew Martin from Pixabay

In her own time, Ada was captured as a demure and beautiful young woman in portraits and sketches that were shared in books about her father. Ada would have sat for hours to have her portrait drawn, but she would have known about quick draw cartoons. Newspapers and magazines such as Punch contained satirical cartoons of the day. They were very influential in the 1840’s. Faraday was drawn in Punch, but Babbage and Lovelace didn’t make it then. But now they are crime busting mathematical superheros in their very own alternate history of computing comic book.

Books, films, even a musical have been created about Ada Lovelace, but as we write the circle has not quite been closed. There are no computer games about Ada. But maybe you could change that.


Further reading

The Thrilling Adventures of Lovelace and Babbage: The (Mostly) True Story of the First Computer by Sydney Padua.


This article was first published on the original CS4FN website and a copy can be found on page 17 of issue 20 of the CS4FN magazine, which celebrates the work of Ada Lovelace. You can also read some of our other posts about Ada Lovelace and she features as one of our Women in Computing poster set.


Related Magazine …


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

Ada and the music machine

A man playing a barrel organ with a soft toy monkey.
Image by Holger Schué from Pixabay

Charles Babbage found barrel organs so incredibly irritating that he waged a campaign to clear them from the streets, even trying to organise an act of parliament to have them banned. Presumably, it wasn’t the machine Babbage hated but the irritating noise preventing him from concentrating: the buskers in the streets outside his house constantly playing music was the equivalent to listening to next door’s music through the walls. His hatred, however, may have led to Ada Lovelace’s greatest idea.

It seems rather ironic his ire was directed at the barrel organ as they share a crucial component with his idea for a general purpose computer – a program. Anyone (even monkeys) can be organ grinders, and so play the instrument, because they are just the power source, turning the crank to wind the barrel. Babbage’s first calculating machine, the Difference Engine was similarly powered by cranking a handle.

The barrel itself is like a program. Pins sticking out from the barrel encode the series of notes to be played. These push levers up and down, which in turn switch valves on and off, allowing air from bellows into the different pipes that make the sounds. As such it is a binary system of switches with pins and no pins round the barrel giving instructions meaning on or off for the notes. Swap the barrel with one with pins in different positions and you play different music, just as changing the program in a computer changes what it does.

Babbage’s hate of these music machines potentially puts a different twist on Ada Lovelace’s most visionary idea. Babbage saw his machines as ways to do important calculations with great accuracy, such as for working out the navigation tables ships needed to travel the world. Lovelace, by contrast, suggested that they could do much more and specifically that one day they would be able to compose music. The idea is perhaps her most significant, and certainly a prediction that came true.

We can never know, but perhaps the idea arose from her teasing Babbage. She was essentially saying that his great invention would become the greatest ever music machine…the thing he detested more than anything. And it did.

– Paul Curzon, Queen Mary University of London

More on …

Related Magazines …


This article was funded by UKRI, through Professor Ursula Martin’s grant EP/K040251/2 and grant EP/W033615/1.

QMUL CS4FN EPSRC logos

Ada Lovelace in her own words

A jumble of letters
Image by Paul Curzon

Charles Babbage invented wonderful computing machines. But he was not very good at explaining things. That’s where Ada Lovelace came in. She is famous for writing a paper in 1843 explaining how Charles Babbage’s Analytical Engine worked – including a big table of formulas which is often described as “the first computer program”.

Charles Babbage invented his mechanical computers to save everyone from the hard work of doing big mathematical calculations by hand. He only managed to build a few tiny working models of his first machine, his difference engine. It was finally built to Babbage’s designs in the 1990s and you can see it in the London Science Museum. It has 8,000 mechanical parts, and is the size of small car, but when the operator turns the big handle on the side it works perfectly, and prints out correct answers.

Babbage invented, but never built, a more ambitious machine, his Analytical Engine. In modern language, this was a general purpose computer, so it could have calculated anything a modern computer can – just a lot more slowly. It was entirely mechanical, but it had all the elements we recognize today – like memory, CPU, and loops.

Lovelace’s paper explains all the geeky details of how numbers are moved from memory to the CPU and back, and the way the machine would be programmed using punched cards.

But she doesn’t stop there – in quaint Victorian language she tells us about the challenges familiar to every programmer today! She understands how complicated programming is:

“There are frequently several distinct sets of effects going on simultaneously; all in a manner independent of each other, and yet to a greater or less degree exercising a mutual influence.”

the difficulty of getting things right:

“To adjust each to every other, and indeed even to perceive and trace them out with perfect correctness and success, entails difficulties whose nature partakes to a certain extent of those involved in every question where conditions are very numerous and inter-complicated.”

and the challenge of making things go faster:

“One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation.”

She explains how computing is about patterns:

“it weaves algebraical patterns just as the Jacquard-loom weaves flowers and leaves”.

and inventing new ideas

“We might even invent laws … in an arbitrary manner, and set the engine to work upon them, and thus deduce numerical results which we might not otherwise have thought of obtaining”.

and being creative. If we knew the laws for composing music:

“the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.”

Alan Turing famously asked if a machine can think – Ada Lovelace got there first:

“The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform.”

Wow, pretty amazing, for someone born 200 years ago.

– Ursula Martin, University of Oxford (From the archive)


More on …

Related Magazines …


EPSRC supported this article through research grants (EP/K040251/2 and EP/K040251/2 held by Professor Ursula Martin as well as grant EP/W033615/1). 

QMUL CS4FN EPSRC logos

Ada Lovelace: Visionary

It is 1843, Queen Victoria is on the British throne. The industrial revolution has transformed the country. Steam, cogs and iron rule. The first computers won’t be successfully built for a hundred years. Through the noise and grime one woman sees the future. A digital future that is only just being realised.

Red floating treble clef
Image by Gerd Altmann from Pixabay

Ada Lovelace is often said to be the first programmer. She wrote programs for a designed, but yet to be built, computer called the Analytical Engine. She was something much more important than a programmer, though. She was the first truly visionary person to see the real potential of computers. She saw they would one day be creative.

Charles Babbage had come up with the idea of the Analytical Engine – how to make a machine that could do calculations so we wouldn’t need to do it by hand. It would be another century before his ideas could be realised and the first computer was actually built. As he tried to get the money and build the computer, he needed someone to help write the programs to control it – the instructions that would tell it how to do calculations. That’s where Ada came in. They worked together to try and realise their joint dream, jointly working out how to program.

Ada also wrote “The Analytical Engine has no pretensions to originate anything.” So how does that fit with her belief that computers could be creative? Read on and see if you can unscramble the paradox.

Ada was a mathematician with a creative flair and while Charles had come up with the innovative idea of the Analytical Engine itself, he didn’t see beyond his original idea of the computer as a calculator, she saw that they could do much more than that.

The key innovation behind her idea was that the numbers could stand for more than just quantities in calculations. They could represent anything – music for example. Today when we talk of things being digital – digital music, digital cameras, digital television, all we really mean is that a song, a picture, a film can all be stored as long strings of numbers. All we need is to agree a code of what the numbers mean – a note, a colour, a line. Once that is decided we can write computer programs to manipulate them, to store them, to transmit them over networks. Out of that idea comes the whole of our digital world.

Ada saw even further though. She combined maths with a creative flair and so she realised that not only could they store and play music they could also potentially create it – they could be composers. She foresaw the whole idea of machines being creative. She wasn’t just the first programmer, she was the first truly creative programmer.

– Paul Curzon, Queen Mary University of London

This article was originally published at the CS4FN website, along with other articles about Ada Lovelace. We also have a special Ada Lovelace-themed issue of the CS4FN magazine which you can download as a PDF (click picture below).

See also: The very first computers and Ada Lovelace Day (2nd Tuesday of October). Help yourself to our Women in Computing posters PDF.