**From New Scientist #2918, 25th May 2013** [link]

Using the number grid shown (A) it is possible to generate nine-digit numbers in the following way: Start on any square and then move horizontally, vertically or diagonally to an adjacent square that hasn’t already been visited. Repeat until all nine squares have been visited. For example, the path shown in diagram B generates the number 235968741.

I have listed all the numbers that can be generated in this way, and that end in a certain digit. Some of these are divisible by four (and only four) of the numbers 1 to 9. The rest are divisible by five (and only five) of the numbers 1 to 9.

How many numbers are there in my list?

[enigma1750]

### Like this:

Like Loading...

This Python program runs in 58ms. It uses the [[

`yield from ...`

]] construct so without modification it needs to be run under Python 3.Solution:There are 32 numbers in the list.A rather fiddly solution:

Pretty similar to Jim’s version:

All nine-digit numbers formed from digits 1 to 9 are divisible by 1,3,9.

If the ending digit of the answer is even, then all the candidate numbers are divisble by 1,2,3,6,9, so no candidates are divisible by exactly four of the numbers 1 to 9.

If the ending digit is one of 1,3,7,9, then the candidate numbers can only be divisible by 1,3,7,9, so no candidates are divisible by exactly five of the numbers 1 to 9.

The ending digit therefore has to be 5.

From there, it is not hard to enumerate by hand the patterns of sequences that end at 5.

The ending digit can only be 5 as it is only then searched number can only be divisible by 4 and 5 odd digits exactly as no even number can be a divisor which easily takes us to the solution by a very few number of sets.

I changed that part with the above code, only one is enough, missed that…