total number of magic square from 9 numbers

Posted by Peeyush on Stack Overflow See other posts from Stack Overflow or by Peeyush
Published on 2011-11-22T09:48:56Z Indexed on 2011/11/22 9:51 UTC
Read the original article Hit count: 289

Filed under:
|
|

9 numbers need to be arranged in a magic number square.
A magic number square is a square of numbers that is arranged such that every row and column has the same sum.(condition for diagonal has been relaxed) For example:

1 2 3
3 2 1
2 2 2

How do we calculate total number of distinct magic square from 9 numbers.
Two magic number squares are distinct if they differ in value at one or more positions. For example, there is only one magic number square that can be made of 9 instances of the same number. e.g. for these 9 numbers { 4, 4, 4, 4, 4, 4, 4, 4, 4 }, answer should be 1.
Also the complexity should be optimal.
Do we need to iterate through all the permutations , discarding if a[0]+a[1]+a[2] %3!=0 such combinations ? moreover how do we remove duplicate magic square?

© Stack Overflow or respective owner

Related posts about c

    Related posts about algorithm