https://github.com/ksprugevics/magic-square
Some simple algorithm implementations for solving (ordinary) magic squares using NumPy
https://github.com/ksprugevics/magic-square
algorithms mathematics numpy python
Last synced: 2 months ago
JSON representation
Some simple algorithm implementations for solving (ordinary) magic squares using NumPy
- Host: GitHub
- URL: https://github.com/ksprugevics/magic-square
- Owner: ksprugevics
- Created: 2023-08-19T07:45:24.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-23T06:39:54.000Z (almost 3 years ago)
- Last Synced: 2025-07-05T05:05:53.810Z (12 months ago)
- Topics: algorithms, mathematics, numpy, python
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Magic square finding algorithms
This repo contains some algorithm implementations of solving [ordinary magic squares](https://en.wikipedia.org/wiki/Magic_square#Classification_of_magic_squares)
I don't claim to be an author of any of the algorithms, these are just my implementations using NumPy. The script contains a main method for testing and the following algorithms defined as seperate functions:
0. **validate(numbers, size)** - validates whether a given matrix is an ordinary magic square
1. **calculateMagicConstant(size)** - calculates the magic constant, given the size of the matrix
2. **bogoSolveMagicSquare(size)** - shuffles the matrix until a magic square is found (very slow)
3. **oddMagicSquareSolve(size, startNumber)** - given the size and starting number, finds a magic square. Only works with odd values of n. Based on https://www.1728.org/magicsq1.htm
4. **oddMagicSquareSolve2(size)** - given the size, finds a magic square. Only works with odd values of n. Based on https://www.geeksforgeeks.org/magic-square/
5. **doublyEvenMagicSquareSolve(size)** - given the size, finds a magic square. Only works with doubly-even (divisible by 4) values of n. Based on https://www.1728.org/magicsq2.htm
6. **singlyEvenMagicSquareSolve(size)** - given the size, finds a magic square. Only works with singly-even (divisible by 2 but not by 4) values of n. Based on https://www.1728.org/magicsq3.htm
Size in the function definitions correspond to n