You're reading: Posts By Samuel Hansen

I’ve made myself a calculator

Screenshot of my calculator, showing 3867/5000 = 0.7734

For the past couple of weeks at work, I’ve been checking through our computer-based assessments before the students have a go at them. That means I’ve had to do lots and lots of calculations by hand, to confirm the computer’s got the right answer. Well, not quite by hand – I use a calculator for the stuff that I can’t keep in my head.

I’ve got a calculator app called RealCalc Plus on my phone, which I highly recommend. The main thing it’s got going for it is its RPN mode. While most calculators ask you to type in expressions pretty much as you’d see them on a page, read from left to right with numbers separated by operators and brackets, reverse Polish notation looks like this:

1 5 √ + 2 ÷

That computes $\phi = \frac{1+\sqrt{5}}{2}$. You put the numbers in first, and then say what to do with them. This might look obtuse, but it can make long expressions much easier to type in. Because there are no brackets, you don’t need to remember to close them. And if the same fragment appears more than once in the expression, you can just duplicate it on the stack, instead of typing it all out again. I’ve been using an RPN calculator for a few years now, and I get a warm mathmo feeling when I think about all the time it’s saved me.

But there are a couple of things that regularly trip me up. Once you’ve performed a calculation, you can see the result but you can’t see how you got there, so when you’ve got a couple of long numbers sitting on the stack and you can’t remember which is which, you just have to start again. And if you want to repeat a calculation but with a slightly different starting value, you’ve got no choice other than to type it all in again.

So, about halfway through my marathon of testing, with thumbs sore from tapping calculator buttons, I decided there was nothing for it except to make my own calculator. It would mainly be like RealCalc, but do something to solve those two problems. I headed straight to my favourite gaudily-decorated coding environment,, and set to work.

xkcd comic "Is it worth the time?" - a table showing how long you should spend shaving time off a task, depending on how often you do it
xkcd no. 1205, “Is it worth the time?”, licensed under a Creative Commons Attribution-NonCommercial 2.5 License.

In this case, I think the day or so I spent making the calculator was worthwhile. I’ve been using the new calculator to finish off my testing duties, and it feels much better to use: I can do calculations quicker, and I lose track of what I’m doing less often.

The first change I made was to display not just the results of calculations, but how they were obtained. When you press 1 2 +, a box is pushed to the stack with a 3 at the bottom, but also the 1 and 2 and a + symbol above it. This works for nested operations, too, but the ingredients are hidden by default until you tap the box. There’s an ‘undo’ button, which throws away an operation and puts the operands back on the stack – very handy when you tap the wrong operator!

Numbers that you type in directly are shown in blue. You can tap any blue number and enter a new value, and any operations it feeds into are recalculated. I’ve used that quite a few times to set up a formula, such as the quadratic equation, and each time I used it I just need to change the input values and the result appears at the bottom.

It’s also useful for factorising numbers: I can start by typing something like 10199 3 ÷, and I just replace the 3 with different prime numbers until the result is an integer.

After a while, I realised that there was another optimisation to be made: sometimes a formula uses the same variable more than once! For example, in the quadratic formula, $b$ appears twice:

\[ x = \frac{ -b \pm \sqrt{b^2 – 4ac}}{2a} \]

It would be nice if you could use the same number in more than one place, and have every instance update when you change the value of one of them. Every RPN calculator has a ‘copy’ button, to push another copy of the last item on the stack, so I just decided to make copies remember they’re linked. You can move copies of a number around, but when you overwrite one of them, all the others take the new value too.

In order for this to not make your head explode, I needed a way of showing which number boxes are linked together in this way. Asking the user for a name felt like overkill, and would interrupt your flow unnecessarily if you’re not planning on using the overwriting feature.

I came up with a nice solution: when you copy a number, it’s assigned an emoji. All copies of the number have the emoji stuck onto them, so you can quickly tell who they are.

This was way more useful than I expected! While I was testing a horrible question involving cancelling fractions with very large denominators, the linking feature really came into its own. By setting up $n/p$ and $d/p$, I could quickly find common factors of $n$ and $d$ just by changing the value of $p$ until both divisions produced integers. Bonza!

I’m quite happy with my calculator and I haven’t felt the need to go back to RealCalc yet. Hooray!

You can use it too: go to If you open it on your phone, you can add it to your home screen and it should act like a normal app, instead of a web page.

“Transposition”, a sliding block puzzle by Jacob Siehler

Start and finished states of a Transposition puzzle

This is just a quick post to tell you about a nice puzzle game I spotted on Mathstodon.

It’s called Transposition, and it’s a sliding block puzzle in the vein of the popular game Rush Hour. You’re given a grid that’s almost full of rectangular blocks, and you have to slide them around each other until the two coloured blocks have swapped places.

The puzzle was invented by mathematician Jacob Siehler, who says he used a computer search to generate a pool of puzzles, given the rules of the game. I took quite a while to solve all 5 “easy” puzzles – as with any logic puzzle, you need to play about for a while to get a feel for the mechanics. I hadn’t appreciated at first that the grey blocks don’t need to be in their starting places when you solve the board – only the coloured blocks need to in the right positions.

There are 26 puzzles at the moment, ranging from “easy” to “very hard”. Have a crack at it! I really enjoyed it.

Play: Transposition, by Jacob Siehler

Steckles on QI!

Katie Steckles talking to Sandi Toksvig on QI

Our Katie was on BBC Two last night! As part of the QI Christmas special, Katie told that old chestnut about infinitely many mathematicians walking into a bar.

Viewers in the UK can see the show on the iPlayer; Katie’s segment starts about 12 minutes in.

Byrne’s Euclid recreated for the web

This is astonishing. Designer and ‘data geek’ Nicholas Rougeux has painstakingly recreated all six books of Oliver Byrne’s Euclid on the web, following the original as closely as possible while adding links between propositions and even making the diagrams interactive.

Here are the mathematicians you nominated to go on the new £50 note

The Bank of England has released a preliminary list of names nominated by the public to appear on the new £50 note. I’ve done a bit of analysis on the list, and present here my findings.

To recap: the Bank asked for nominations satisfying the following conditions:

  • have contributed to the field of science
  • be real – so no fictional characters please
  • not be alive – Her Majesty the Queen is the only exception
  • have shaped thought, innovation, leadership or values in the UK
  • inspire people, not divide them

The released list consists of the names that were nominated in the first week, and belong to people who are real, deceased, and contributed to science ‘in any way’. They haven’t divulged the number of times each name was nominated, or the ineligible names.

Zeckendorf cup arithmetic

My 5-minute talk at the big MathsJam conference this weekend was about some stacking cups that my daughter is too young to appreciate. Here’s the really quick version, in just over a minute:

I gave the answer at MathsJam, but the title of this post contains a big hint that should get you there with a bit of googling.