Cult TV series Doctor Who might be deep in the land of science fiction, but underneath the time travel, aliens and hijinx is, according to scriptwriter Stephen Thompson, a fair bit of maths.

Thompson, who has also written for the BBC’s Sherlock, is speaking to a room full of mathematicians at an event to celebrate the 150th anniversary of the London Mathematical Society, the UK’s leading learned society for the subject and one of the world’s oldest.

“Doctor Who is hugely popular in our culture, and perhaps no other show on British television raids the store cupboard of mathematics so mercilessly, often twisting those concepts beyond what is reasonable, but sometimes I hope generating some great stories out of it,” says Thompson.

Part of a team of writers that includes showrunner Steven Moffat and legendary author Neil Gaiman, Thompson is responsible for season six’s The Curse of the Black Spot, season seven’s Journey to the Centre of the TARDIS and season eight’s Time Heist.

However, before Doctor Who, Thompson had quite a different role.

“There are some of the greatest mathematical minds of a generation here in front of us in this room. Ladies and gentlemen, I’m not one of them” he explains.

“At best I’m a mediocre mathematician – what a charlatan I am standing in front of you here. Yes I did maths A Levels, I did a maths degree and I went on to be a maths teacher for twelve years at Tiffin School; I was head of maths.

“Then I took a sudden left turn and became a scriptwriter, and now I work with the BBC, and yes I’ve written for Sherlock and, indeed, Doctor Who.”

In the footsteps of the Simpsons

Although keen to highlight the mathematical underpinning and its benefits for the show’s 11 million fans, many of whom are children that may be encouraged to study the subject, Thompson is clear that Doctor Who is not unique on this front.

“If you’re fans of The Simpsons, one of the executive producers of The Simpsons and later a scriptwriter, he was a Harvard mathematician, and almost every episode of the Simpsons he just shoehorns in a little piece of mathematics,” says Thompson.

“The Simpsons was developed in 1989 when everybody had just got a video recorder, so what they’d do is they’d put these little freeze-frame jokes, where people can actually freeze the screen with their video recorder to show these little jokes.

“There’s this beautiful one where Bart Simpson is scribbling on a blackboard, like all the episodes do – the episode is called The Wizard of Evergreen Terrace, and he’s scribbling on the board, and quite casually he draws a counterexample to Fermat’s Last Theorem and then rubs it out before you’ve had a chance to look at it.”

Finding names for the TARDIS

There are two scenes we hate writing in Doctor Who

While it may seem that Doctor Who’s references to maths are more embedded, the programme also carries the occasional Simpsons-like element.

“In Doctor Who, we nod to mathematics. Sometimes we embrace it quite fully, but we nod to mathematics occasionally,” says Thompson, giving an example from The Curse of the Black Spot.

“There are two scenes we hate writing in Doctor Who. The first one is the scene that goes ‘oh look, it’s bigger on the inside’. We hate writing that because there’s no individual way of writing it.

“The other scene we all hate writing is the scene where the Doctor is standing by the console of the TARDIS, its shaking, he’s travelling through space, he’s being bombed by solar energy or something, and gradually bits of the TARDIS are exploding and he says, ‘oh no look, the atom accelerator has blown’ or something. Every single week we have to struggle to think of what new item on the TARDIS could be broken.

Thompson, however, decided to take inspiration from maths when this task lay to him.

“I wrote my first episode of Doctor Who five years ago, and I was struggling with that scene so I went to my pure mathematics textbook from my further maths course, I opened it at the index and stabbed my finger in it, at random, and chose a word.

“My first episode of Doctor Who was broadcast later that year, and the console is shaking, sparks are flying, smoke bombs are going off, and Matt Smith turns to Karen Gillan and says: ‘oh no, the parametric engines are jammed!’

The assembled mathematicians find this hilarious. Parametrics, for those of you that aren’t aware, is actually a branch of statistics.

Doctor Who takes on dimensions

Doctor Who doesn’t just namedrop maths terminology: in some episodes it deals with mathematical concepts far more directly.

One of the more recent episodes that fits in this category is season eight’s Flatline, which features monsters that only occupy two dimensions.

A brilliant concept that makes for some particularly chilling moments, the premise has its roots in a much earlier work of fiction.

“Many of you will have read a beautiful book called Flatland, which was written by Edwin Abbott,” explained Thompson.

“If you’ve not read it, go home and read it – it’s utterly charming. Edwin Abbott was a theologian in the 19th century and also a headteacher who wrote this beautiful fantasy book that asked: what if the world were two dimensional, what if the world were flat?”

The book, which is well-known in academic circles, and is rich with social commentary as well as taking a fascinating approach to dimensions.

