The 2-, 3- and 4-digit numbers that are divisible by each of their digits are:
12 124 1236 3915
15 126 1248 3924
24 128 1296 4128
36 132 1326 4172
48 135 1362 4236
162 1368 4368
168 1395 4392
175 1632 4632
184 1692 4872
216 1764 4896
248 1824 4932
264 1926 4968
312 1935 6132
315 1962 6192
324 2136 6312
384 2184 6324
396 2196 6384
412 2316 6432
432 2364 6912
612 2436 6984
624 2916 8136
648 3126 8496
672 3162 8736
728 3168 9126
735 3195 9135
784 3216 9162
816 3264 9216
824 3276 9315
864 3492 9324
936 3612 9432
3624 9612
3648 9648
3816 9864
3864
The solution is:
48 + 672 + 3195 = 3915
DIM num(2 TO 4, 80)
col = 1: row = 1: pLen = 2
CLS
FOR i = 12 TO 9876
n$ = LTRIM$(STR$(i))
good = 1
FOR j = 1 TO LEN(n$)
d$ = MID$(n$, j, 1)
IF INSTR(MID$(n$, j + 1), d$) THEN good = 0: EXIT FOR
IF d$ = "0" THEN good = 0: EXIT FOR
IF i MOD VAL(d$) <> 0 THEN good = 0: EXIT FOR
NEXT
IF good THEN
pCt = pCt + 1
IF LEN(n$) > pLen THEN col = col + 1: row = 1: pCt = 1
num(LEN(n$), pCt) = i
howMany(LEN(n$)) = pCt
LOCATE row, col * 10 - 9
PRINT i; : ct = ct + 1
row = row + 1
IF row > 34 THEN row = 1: col = col + 1
pLen = LEN(n$)
END IF
NEXT
PRINT ct
LOCATE 38, 1
FOR a = 1 TO howMany(2)
FOR b = 1 TO howMany(3)
FOR c = 1 TO howMany(4)
b$ = LTRIM$(STR$(num(2, a))) + LTRIM$(STR$(num(3, b))) + LTRIM$(STR$(num(4, c)))
good = 1
FOR i = 1 TO LEN(b$) - 1
IF INSTR(MID$(b$, i + 1), MID$(b$, i, 1)) THEN good = 0: EXIT FOR
NEXT
IF good THEN
dVal = num(2, a) + num(3, b) + num(4, c)
good = 0
FOR i = 1 TO howMany(4)
IF dVal = num(4, i) THEN good = 1: EXIT FOR
NEXT
IF good THEN PRINT num(2, a); num(3, b); num(4, c); dVal
END IF
NEXT
NEXT
NEXT
From New Scientist, 19 August 2006, Enigma number 1405. |