### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

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

### Site Stats

- 168,353 hits

Programming Enigma Puzzles

3 April 2012

Posted by on **From New Scientist #2859, 7th April 2012** [link]

Clever logic should enable you to find the nine-figure number that I have in mind. It consists of the digits 1 to 9 in some order, and in the number each digit is next to another that differs from it by one.

In just one case a digit has both neighbours differing from it by one. Furthermore, the solution is exactly divisible by more than three-quarters of the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and 12.

What is the nine-figure number?

[enigma1692]

Advertisements

%d bloggers like this:

Here’s my initial solution in Python. It runs in 316ms.

Solution:The number is 436578912.Here’s a faster program that uses recursion. It runs in 54ms.

@geoff: When I execute this model with the

mzn-gecode -asolver it finds 9 solutions (one of which is the correct answer).There’s a problem with the

constraintat line 15. If each digit is next to a digit that differs from it by one, then we know that A and B must differ by one, and also H and I must differ by one. So we need to check those and the neighbours of C, D, E, F, and G. So a correct constraint would be:Also the

constraintat line 25 checks if any of the digits has two neighbours that differ from it by one, whereas we need to check that there isexactlyone of the digits that satisfies this condition. FortunatelyMiniZinchas theexactly()predicate that can be used to check this:With these changes I get a model which produces only one solution, which is the answer to the problem.

On my machine the

mzn-gecodesolver was the only one that gave an answer in a reasonable time using this model.Jim,

Point taken. I did not spot this problem – it only gave one correct solution when I ran it on the Geocode solver, without looking for multiple solutions.