Given a 3x3 square fill in the numbers 1-9, using each number once, so that each row, column and diagonal add up to the same number. A diagonal must be as long and wide as the square to be counted. For instance, on a standard number pad the only valid diagonals are 1, 5, 9 and 3, 5, 7.
To take this a step further, write a program that can solve this for an NxN square (e.g.: for a 5x5 square use the numbers 1-25).
Comments:
Carlos - 10 years, 9 months ago
I had solved this problem before, it only works for odd numbers, for even numbers i believe you need something to remember, used numbers. It's a bit more complicated and unfortnately i don't have the time today. Here's the solution for odd numbers:
reply permalink
Johnny - 10 years, 9 months ago
Not sure how to program but the answer for a 3x3 square is
[2,7,6] [9,5,1] [4,3,8]
reply permalink
Q - 10 years, 9 months ago
So I only have a few minutes but some basics would be to gather the input of box size, figure out how many total squares, and the sum that each row/col/diag should be.
squaresize = n
squarecount = squaresize2
rowcoltotal = ((squarecount(squarecount+1))/2)/squaresize
From here you can rotate through the lowest available number and highest numbers plus a middle number to get the possible rows/columns then rotate their positions to find the right match up plus diags. You would also have to watch out for some boxes that would not be able to be solved, such as a 2x2 box.
reply permalink
Pyro - 10 years, 8 months ago
So i have this solution in python for n=3, but it dosen't work for n>3. The number of possible permutations are too big. Working on it :D
reply permalink
Anonymous - 10 years, 6 months ago
reply permalink