If I place all the numbers from 1 to 9 in a 3x3 grid and I add the products of each row and column, then what is the minimal sum?
For example, the sum is 450 in the picture below.
Asked
Active
Viewed 250 times
0
1 Answers
3
import math
import itertools
def prod(l):
r1 = l[0]*l[1]*l[2]
r2 = l[3]*l[4]*l[5]
r3 = l[6]*l[7]*l[8]
c1 = l[0]*l[3]*l[6]
c2 = l[1]*l[4]*l[7]
c3 = l[2]*l[5]*l[8]
return (r1+r2+r3+c1+c2+c3)
x = itertools.permutations([1,2,3,4,5,6,7,8,9])
minsum = math.inf
maxsum = 0
minlist = []
maxlist = []
for i in x:
a = prod(i)
if a < minsum:
minlist = i
minsum = a
if a > maxsum:
maxlist = i
maxsum = a
print(minsum)
print(maxsum)
print(minlist)
print(maxlist)`
Not extremely enlightening but I ran this python code to find an answer of $436$, (and a max of $947$).
J P
- 759
-
What was the order? – healynr May 05 '20 at 21:50
-
1$\begin{bmatrix}1 & 6 & 9\7 & 3 & 4 \ 8 & 5 & 2\end{bmatrix}$, however keep in mind that any permutation of the rows and columns will be the same. – J P May 05 '20 at 21:52