Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/r4nd0lph-c/magic_square_generator

This class is an implementation of a normal magic square.
https://github.com/r4nd0lph-c/magic_square_generator

mathematics

Last synced: 2 days ago
JSON representation

This class is an implementation of a normal magic square.

Awesome Lists containing this project

README

        

# Magic Square generator ✨
### Theory
A Magic square is a square array n×n filled with different numbers in such a way that the sum of the numbers in each row, each column and on both diagonals is the same.

If the array includes just the positive integers 1, 2, ..., n² the magic square is said to be normal.

* Order is the number of integers along one side n;
* Magic constant is the sum of the numbers in each row, column and on the diagonals, depends only on n and is determined by the formula n×(n²+1)/2.

Normal magic squares exist for all orders of n>=1, with the exception of n=2, although the case of n=1 is trivial - the square consists of a single number.

This class is an implementation of a normal magic square.

------------

### Examples
```java
// Creating a class object (size n)
MagicSquare square = new MagicSquare(n);

// Get the order value
square.getOrder();

// Get the magic constant value
square.getMagicConstant();

// Displaying the elements of the magic square on the screen
for (int i = 0; i < square.getOrder(); i++) {
for (int j = 0; j < square.getOrder(); j++) {
System.out.print(square.getBody()[i][j] + " ");
}
System.out.println("");
}
```

------------
### Results
![result image](result.png)
------------

### References to the sources used
- [Definition](https://en.wikipedia.org/wiki/Magic_square "Definition")
- [Odd Magic Squares | Algorithm](https://www.1728.org/magicsq1.htm "Odd Magic Squares | Algorithm")
- [Doubly Even Magic Squares | Algorithm](https://www.1728.org/magicsq2.htm "Doubly Even Magic Squares | Algorithm")
- [Singly Even Magic Squares | Algorithm](https://www.1728.org/magicsq3.htm "Singly Even Magic Squares | Algorithm")

------------