A package for magic squares and N-Queens problem.
Project description
Magic Square Project
This project contains Python modules for generating magic squares, solving the N-Queens problem, and generating the Bachet matrix.
Contents
Bachet Matrix Generator
The Bachet matrix is a unique square matrix with a distinct construction pattern. The matrix is named after Claude Gaspar Bachet de Méziriac. Learn more.
Functions
bachet_matrix(n: int)
: Generate the Bachet matrix of ordern
.
Magic Square Calculator
Generate a 4x4 magic square matrix based on a given date or predefined values.
>>> from magicsq.eulerbox import EulerMagicBox
>>> magic_square = EulerMagicBox.magicsquare("22-12-1887")
>>> magic_square
array([[22, 12, 18, 87],
[20, 85, 20, 14],
[84, 19, 15, 21],
[13, 23, 86, 17]])
>>>
Functions
magicsqaure(date: str, values_list: list, random: bool)
: Calculate the magic square matrix based on a given data.
>>> from magicsq.bachet_odd import *
>>> ordr = 5
>>> bachet = bachet_matrix(ordr)
>>> bachet
array([[ 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 2, 0, 6, 0, 0, 0],
[ 0, 0, 3, 0, 7, 0, 11, 0, 0],
[ 0, 4, 0, 8, 0, 12, 0, 16, 0],
[ 5, 0, 9, 0, 13, 0, 17, 0, 21],
[ 0, 10, 0, 14, 0, 18, 0, 22, 0],
[ 0, 0, 15, 0, 19, 0, 23, 0, 0],
[ 0, 0, 0, 20, 0, 24, 0, 0, 0],
[ 0, 0, 0, 0, 25, 0, 0, 0, 0]])
>>> bachet_ops = operations(bachet_ops, ordr)
>>> bachet_ops
array([[ 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 2, 0, 6, 0, 0, 0],
[ 0, 0, 3, 40, 7, 48, 11, 0, 0],
[ 0, 4, 32, 8, 50, 12, 8, 16, 0],
[ 5, 0, 9, 42, 13, 10, 17, 0, 21],
[ 0, 10, 44, 14, 2, 18, 20, 22, 0],
[ 0, 0, 15, 4, 19, 12, 23, 0, 0],
[ 0, 0, 0, 20, 0, 24, 0, 0, 0],
[ 0, 0, 0, 0, 25, 0, 0, 0, 0]])
>>> get_center_matrix(bachet_ops)
array([[ 3, 40, 7, 48, 11],
[32, 8, 50, 12, 8],
[ 9, 42, 13, 10, 17],
[44, 14, 2, 18, 20],
[15, 4, 19, 12, 23]])
N-Queens Solver
Solve the classing N-Queens problem where the objective is to place N
queens on an NxN
chessboard such that no two queens threaten each other.
Functions
solve()
: Solve the N-Queens problem and store the solutions._queens(n: int, i: int, *args)
: A generator for all possible queen placements._display_chessboard(queens_positions: list)
: Prints a visual representation of the chessboard.
>>> from magicsq.nqueens import NQueensSolver
>>> my_queen = NQueensSolver(8)
>>> my_queen_pos_obj = my_queen._queens(8)
>>> solution_list = list(my_queen_pos_obj)
display_solutions()
: Display all solutions for the N-Queens problem.
>>> from magicsq.nqueens import NQueensSolver as qs
>>> my_queen = qs(8)
>>> my_queen.solve()
>>> my_queen.display_solutions(1)
+--+--+--+--+--+--+--+--+
| Q| | | | | | | |
+--+--+--+--+--+--+--+--+
| | | | | | | Q| |
+--+--+--+--+--+--+--+--+
| | | | | Q| | | |
+--+--+--+--+--+--+--+--+
| | | | | | | | Q|
+--+--+--+--+--+--+--+--+
| | Q| | | | | | |
+--+--+--+--+--+--+--+--+
| | | | Q| | | | |
+--+--+--+--+--+--+--+--+
| | | | | | Q| | |
+--+--+--+--+--+--+--+--+
| | | Q| | | | | |
+--+--+--+--+--+--+--+--+
[1, 5, 8, 6, 3, 7, 2, 4]
Installation & Usage
pip install magicsq
License
This project is licensed under the GNU General Public License v3.0 (GPLv3) - see the LICENSE file for details.
The GPLv3 is a free, copyleft license for software and other kinds of works. This means you can use, modify, and distribute this software freely as long as your derived works are also open source under the GPLv3.
Contributions
- Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss potential improvements.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file magicsq-0.1.tar.gz
.
File metadata
- Download URL: magicsq-0.1.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 711db0edc26425ee98f8111c54e92b4b00b82813d208b1cd037275766868812f |
|
MD5 | 1f3acb4e5f85251e86d9cd988f0de9fc |
|
BLAKE2b-256 | 25cdd70c90672eb9be5261d97a3217199c869256365af932ca5d9244a3028a75 |
File details
Details for the file magicsq-0.1-py3-none-any.whl
.
File metadata
- Download URL: magicsq-0.1-py3-none-any.whl
- Upload date:
- Size: 20.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3279e7e124cf0b879795d50240cce0e8b75b1e7c5a1bdeafa39668786fe2ba8 |
|
MD5 | 62e75d2b1f261dd9a0a014e718b6de7a |
|
BLAKE2b-256 | 7894759a898eea99f0341a65f9e4b7364fddf1ce677b84a989aa45fc1e45f4de |