Could anyone instruct me about the Python code for magic square 3*3, I got some instructions but still made no dent in program. Could anybody send me the code?
O(∩_∩)O Thanks!
here's the instrctions:
magic3
# In recreational mathematics, a magic square of order n
# is an arrangement of n^2 numbers, usually distinct
# integers, in a square, such that n numbers in all rows,
# all columns, and both diagonals sum to the same constant
# (this constant is the magic number).
# A normal magic square contains the integers from 1 to n^2.
# (From Wikipedia, the free encyclopedia.)

# Implement the function to find the arrangement of numbers
# for n=3. The returned arrangement must be a TUPLE of numbers,
# where the first 3 elements are for the first row; the
# second 3 for the second row, and the last 3 for the last row.
# For example, (1,2,3,4,5,6,7,8,9) corresponds to
# the following square:
# 1 2 3
# 4 5 6
# 7 8 9

# Hint: the magic number is always: n*(1+n*n)//2
# Hint: use itertools.permutations for brute force.
# Note: answer is not all, but how your code
# comes up with the answer.
O(∩_∩)O~

Recommended Answers

All 2 Replies

I think the question consist of 2 parts:
(1) Think about how to generate all the possible combinations of the magic squares.
(2) Check if all the rows and cols return the same magic constant for each combination
generated in step 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.