“It’s actually a very interesting satire of Victorian society, because we imagine each different character were a polygon living in this flat world. And the number of sides you had reflected your status in society, so all the dowdy workers were little triangles because they’ve only got three sides and this very regal hexagon swans through the world because they have six sides,” explains Thompson.

“Last year a brilliant writer on our team called Jamie Mathieson wrote an episode called Flatline that explored exactly that. What if the Doctor encountered a two-dimensional monster? And more than that, what if the monster itself once it touched him or touched any human could take away dimensions so they too became flat?”

Neil Gaiman’s contributions to the TARDIS

One the greatest sources of maths-related Doctor Who is the Doctor’s time-travelling spaceship, the TARDIS.

“One of the most vital ingredients of Doctor Who is of course the TARDIS, and the fact that the Doctor has the greatest spaceship in the galaxy at his disposal is one of the most potent elements of Doctor Who,” says Thompson. “Some really interesting maths is involved in those episodes which centre around the TARDIS.”

One of these is Thompson’s own, Journey to the Centre of the TARDIS, but another is the first episode written by author Neil Gaiman: series six’s The Doctor’s Wife.

“He employs two really interesting ideas about this spaceship the TARDIS,” says Thompson.

“One is that it is bespoke, it can continually reinvent itself for every new Doctor – for every new owner of the TARDIS can create new rooms at will. But the other of course is that it’s multidimensional, so it does not obey the laws of three-dimensional geometry.

“Neil Gaiman represented this beautifully in his episode. What happened is Amy and Rory are chasing through the TARDIS and Rory of course starts appearing at that end of the corridor, but because the TARDIS can bend round multidimensionally, he appears at that end of the corridor suddenly.”

The multidimensional nature of the TARDIS is, according to Thompson, the subject of much discussion in Doctor Who script meetings.

We have the banana conversation all the time – it’s the most clumsy mathematical conversation you’ve ever heard

“We have a conversation in editorial meetings at BBC Wales about the TARDIS, and we call it the banana conversation,” explains Thompson.

“We have the banana conversation all the time – it’s the most clumsy mathematical conversation you’ve ever heard.

“Basically we’re trying to talk about how more dimensions than three dimensions can be represented. And we say time and again in meetings, well if you hold a banana above a flat surface and the banana just touches in two places, the spider running across the table will see the banana.

“It’s a terrible conversation – absolute rubbish mathematically – but you get the idea. It’s the best we can do.”

Time Heist: a crossing-the-river problem

Getting to grips with the spatial variability of the TARDIS isn’t the only dimensional issue that Doctor Who scriptwriters encounter.

The importance of time in the programme poses some interesting challenges of its own, which in one episode Thompson resolved using a school maths problem.

“Steven Moffat, who runs the show, came to me a few years ago and said I want to write an episode about bank robbery,” he explains.

“So I wrote an episode called Time Heist, which was broadcast last year, and its quite tricky actually, because if the Doctor’s doing a bank robbery but he’s got the TARDIS on his side; the TARDIS is multidimensional, it can turn up in any safe you want – locked solid, barred.

“That was our first problem, so we approached the problem a different way, quite mathematically actually. We approached the problem from the point of view of the bank: what would be the safest, the greatest bank in the galaxy?”

Thompson decided that the solution would be a telepath that could instantly detect that a person was entering the bank with the intention of robbing it, also ensuring that the Doctor could not just simply warp in with the TARDIS.

“So these are our two sides: the Doctor with his TARDIS, convening his way into the safe, but they have a telepath and they can stop him. At which point you have to work out a very delicate game between the two of them.”

The solution to this game, it turns out, was based on the crossing-the-river problem, which is a popular school teaching aid.

“You know: you’ve got a wolf, a goat and some cabbages on the side of the river, and only one of them can go in the boat at any one time,” says Thompson.

“You can’t leave the goat with the cabbages and you can’t leave the wolf with the goat because it would eat it, so you go through this very long, painful process of rowing the boat backwards and forwards until you get everyone where they need to be.”

The problem proved ideal for the basis of Thompson’s script.

“That is precisely the mathematical problem we were faced with in creating Time Heist,” he says.

“This is how the Doctor actually cracks it: what he does is very quickly in the TARDIS he takes the plan for the heist and cuts it into four bits. And then very quickly in the TARDIS he beams over to the bank and leaves those four bits of the plan and then beams back before the monster can read his mind.

“Then he wipes his own memory and sends himself back to the bank, and bit by bit he gets the plan. But of course he never knows all of it, so the monster can never read his mind.

“That was the long solution to the crossing the river problem.”