Maths seems to be one of those things I’ve always been quite good at. I have no illusions in that I know there are plenty of people better, better enough that I wouldn’t be worth a second thought, but I held my own well into university level maths. One thing I’ve always noticed is that people in the general public seem to see programming and maths as very strongly connected. I know, for example, people who’ve been surprised at someone going into computer science (and doing quite well as I understand) when they took soft maths (business maths, but it may be named differently elsewhere) in high school.

Now I don’t really believe there’s a really strong requirement for maths in most programming courses. Obviously if you head for real computer science you’ll need some, and some maths skills will help you pick up iteration and especially functional programming concepts, but I don’t see it as a prerequisite. That said, however, I do believe there are some quite similar skills required in modelling problems. You may remember the sort of problems I’m talking about from school - ‘A train leaves city X travelling to city Y at 60 km/h at 7:15 pm while another leaves Y for X travelling at 70 km/h at 8pm. Given that X and Y are 200 km apart, at what time do the trains meet.’ The trick with that sort of problem is simply turning all the words into equations, which then rearrange quite simply.

So, anyway, I’m not going to solve that one (I just made up the numbers, so I doubt the answer is nice) but I will point to the problem I actually want to talk about - Grant Holiday’s A five digit number in code. The specific problem is reproduced below.

Find a five digit number that when you place a 9 at the start of it, is equal to four times the original number when the 9 is placed at the end.

Now, what interests me is that Grant says he couldn’t work out the maths but was able to write a program to spin through and try each 5 digit number until it tested them all. Fo me, the leap in that problem is understanding how to model the ‘adding a 9 to the start or end of a number. If we know that the number if 5 digits, you can assume it’s something like ABCDE, so we want to find 9ABCDE, which is the same as 900000 + ABCDE. Similarly, Adding a 9 to the end takes us from ABCDE to ABCDE9, or (ABCDE * 10) + 9.

I can understand not making that jump, mostly because maths doesn’t usually spend much time think about X digit numbers, but once you do the problem becomes pretty simple algebraic manipulation. My question, however, is how can you code a solution to that problem without making that jump? The only other way I can think of is to convert each 5 digit number to a string, do the concatenation with a 9 at each end, then convert it back to an integer, but I don’t think anyone would really buy that as a nice solution.

Anyway, I was just surpised here that is was easier to write a program to solve this than to do the maths. Perhaps the algebraic manipulation is tougher for some people than I would have thought (admittedly I half stuffed it up the first time, but caught myself when I got a negative number). I really must do more maths puzzles though, since this one was quite fun.