Magic Squares In Grid
To count the number of 3 x 3 magic squares inside the given grid, we can iterate through all possible 3 x 3 subgrids and check if each subgrid is a magic square. A 3 x 3 grid is a magic square if it meets the following criteria:
- All the numbers in the grid are distinct and lie between 1 and 9.
- The sum of the numbers in each row, each column, and both diagonals is the same.
Here’s the code:
|
|
This code iterates through all possible 3 x 3 subgrids and uses the is_magic
function to check if each subgrid is a magic square. The function verifies the constraints for a magic square and returns True
or False
. The main function counts the number of magic squares and returns the final count.