Secondary Maths Uncategorized

Coding’s Back in Town – Let’s Use it to Inspire Maths



For me utilising coding in mathematics lessons started in my first school job. I was asked, “as a maths person can you code?” “Yes” was my response and off I went. I began by teaching students how to write retro-games such as Space Invaders and Pac-Man as a way of learning to code. This experience made me appreciate that some students gain a greater appreciation of certain mathematical concepts such as algebra from experimenting with code. They seem to gain more understanding than they would have from a more traditional lesson.

space invaders

I’m a great believer in students experimenting in the maths classroom. I believe this gives time for understanding to form and for students’ mathematical understanding to develop. Therefore typing a few lines of code using such languages as BASIC allows students time to think more logically about the processes and why they are important to the overall concept.

So now that coding is becoming very trendy with children, lets capitalise on that trend and bring it back into the maths classroom. There’s much to gain. Sitting just below the surface coding is everywhere. As I type this blog my laptop is coding for me. We take this very much for granted. The Executive Chairman of Google, Eric Schmidt says now is the time to learn to code – “for most people on Earth, the digital revolution hasn’t event started yet. Within the next 10 years, all that will change. Let’s get the whole world coding!”

How can we bring coding into mathematics lessons? Here’s an example of how to do this in a sixth form probability lesson.

Let’s start with a story…

How strange would it be if you and a friend started at two unrelated random points and ended up at the same place? What if you and five friends started at six random points and all ended up at the same place? Synchronicity, magic or maths?

Here is my variation on a game called Kruskal’s Count [1-6]. Ask a student to shuffle a deck of cards thoroughly. Deal out all the 52 cards, face up, in a line on the floor. Then one student is asked to start at the first card and walk on to the next position by using the value of that card.

coding 2


For example starting with the number 3. They then count to the next card using the value of the card, e.g. count three cards along to the 4. In this example they would then count four along to the 9, nine along to the 2, then two to the Queen. In my version of Kruskal’s Count any court card and 10 you land on as one. So they would then move along one to Jack and one more to 7 and so on until they can go no further.

playing cards

Once the first student has reached the final destination, the next is asked to repeat this journey through the cards starting at the next card. They very surprising thing is that no matter which of the first few cards you start at there is a very high chance that you will always end up at the same destination card – in this example it’s 9. The mathematics behind this game has to do with the fact that the various routes you take to get to the end cross each other along the way. When these routes have the same cards in their pattern they become one route. As they cross a number of times before getting to the end of the pack, the chance is high that they will land on the same cards. The more cards in the pattern the greater the chance that the routes will join.

It’s good for students to experiment practically first in this way as this allows time to develop conjectures. The computer offers a way of testing these by running the simulation a million times! But the coding itself allows the student to think more carefully, through each line of code, what has just happened.

When moving on to code the Kruskal’s Count the first thing students need to think about is how to create a random deck of cards through code on the computer. One way is to first code an ordered deck and then randomly swap cards through the second procedure. Running the code a few times allows the production of random decks of cards. Let’s have a look at how to start this:

rem ordered deck a$()

for m = 1 to 52

b$ = a$(m)

c = rnd(52)+1

a$(m) = a$(c)

a$(c) = b$

next m

The next stage is to code decisions. First the code needs to identify the starting card, second how many along to move from this card and then to repeat these steps until the final destination card is reached. If there are 13 different cards there would be a need for 13 decision statements, the one for the value 3 is shown below:

IF d$ = “3” THEN p = 3

If you are interested to see the whole code for this particular game it is available online [7] and free BASIC software [8]. Once the code has been written it can be run a million times to workout a very precise probability of multiple starting points landing on the same final card. In this case the probability is 0.9636. Quite amazing!

Students could think about theoretical probability after the coding has come to its final answer. An insight on how to calculate an approximate value of the theoretical probability is given in my paper ‘Magic Card Maths’ [2]. Here is a short summary:

Using six initial possible card starting points, the average position of a card is (1+2+3+….+6)/6=3.5

The average length of each subsequent jump you make from this point, moving from one card to the next is (1+2+3+….+9+1+1+1+1)/13 3.8

The number of expected cards which will be chosen after the first card is (52-3.5)/3.8=12.76, which is about 12. The chance you will land on the same card is approximately 1 /3.8

#Therefore the chance of missing all the cards is  coding 3

This means that you have around 3 chances in 100 that the trick will not work for all eight cards on the top row, but 97 times out of 100 that it will.

Once engaged students could experiment with other versions of the game such as:

1)     What is the probability if all court cards and tens are counted as 10?

2)     Would using two packs of cards create a greater chance of the trick working for all eight starting points?

3)     What would be the probability if only the A, 2, 3, 4, and 5 cards are used?

Happy Coding.


Steve Humble

Newcastle University



  1. Gardner, M. (1978)Mathematical Games, Scientific American. 238, No. 2.
  2. Havil, J. (2008). Impossible: Surprising Solutions to Counterintuitive Conundrums. Princeton, Princeton University Press
  3. Humble, S. (2010). Dr. Maths Randomness Show, Alchemist Cafe, Dublin.
  4. Humble, S. (2008). Magic Card Maths. The Montana Mathematics Enthusiast, 5, Nos: 2, 327-336.
  5. Kraus, A.F. Sum Total, Ibidem, 232, No. 12 (1957); No. 13 (1958), 255- 259.
  6. Lagarias, J.C., Rains, E. and Vanderbei, R.J. (2001). The Kruskal Count. Cornell University Library.
  7. Humble, S. (2015). Code for Kruskal.
  8. Chipmuck Basic website


Collins Secondary

Collins Secondary is the home of innovative learning resources for all stages of secondary education. We support thousands of teachers and pupils who are using our award-winning materials every day, and provide what you need to enhance the learning experience with our easy to use and flexible programmes.

More Posts - Website

Follow Me:

Leave a Comment