A few months ago I launched Enigmatic Code - a place to post programmatic solutions to New Scientist's Enigma Puzzles.
As well as solving contemporary puzzles I found that Google Books has archives of back issues of New Scientist dating back to the 70's and 80's, and in my search for early Enigma puzzles I came across Enigma 45: Six squares - harder. Which involves finding non-negative integers P, Q, R such that P+Q, P+R, Q+R, P-Q, P-R and Q-R are all perfect squares.
After a couple of false starts - the solution clearly wasn't as small as I'd hoped - I came up with an algorithm to attack the problem in a reasonable amount of time.
However, when I checked my solution against the one published in the following issue of New Scientist I saw that the answer I had got wasn't the same as the solution they were expecting. But we had been asked to minimise the sum P+Q+R and my answer had a smaller sum than the published solution.
Curiously in New Scientist #1190 it is noted that no correct entries were received for this puzzle. "Shame on you!", they said. So I sent an email with my solution to ask if I was the first correct entry, albeit 32 years too late.
In the current issue of New Scientist (#2853) they published a short item in the Feedback section about this story, and they have awarded me a £15 prize for solving the puzzle!
If you're thinking it's a little unfair to unleash 2011 computing power on a 1980's problem, I decided to put a more contemporary machine up to the challenge. The BBC Micro was launched at the end of 1981 (although I don't think I would have been using them until probably '82 or '83), and I did quite a lot of programming on them at school, so I stood a fair chance of being able to get a program together to attack the problem.
So, I downloaded BeebEm3, and managed to get a BBC BASIC program together to solve the problem. The hardest part was trying to input the program, as the keymap of the emulator doesn't match the layout of my laptop keyboard. Nevertheless I managed to get my program typed in and running. (See image above).
It took about 40 minutes to run, and the Emulator said it was running at a speed of around 17, which I'm assuming means it's running at 17x the speed of a real BBC Micro. If so, that would mean that a contemporary-ish home computer could have solved the problem in under 12 hours (in BASIC!). Admittedly it's not up to the 255ms runtime of my present day Python solution, but it would be a bit of a poor reflection on 32 years of computing development if it was. (Note: I found that the TIME command reports the elapsed time as it would be on a real machine (presumably it's derived directly from the CPU clock), so I augmented the program to report elapsed timing and it seems the program would take 5h2m to find the smallest solution and and additional 2h17m to find the published solution on an actual BBC Micro).
Note: It turns out this problem was posed by Italian mathematician Pietro Mengoli in 1674, and the smallest solution was discovered by Euler a century later, probably without the benefit of even a BBC Micro.
Note: I've now received my physical copy of New Scientist #2853 and I've reproduced the article (and the illustration) on my own blog: New Scientist: Enigma 45.
Thursday, February 23, 2012
Thursday, January 12, 2012
A Variation on the West Mendip Way
The walk goes from Wells Cathedral to Weston Bay at Uphill, just south of Weston-super-Mare. (Officially it goes the other way, but I think it might be more aesthetically pleasing to finish at the sea).
I've made some variations to the official route, these are:
- Avoid dropping down to the road at Draycott, instead cut through Draycott Sleights Nature Reserve.
- Walk along the cliff edge from Cheddar to Blackrock Gate.
- Go along Velvet Bottom and up to Beacon Batch (the highest point in the Mendips), and then rejoin the official route at Rowberrow Warren. (Although an even further extension could be added here to take in Dolebury Warren).
- A minor detour from the official route to visit the summit of Crook Peak.
The route could be split into 3 stages of around 10 miles each:
- Wells to Cheddar, 11 miles (17.4 km).
- Cheddar to Winscombe Hill, 10 miles (15.8 km).
- Winscombe Hill to Uphill, 10 miles (16.0 km).
The last stage is trickier, you would need to get a bus between Winscombe Hill and Weston-super-Mare, and then another one between Weston-super-Mare and Uphil (Beach End Road), which will take over an hour. It may be easier to arrange for someone to meet you at the beach for a celebratory ice-cream.
Or it could be done in 2 stages of around 16 miles each, by breaking the route at the summit of Beacon Batch. That way you get to do the highest point in the Mendips on both days.
- Wells to Beacon Batch, 16 miles (25.8 km)
- Beacon Batch to Uphill, 15 miles (23.4 km)
My plan is to do this when there is a fine spell of weather in the spring.
Tuesday, January 10, 2012
Enigmatic Python
While attempting to find a programmatic solution to Enigma 1602 (which really is much easier to do with pencil and paper) for the Enigmatic Code Blog, I briefly toyed with the idea of writing my own code to solve sets of linear simultaneous equations.
Then I discovered the rather marvellous SymPy library, which can do that and much more, and made for a very neat solution.
I've really only scratched the surface of this module, but I expect to use it more in the future.
Along with other Python modules that aid in the writing Enigma solutions: unlimited precision integers, set, itertools, collections, fractions, probably lots of other standard modules I have yet to discover, and, of course, my own set of useful routines.
And finally, to squeeze that bit extra out your Python programs you can always try using PyPy, which is a Python interpreter, written in Python. It includes a JIT compiler and often ends up running faster than the standard CPython interpreter.
I tried it on my code for Enigma 1653 - one of the trickier ones - and I got the following runtimes:
CPython 2.7.1: 2m01s
CPython 3.2: 1m52s
PyPy: 0m10s
Then I discovered the rather marvellous SymPy library, which can do that and much more, and made for a very neat solution.
I've really only scratched the surface of this module, but I expect to use it more in the future.
Along with other Python modules that aid in the writing Enigma solutions: unlimited precision integers, set, itertools, collections, fractions, probably lots of other standard modules I have yet to discover, and, of course, my own set of useful routines.
And finally, to squeeze that bit extra out your Python programs you can always try using PyPy, which is a Python interpreter, written in Python. It includes a JIT compiler and often ends up running faster than the standard CPython interpreter.
I tried it on my code for Enigma 1653 - one of the trickier ones - and I got the following runtimes:
CPython 2.7.1: 2m01s
CPython 3.2: 1m52s
PyPy: 0m10s
Thursday, December 1, 2011
Enigmatic Code
For the past few years I've been treating the weekly Enigma puzzles in New Scientist magazine as a programming challenge. I started writing programs to solve the puzzles in Perl (and occasionally C, when speed was all important), but more recently I've been coding up solutions in Python.
Now, I've decided to set up a blog to share programmatic solutions to Enigma puzzles. I opted for a WordPress blog, as I couldn't get Blogger comments to accept pre-formatted code. Whereas WordPress comments let you use HTML <pre> tags.
I aim to add new puzzles (and solutions) as they are published (usually on a Thursday in the UK), and also retroactively add old puzzles that I have coded solutions for, or found from the New Scientist archives on Google Books, as time allows.
If you'd like to join in, please visit the Enigmatic Code blog.
Now, I've decided to set up a blog to share programmatic solutions to Enigma puzzles. I opted for a WordPress blog, as I couldn't get Blogger comments to accept pre-formatted code. Whereas WordPress comments let you use HTML <pre> tags.
I aim to add new puzzles (and solutions) as they are published (usually on a Thursday in the UK), and also retroactively add old puzzles that I have coded solutions for, or found from the New Scientist archives on Google Books, as time allows.
If you'd like to join in, please visit the Enigmatic Code blog.
Monday, September 26, 2011
Dice Emulator
![]() |
| The Merry Game of Floundering |
All the pieces were there with the exception of one of the dice (it needs to two). So rather than search for another die I did what any self-respecting programmer would do, and knocked up a dice emulator in Python.
Thanks to the pygame library and Unicode characters U+2680 - U+2685 I was able to get a program together quickly which we then triggered using the Apple Remote (using Remote Buddy's Virtual Mouse behaviour). Simple as that!
![]() |
| dice.py |
- Download dice.py
[*] While searching for more information on the game I found someone selling a set on eBay as "The Messy Game of Floundering".
Monday, August 8, 2011
Gotta Catch 'Em All
![]() |
| "Fade to Black" |
We're now half way through their visit and I've managed to collect 53 out of 60. You can see my efforts in my Facebook photo gallery. (Facebook registration not required).
So I'm missing 7:
- Gorilla 5 ("Sky Gorilla"), Anchor Square, Bristol Harbour.
- Gorilla 9 ("Willard"), A Marriott Hotel Lobby, Bristol.
- Gorilla 25 ("Elvis"), Bristol Bus Station.
- Gorilla 26 ("Hubert"), Holiday Inn, Bristol.
- Gorilla 29 ("Ape Scape"), Bristol Airport.
- Gorilla 40 ("Winston"), Avon Gorge Hotel, Bristol.
- Gorilla 60 ("Guerilla Tourist"), Birmingham(!) Coach Station.
I foolishly thought that the Marriott Hotel City Centre would be the one by The Centre, but it turns out that that's the Marriott Royal Hotel, and the City Centre one is at Broadmead (I suppose that's "City Centre" when compared to Bristol Airport or somewhere). So I ended up at the wrong hotel. (Although #9 is in the Broadmead one from today).
I don't usually happen to pass through Bristol Airport on my day-to-day travels, but I might be able to get out there sometime in the next month to collect #29.
But Birmingham seems a bit of a way to go. Although I know the closer I get to completing the set the more I will feel compelled to make a trip up to Brum.
But there maybe a solution. Apparently Gorilla #61 ("Doris") is currently floating around Bristol Harbour on the prow of The Matthew, and was organised too late to make it on to the official maps. So there may be hope of sneakily collecting 60 Gorillas after all.
Update 2011-09-04: I'm now on 59/61 - only #29 and #60 to get!
Update 2011-09-06: I went to Bristol Airport and collected #29 today. 60/61.
Update 2011-09-25: I finally collected "Guerilla Tourist" today, now that he's outside Bristol Zoo and no longer in Birmingham. 61/61.
Monday, May 23, 2011
Apocalypse - Not!
So, the Rapture didn't happen on Saturday 21st May 2011. Still, it's not the end of world, eh?
But it was the end of my venerable Compaq Evo N610c that is so good IMDb didn't want it back when I left. The hard disk failed a mere 7½ years after being replaced. Oh well, it's been doing a good job for the last (almost) two years as a firewall machine with Ubuntu installed and a PCMCIA network card.
I recently retired it from firewall duty (as it's on-board network connection seemed to be playing up, although the problem disappeared once I'd retired it), so it was left acting as a local DNS server and a WebDAV server (for sharing iCal calendars). Both of which I would rather the Time Capsule would take care of - but it doesn't.
I've managed to resurrect it for now using the 160GB drive that I removed from my MacBook when I expanded that, attached to the Evo using a cheap USB enclosure, put another Ubuntu install on it and installed dnsmasq and got lighttpd running as a WebDAV server, and everything seems dandy again.
But I can't help thinking I'd be better off with a proper low power server to do this kind of thing.
But it was the end of my venerable Compaq Evo N610c that is so good IMDb didn't want it back when I left. The hard disk failed a mere 7½ years after being replaced. Oh well, it's been doing a good job for the last (almost) two years as a firewall machine with Ubuntu installed and a PCMCIA network card.
I recently retired it from firewall duty (as it's on-board network connection seemed to be playing up, although the problem disappeared once I'd retired it), so it was left acting as a local DNS server and a WebDAV server (for sharing iCal calendars). Both of which I would rather the Time Capsule would take care of - but it doesn't.
I've managed to resurrect it for now using the 160GB drive that I removed from my MacBook when I expanded that, attached to the Evo using a cheap USB enclosure, put another Ubuntu install on it and installed dnsmasq and got lighttpd running as a WebDAV server, and everything seems dandy again.
But I can't help thinking I'd be better off with a proper low power server to do this kind of thing.
Subscribe to:
Posts (Atom)





