Use the ten natural digits 0,1,2,3,4,5,6,7,8,9 each occurring only twice
and fill up the the 20 empty boxes in the figure shown below such that the resulting 13 numbers (7 vertical, 6 horizontal) are each multiple of 7 and their product is the highest of all such arrangements.
+---+
| |
+---+---+---+
| | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+---+---+
| | | | | | | |
+---+---+---+---+---+---+---+
| | | | | |
+---+---+ +---+---+
Note: As the figure suggests, the bottom row should be considered as two 2-digit numbers rather than a single 4-digit number.
DECLARE SUB addOn (nbr#, offset#)
DEFDBL A-Z
CLEAR , , 25000
OPEN "7 mult muse.txt" FOR OUTPUT AS #2
DIM SHARED gr(5, 7), begend(13, 4), avail AS STRING, maxnbr, overprod, maxoverprod
DATA 1,4,1,4
DATA 2,3,2,5
DATA 1,4,4,4
DATA 3,2,3,6
DATA 2,3,4,3
DATA 2,5,4,5
DATA 4,1,4,7
DATA 3,2,5,2
DATA 3,6,5,6
DATA 4,1,5,1
DATA 4,7,5,7
DATA 5,1,5,2
DATA 5,6,5,7
FOR i = 1 TO 13
READ r0, c0, r1, c1
IF i > 1 THEN
begend(i, 1) = r0
begend(i, 2) = c0
begend(i, 3) = r1
begend(i, 4) = c1
dr = SGN(r1 - r0): dc = SGN(c1 - c0)
r = r0: c = c0
WHILE r <= r1 AND c <= c1
gr(r, c) = -1
r = r + dr: c = c + dc
WEND
END IF
NEXT
avail = "0011223344556678899"
overprod = 7
gr(1, 4) = 7
addOn 2, 0
CLOSE
SUB addOn (nbr, offset)
r0 = begend(nbr, 1)
c0 = begend(nbr, 2)
r1 = begend(nbr, 3)
c1 = begend(nbr, 4)
dr = SGN(r1 - r0): dc = SGN(c1 - c0)
r = r0 + dr * offset: c = c0 + dc * offset
IF gr(r, c) = -1 THEN
IF offset = 0 THEN st = 1: ELSE st = 0
FOR v = st TO 9
vchar$ = LTRIM$(STR$(v))
ix = INSTR(avail, vchar$)
IF ix > 0 THEN
MID$(avail, ix, 1) = " "
gr(r, c) = v
IF r = r1 AND c = c1 THEN
tot = 0
rx = r0: cx = c0
WHILE rx <= r1 AND cx <= c1
tot = 10 * tot + gr(rx, cx)
rx = rx + dr: cx = cx + dc
WEND
IF tot MOD 7 = 0 THEN
savoverprod = overprod
overprod = overprod * tot
IF nbr = 13 THEN
IF overprod >= maxoverprod THEN
maxoverprod = overprod
FOR rx = 1 TO 5
FOR cx = 1 TO 7
PRINT USING "##"; gr(rx, cx);
PRINT #2, USING "##"; gr(rx, cx);
NEXT
PRINT: PRINT #2,
NEXT
PRINT overprod
PRINT #2, overprod
END IF
ELSE
addOn nbr + 1, 0
END IF
overprod = savoverprod
END IF
ELSE
addOn nbr, offset + 1
END IF
gr(r, c) = -1
MID$(avail, ix, 1) = vchar$
END IF
NEXT
ELSE
IF r = r1 AND c = c1 THEN
tot = 0
rx = r0: cx = c0
WHILE rx <= r1 AND cx <= c1
tot = 10 * tot + gr(rx, cx)
rx = rx + dr: cx = cx + dc
WEND
IF tot MOD 7 = 0 THEN
savoverprod = overprod
overprod = overprod * tot
IF nbr = 13 THEN
IF overprod >= maxoverprod THEN
maxoverprod = overprod
FOR rx = 1 TO 5
FOR cx = 1 TO 7
PRINT USING "##"; gr(rx, cx);
PRINT #2, USING "##"; gr(rx, cx);
NEXT
PRINT: PRINT #2,
NEXT
PRINT overprod
PRINT #2, overprod
END IF
ELSE
addOn nbr + 1, 0
END IF
overprod = savoverprod
END IF
ELSE
addOn nbr, offset + 1
END IF
END IF
END SUB
finds
0 0 0 7 0 0 0
0 0 9 3 8 0 0
0 5 2 0 1 7 0
9 0 4 1 2 5 6
8 4 0 0 0 6 3
2.105833777803707D+38
reformatted:
7
9 3 8
5 2 0 1 7
9 0 4 1 2 5 6
8 4 6 3
with the product being 2.1058337778037... x 10^38
Using UBASIC to find the exact product:
list
10 T=7*938*52017*9041256*84*63
20 T=T*98*504*924*7301*812*756*63
30 print T
OK
run
210583377780370729221760220343879401472
OK
Edited on January 17, 2014, 12:50 pm
|
Posted by Charlie
on 2014-01-17 12:49:46 |