# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1463: D for divisibility

From New Scientist #2624, 6th October 2007

I have placed each of the digits 1 to 9 in a 3 by 3 grid, with one digit in each little box. So I can read off 12 three-figure numbers; three from left to right across the rows, three from right to left, three from top to bottom in the columns, and three from bottom to top.

Now for each digit, D say, I have counted how many of those 12 numbers are exactly divisible by D. In each case the answer is itself divisible by D.

What are the highest and lowest of the 12 three-figure numbers?

[enigma1463]

### One response to “Enigma 1463: D for divisibility”

1. Jim Randell 10 February 2013 at 3:01 pm

This Python program runs in 730ms.

```from itertools import combinations, permutations
from enigma import irange, nconcat, printf

# A B C
# D E F
# G H I

# to avoid symmetrical repeats we don't permute A, C, G and B, D
ds = set(irange(1, 9))
for (A, C, G) in combinations(ds, 3):
ds2 = ds.difference((A, C, G))
for (B, D) in combinations(ds2, 2):
ds3 = ds2.difference((B, D))
for (E, F, H, I) in permutations(ds3):
# construct the 3-digit numbers
ns = list(nconcat(x) for x in (
(A, B, C), (D, E, F), (G, H, I),
(C, B, A), (F, E, D), (I, H, G),
(A, D, G), (B, E, H), (C, F, I),
(G, D, A), (H, E, B), (I, F, C),
))
# the count of 3-digits numbers divisible by d should itself
# be divisible by d, for each digit d
if all(sum(1 for n in ns if n % d == 0) % d == 0 for d in ds):
(mx, mn) = (max(ns), min(ns))
printf("max={mx} min={mn} [{A} {B} {C} / {D} {E} {F} / {G} {H} {I}]")
```

Solution: The highest number is 971. The lowest number is 164.

This site uses Akismet to reduce spam. Learn how your comment data is processed.