May 2016 Archives

Brutally Solving a Logic Puzzle with Perl 6

Every now and then, I enjoy solving logic puzzles (or attempting to). Recently I came across this one:

+----+----+----+
|    |  3 | 17 |
+----+----+----+
|  5 |    |    |
+----+----+----+
| 13 |    |  7 |
+----+----+----+

There are five prime numbers in a 3x3 grid, and the goal is to fill in the empty cells with four other prime numbers, so that the sum of every row, every column, and both diagonals is also a prime number, less than 100. Each number can only be used once, and this applies to the numbers in the grid as well as to all the sums. Lastly, the sum of all these numbers must be a prime number as well (greater than 100, obviously).

I took me quite a while to find a solution — but when I finally did, I had not one, but (at least) two solutions. The puzzle description didn’t mention anything about multiple solutions, so I thought I made a mistake along the way. However, having triple-checked all the math, I couldn’t find any errors. I decided it was time to use the force (I couldn’t miss the opportunity to use this phrase, considering the date when I’m posting this) — namely, the brute force.

About Michał Wojciechowski

user-pic I blog about Perl.