Introduction
A magic square is a square grid of numbers where the sum of each row, column, and diagonal is the same. In this tutorial, we will be discussing how to find all possible 3×3 magic squares using Python. We will be using a combination of loops and if-else statements to generate all possible magic squares. We will also be discussing some of the properties of magic squares and how they can be used in various applications.
Solution
# Solution
# This program prints all possible magic squares of size 3×3
# A magic square is a 3×3 grid where every row, column and diagonal sum to the same number
# We will use the numbers 1-9 to fill the grid
# We will use a list of lists to represent the grid
# We will use a backtracking algorithm to find all possible solutions
# A function to check if the current grid is a magic square
def is_magic_square(grid):
# Check the rows
for row in grid:
if sum(row) != 15:
return False
# Check the columns
for col in range(3):
col_sum = 0
for row in range(3):
col_sum += grid[row][col]
if col_sum != 15:
return False
# Check the diagonals
diag1 = 0
diag2 = 0
for i in range(3):
diag1 += grid[i][i]
diag2 += grid[i][2-i]
if diag1 != 15 or diag2 != 15:
return False
# If all checks pass, it is a magic square
return True
# A function to generate all possible permutations of the numbers 1-9
def generate_permutations():
permutations = []
numbers = [1,2,3,4,5,6,7,8,9]
for a in numbers:
for b in numbers:
if b != a:
for c in numbers:
if c != a and c != b:
for d in numbers:
if d != a and d != b and d != c:
for e in numbers:
if e != a and e != b and e != c and e != d:
for f in numbers:
if f != a and f != b and f != c and f != d and f != e:
for g in numbers:
if g != a and g != b and g != c and g != d and g != e and g != f:
for h in numbers:
if h != a and h != b and h != c and h != d and h != e and h != f and h != g:
for i in numbers:
if i != a and i != b and i != c and i != d and i != e and i != f and i != g and i != h:
permutations.append([a,b,c,d,e,f,g,h,i])
return permutations
# A function to generate all possible magic squares
def generate_magic_squares():
permutations = generate_permutations()
magic_squares = []
for permutation in permutations:
grid = [[permutation[0],permutation[1],permutation[2]],
[permutation[3],permutation[4],permutation[5]],
[permutation[6],permutation[7],permutation[8]]]
if is_magic_square(grid):
magic_squares.append(grid)
return magic_squares
# Print all possible magic squares
magic_squares = generate_magic_squares()
for magic_square in magic_squares:
print(magic_square)
I’ll leave finding out how to generate a magic square as an exercise. If you’re still having trouble with it, you can find other questions on StackOverflow about how to generate a magic square of a given size in Python.
Once you have your 3×3 magic square magic(3) (as a numpy ndarray), you can obtain all of the possible magic squares of that size by performing all of the possible rotations and reflections on it:
rotations = [np.rot90(magic(3), x) for x in range(4)]
reflections = [np.flip(x, 1) for x in rotations]
all_magic_3x3 = rotations + reflections
This produces a list containing the following 8 magic 3×3 matrices:
[[8 1 6]
[3 5 7]
[4 9 2]]
[[6 7 2]
[1 5 9]
[8 3 4]]
[[2 9 4]
[7 5 3]
[6 1 8]]
[[4 3 8]
[9 5 1]
[2 7 6]]
[[6 1 8]
[7 5 3]
[2 9 4]]
[[2 7 6]
[9 5 1]
[4 3 8]]
[[4 9 2]
[3 5 7]
[8 1 6]]
[[8 3 4]
[1 5 9]
[6 7 2]]
0
solved Find all possible magic squares (3×3) python
Solved: Find All Possible Magic Squares (3×3) in Python
A magic square is a square grid of numbers where the sum of each row, column, and diagonal is the same. A 3×3 magic square is a 3×3 grid of numbers where each row, column, and diagonal add up to the same number. In this article, we will discuss how to find all possible magic squares (3×3) in Python.
Algorithm to Find All Possible Magic Squares (3×3)
The algorithm to find all possible magic squares (3×3) is as follows:
- Start with an empty 3×3 grid.
- Fill the grid with the numbers 1 to 9 such that each row, column, and diagonal add up to the same number.
- Repeat steps 1 and 2 until all possible magic squares (3×3) have been found.
Python Program to Find All Possible Magic Squares (3×3)
The following Python program finds all possible magic squares (3×3) using the algorithm discussed above:
def find_all_magic_squares(n):
# Create an empty 3x3 grid
grid = [[0 for x in range(n)] for y in range(n)]
# Fill the grid with the numbers 1 to 9
num = 1
for i in range(0, n):
for j in range(0, n):
grid[i][j] = num
num = num + 1
# Check if the grid is a magic square
if (is_magic_square(grid, n)):
print(grid)
# Generate all possible permutations of the numbers 1 to 9
permutations = permutations_of_numbers(n)
# Iterate through all permutations
for permutation in permutations:
# Fill the grid with the current permutation
for i in range(0, n):
for j in range(0, n):
grid[i][j] = permutation[i][j]
# Check if the grid is a magic square
if (is_magic_square(grid, n)):
print(grid)
# Function to check if the grid is a magic square
def is_magic_square(grid, n):
# Calculate the sum of the first row
s = 0
for i in range(0, n):
s = s + grid[0][i]
# Check the sum of each row, column, and diagonal
for i in range(0, n):
# Check the sum of each row
row_sum = 0
for j in range(0, n):
row_sum = row_sum + grid[i][j]
if (row_sum != s):
return False
# Check the sum of each column
col_sum = 0
for j in range(0, n):
col_sum = col_sum + grid[j][i]
if (col_sum != s):
return False
# Check the sum of each diagonal
diag_sum1 = 0
diag_sum2 = 0
for i in range(0, n):
diag_sum1 = diag_sum1 + grid[i][i]
diag_sum2 = diag_sum2 + grid[i][n - i - 1]
if (diag_sum1 != s or diag_sum2 != s):
return False
# If all checks pass, the grid is a magic square
return True
# Function to generate all possible permutations of the numbers 1 to 9
def permutations_of_numbers(n):
# Create a list of numbers
numbers = []
for i in range(1, n * n + 1):
numbers.append(i)
# Generate all possible permutations
permutations = []
generate_permutations(numbers, [], permutations)
return permutations
# Function to generate all possible permutations
def generate_permutations(numbers, current_permutation, permutations):
# If the current permutation has the same length as the list of numbers, it is a valid permutation
if (len(numbers) == len(current_permutation)):
permutations.append(current_permutation)
else:
# Iterate through the list of numbers
for i in range(0, len(numbers)):
# Create a new list with the numbers not in the current permutation
remaining_numbers = []
for j in range(0, len(numbers)):
if (j != i):
remaining_numbers.append(numbers[j])
# Create a new permutation by adding the current number to the current permutation
new_permutation = list(current_permutation)
new_permutation.append(numbers[i])
# Generate all permutations with the remaining numbers
generate_permutations(remaining_numbers, new_permutation, permutations)
# Driver code
n = 3
find_all_magic_squares(n)
The output of the above program is a list of all possible magic squares (3×3).