### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,115)
- misc (2)
- project euler (2)
- puzzle (29)
- site news (43)
- tantalizer (29)
- teaser (3)

### Site Stats

- 166,357 hits

Programming Enigma Puzzles

2 January 2013

Posted by on **From New Scientist #2898, 5th January 2013** [link]

The grid shown above is a cross-figure puzzle with one digit given as a starting point. All 11 answers are prime numbers, none of them starts with a zero, and adding the individual figures in each column gives the same sum.

What are the two numbers in the shaded regions of the grid?

[enigma1730]

Advertisements

%d bloggers like this:

This Python program is a direct implementation of the problem statement, and finds the solution in 41ms. I’m sure I can make a prettier version though.

Solution:The two shaded numbers are 41 and 571.Here is the completed grid.

Here’s a slightly cleverer (and also slightly slower) Python program that gets the same solution. It runs in 69ms.

You can also use the generic cross figure solver given in my solution for

Enigma 1760to solve this problem. But it isn’t quick – it takes 3m26s under PyPy – because the condition that the columns sum to the same value is not checked until the the grid is filled out. But here’s the code if you want to see it.Numbering the grid as for a crossword, as shown in this diagram.

The digits of all columns add to the same sum, which must be less than 10 due to the middle digit of 6 across.

So 1 down must be 23 (29 would make the common sum more than 1 digit).

7 down must be 11, (if it were 13 or 31, the common digit sum would be 9, so the 3 digit numbers would not be prime)

So the common column digit sum is 7, which is the middle digit of 6 across.

3 across must be 31 (37 would be too large for 4 down)

Some code for the rest:

Each square contains a single digit, so we can use the alphametic solver (

SubstitutedExpression()) from theenigma.pylibrary to solve this puzzle, by expressing the constraints as Python expressions.Here is a run-file that executes in 120ms.

With analysis we can provide additional constraints. For example: no prime can start or end with a zero, so none of the letters (except

H) can be zero, but all the columns must sum toH, so it can’t be zero either, hence we could add the parameter--digits="1-9". Also none of the digits that end a prime (with more than 1 digit) can be 2, 4, 5, 6, 8, so we could also add the parameter--invalid="24568,CDFJLMNPQ", although these provide only a modest reduction in overall execution time.