0

A parker square here refers to a 9x9 grid containing integers. After squaring all the integers, the total of each row, each column and each diagonal is the same. (All cells in the grid cannot contain the same number).

https://www.youtube.com/watch?v=aOT_bG-vWyg A 5 minute video on the topic

I'm trying to approach this problem but I need some help optimizing my current solution, which is pretty amateur.

First, I named all the cells in the 9x9 grid as variables a-i Then, I calculate the sum of the squares of each column , row and diagonal separately and check if all of them are equal once.

My code is here below in Python, any suggestions?

for a in range(1,10):
    for b in range(1,10):
        for c in range(1,10):
            for d in range(1,10):
                for e in range(1,10):
                    for f in range(1,10):
                        for g in range(1,10):
                            for h in range(1,10):
                                for i in range(1,10):
                                     j=a**2+b**2+c**2
                                     k=d**2+e**2+f**2
                                     l=g**2+h**2+i**2
                                     m=a**2+d**2+g**2
                                     n=b**2+e**2+h**2
                                     o=c**2+f**2+i**2
                                     p=a**2+e**2+i**2
                                     q=c**2+e**2+g**2
                                     if j==k and k==l and l==m and m==n and n==o and o==p and p==q:
                                         print(a)
                                         print(b)
                                         print(c)
                                         print(d)
                                         print(e)
                                         print(f)
                                         print(g)
                                         print(h)
                                         print(i)
                                     break
Nazaal
  • 395
  • 1
  • 3
  • 10
  • It's called a magic square. The parker square was just the specific one in the video. – OneCricketeer Apr 18 '16 at 20:02
  • You can use the [cartesian product](https://docs.python.org/2/library/itertools.html#itertools.product) instead of those nested loops, e.g. `product(range(1, 10), repeat=9)` – Peter Wood Apr 18 '16 at 20:10
  • Here is a [Java magic square tester](http://stackoverflow.com/a/33625476/2308683) answer that I wrote. The initial list would need changed to square all the numbers. – OneCricketeer Apr 18 '16 at 20:14
  • @PeterWood There can't be duplicate numbers, so it should be a permutation. – OneCricketeer Apr 18 '16 at 20:17

2 Answers2

0

Does your code work? (That might be your first order of business.)

A few suggestions...

1) Do you think that this might be similar to a magic square? There are ways to construct those, such as https://plus.maths.org/content/anything-square-magic-squares-sudoku, that may be of help.

2) Since you can't contain the same number twice, maybe you could exit the loop if two numbers were the same. This is a faster operation than squaring and summing.

3) You could think about a reduction of strength. If the numbers in two row add up to the same, does the sum of their squares also add up to the same?

4) Rather than going brute force through all 999,999,999 combinations, could you permute the vector [1,2,3,4,5,6,7,8,9]? Here's a good SO question about python permutations: How to generate all permutations of a list in Python

Community
  • 1
  • 1
rajah9
  • 11,645
  • 5
  • 44
  • 57
0

As per rajah9's answer, confirming whether your code works should indeed have been the first order of business. From the look of things, your code will not work because it never checks for the critical condition:

"All cells in the grid cannot contain the same number."

To rectify this, the line

if j==k and k==l and l==m and m==n and n==o and o==p and p==q:



ought to be extended to

if j==k and k==l and l==m and m==n and n==o and o==p and p==q: and a!=b and a!=c and a!=d
# and so on until all the 9 cells have been catered for
Stephen Muga
  • 183
  • 8