User:Manoguru/sandbox

Properties
The congruence relations satisfies all the conditions of an equivalence relation:
 * Reflexivity: $a ≡ a (mod n)$
 * Symmetry: $a ≡ b (mod n)$ if and only if $b ≡ a (mod n)$
 * Transitivity: If $a ≡ b (mod n)$ and $b ≡ c (mod n)$, then $a ≡ c (mod n)$

If $a_{1} ≡ b_{1} (mod n)$ and $a_{2} ≡ b_{2} (mod n),$ or if $a ≡ b (mod n),$ then:
 * $a + k ≡ b + k (mod n)$ for any integer $k$ (compatibility with translation)
 * $k a ≡ k b (mod n)$ for any integer $k$ (compatibility with scaling)
 * $a_{1} + a_{2} ≡ b_{1} + b_{2} (mod n)$ (compatibility with addition)
 * $a_{1} - a_{2} ≡ b_{1} - b_{2} (mod n)$ (compatibility with subtraction)
 * $a_{1} a_{2} ≡ b_{1} b_{2} (mod n)$ (compatibility with multiplication)
 * $a^{k} ≡ b^{k} (mod n)$ for any non negative integer $k$ (compatibility with exponentiation)
 * $p(a) ≡ p(b) (mod n)$, for any polynomial $p(x)$ with integer coefficients (compatibility with polynomial evaluation)

If $a ≡ b (mod n)$, then it is false, in general, that $k^{a} ≡ k^{b} (mod n)$. However, one has:
 * If $c ≡ d (mod φ(n)),$ where φ is Euler's totient function, then $a^{c} ≡ a^{d} (mod n)$ provided $a$ is coprime with $n$

For cancellation of common terms, we have the following rules:


 * If $a + k ≡ b + k (mod n)$ for any integer $k$, then $a ≡ b (mod n)$
 * If $k a ≡ k b (mod n)$ and $k$ is coprime with $n$, then $a ≡ b (mod n)$

Lastly, let the multiplicative inverse of $a$ be denoted by $a^{-1}$, then we have the following rules:
 * Existence of a multiplicative inverse: there exists an integer denoted $a^{-1}$ such that $aa^{-1} ≡ 1 (mod n)$ if and only if $a$ is coprime with $n$.
 * If $a ≡ b (mod n)$ and $a^{-1}$ exists, then $a^{-1} ≡ b^{-1} (mod n)$ (compatibility with multiplicative inverse)
 * If $a x ≡ b (mod n)$ and $a$ is coprime to $n$, the solution to this linear congruence is given by $x ≡ a^{-1}b (mod n)$

In particular, if $p$ is a prime number then $a$ is coprime with $p$ for every $a$ such that $0 < a < p$. Thus, a multiplicative inverse exists for all $a$ that are not congruent to zero modulo $p$.

Some of the more advanced properties of congruence relations are the following:
 * Fermat's little theorem: If $p$ is prime, then $a ^{p - 1} ≡ 1 (mod p)$ for $0 < a < p$
 * Euler's theorem: If $a$ and $n$ are coprime, then $a ^{φ(n)} ≡ 1 (mod n)$, where φ is Euler's totient function
 * A simple consequence of Fermat's little theorem is that if $p$ is prime, then $a^{-1} ≡ a ^{p - 2} (mod p)$ is the multiplicative inverse of $0 < a < p$. More generally, from Euler's theorem, if $a$ and $n$ are coprime, then $a^{-1} ≡ a ^{φ(n) - 1} (mod n)$.
 * Another simple consequence is that if $a ≡ b (mod φ(n)),$ where φ is Euler's totient function, then $k^{a} ≡ k^{b} (mod n)$ provided $k$ is coprime with $n$
 * Wilson's theorem: $p$ is prime if and only if $(p - 1)! ≡ -1 (mod p)$
 * Chinese remainder theorem: If $x ≡ a (mod m)$ and $x ≡ b (mod n)$ such that $gcd(m, n) = 1$, then $x ≡ b m_{n}^{-1} m + a n_{m}^{-1} n (mod mn)$ where $m_{n}^{-1}$ is the inverse of $m$ modulo $n$ and $n_{m}^{-1}$ is the inverse of $n$ modulo $m$
 * Lagrange's theorem: The congruence $f (x) ≡ 0 (mod p)$, where $f (x) = a_{0} x^{n} + ... + a_{n} (mod p)$ such that $a_{0} &ne; (mod p)$ and $p$ be prime, has at most $n$ roots.
 * Primitive root modulo n: A number $g$ is a primitive root modulo $n$ if every number $a$ co-prime to $n$ is congruent to a power of $g$ modulo $n$. That is, for every integer $a$ co-prime to $n$, there is an integer $k$ such that $g^{k} ≡ a (mod n)$. A primitive root modulo $n$ exits if and only if $n$ is equal to $2, 4, p^{k}$ or $2p^{k}$ where $p^{k}$ is a power of an odd prime number. If the primitive roots modulo $n$ exist, then there are exactly $φ(φ(n))$ such primitive roots, where φ is the Euler's totient function.
 * Quadratic residue: An integer $a$ is called a quadratic residue modulo $n$ if it is congruent to a perfect square modulo $n$; i.e., if there exists an integer $x$ such that $x^{2} ≡ a (mod n)$. Otherwise, $a$ is called a quadratic non-residue modulo $n$. For prime $p$, the Euler's criterion to check if $a$ is or is not a quadratic residue is, as expressed using the Legendre symbol,
 * $$ \left(\frac{a}{p}\right) \equiv a^{\frac{p-1}{2}} \pmod{p} \quad \text{ and } \quad\left(\frac{a}{p}\right) \in \{-1,0,1\}. $$

The properties of Legendre symbol and its variants can be used to efficiently check whether or not a number is a quadratic residue.

Euler's method
Euler's method for constructing the magic square is similar to De la Hire's method. It consists of breaking the magic square into two primary squares, which when added gives the magic square. As a running example, we will consider a 3×3 magic square. We can uniquely label each number of the 3×3 square by a pair of numbers as

where each pairs of Greek and Latin alphabets, e.g. αa, are meant to be added together, i.e. αa = α + a. Here, (α,β,γ) = (0, 3, 6) and (a,b,c) = (1, 2, 3). An important general constraint to note here is that a Greek letter is paired with a Latin letter only once. Thus, the original square can now be split into two simpler squares:

A magic square can be constructed by ensuring that the Greek and Latin squares are magic squares too. However, the Greek and Latin squares with just three unique terms are much easier to deal with than the original square with nine different terms. The row sum and the column sum of the Greek square will be the same, α + β + γ, if each letter appears only once in a given column and a row. Likewise, for the odd square, since α, β, and γ are in arithmetic progression, their sum is equal to the order of the square times the middle term, i.e. α + β + γ = 3 β. Thus, the diagonal sums will be equal if we have βs in the main diagonal and α, β, γ in the skew diagonal. Similarly, for the Latin square. The resulting Greek and Latin squares and their combination will be as below. Note that the Latin square is just a rotation of the Greek square with the corresponding letters interchanged. Substituting the values of the Greek and Latin letters will give the 3×3 magic square.

Number of basic pandiagonal magic squares
There are two basic ways of placing the four alphabets in the central 2×2 sub-square, not including the reflections, such that the letters are paired to their opposites along the column. Likewise there is only one way of placing the letters in the corners for a given central square (the complementary letters are placed at the opposite corners), such that the resulting square is pan-diagonal. Also there are just two basic ways of placing two complementary alphabets in the central square, not including rotations and reflections, which gives rise to pan-diagonal magic squares. Thus there 4 such basic Greek squares, as shown below. However, in order to ensure that the symbols are uniquely paired to each other, only the first two Greek squares can be used to construct a Latin square. Given a Greek square, a valid Latin square can be constructed by flipping it along the main diagonal or the skew diagonal, or rotating 90 degrees clockwise or anti-clockwise. This gives us 4 possible Latin squares from a given Greek square. Since we can pair one Latin square with any two Greek square, the total number of basic most-perfect 4×4 magic squares constructed this way are 2×4×2 = 16.

6-by-6 Square
Another example of a 6×6 magic square constructed this way is given below, where the diagonal entries are arranged differently:

Composite Squares
The "product" of two magic squares creates a magic square of higher order than the two multiplicands. Let the two magic squares be of orders M and N, such that M ≤ N, then the final square will be of order M × N. There will be N sub squares of M order which will also be a magic.


 * 1) Divide the final checkerboard into N × N sub-checkers of M × M squares.
 * 2) In the square N, reduce by 1 the value of all the numbers.
 * 3) Multiply these reduced values ​​by M × M. The results are reported in the boxes of each corresponding sub-checker of the final square.
 * 4) The squares of the square M are added N × N times to the boxes of the final checkerboard.

Bordering method
In this method, the objective is to wrap a border around a smaller magic square which serves as a core. Let us consider the 3×3 square for example. Subtracting the middle number 5 from each number, we have 0, ± 1, ± 2, ± 3, and ± 4; and the magic constant will be zero. Putting the middle number 0 in the center cell, we want to construct a border such that the resulting square is magic. Let the border be given by:

Since the sum of each row, column, and diagonals must be a constant (which is zero), we have


 * a + a* = 0,
 * b + b* = 0,
 * u + u* = 0,
 * v + v* = 0.

Thus, it must be that if we have chosen a, b, u, and v, then we have a* = - a, b* = - b, u* = - u, and v* = - v. But how should we choose a, b, u, and v? We have the sum of the top row and the right column as


 * u + a + v = 0,
 * v + b + u* = 0.

Since 0 is an even number, there are only two ways that the sum of three numbers will yield an even number: 1) if all three were even, or 2) if two were odd and one was even. Since in our choice of numbers we only have two even non-zero number (± 2 and ± 4), it must be the case that the second statement is true: that two of the numbers are odd and one even.

The only way that both the above two equations can satisfy this parity condition simultaneously, and be consistent with the set of numbers we have, is when u and v are odd. Thus, taking u = 1 and v = 3, we have a = - 4 and b = -2. Hence, the finished square will be as in the left. Adding 5 to each number, we get the finished magic square.

Medjig-method for squares of even order 2n, where n > 2
The namesake of this method derives from mathematical game called medjig created by Willem Barink in 2006, although the method itself is much older. An early instance of a magic square constructed using this method occurs in Yang Hui's text for order 6 magic square. The LUX method to construct singly even magic squares is a special case of the medjig method, where only 3 out of 24 patterns are used to construct the medjig square.

The pieces of the medjig puzzle are 2×2 squares on which the numbers 0, 1, 2 and 3 are placed. There are three basic patterns by which the numbers 0, 1, 2 and 3 can be placed in a 2×2 square, where 0 is at the top left corner:

Each pattern can be reflected and rotated to obtain 8 equivalent patterns, giving us a total of 3×8 = 24 patterns. The aim of the puzzle is to take n2 medjig pieces and arrange them in an n × n medjig square in such a way that each row, column, along with the two long diagonals, formed by the medjig square sums to 3n, the magic constant of the medjig square. An n × n medjig square can create a 2n × 2n magic square where n > 2.

Given an n×n medjig square and an n×n magic square base, a magic square of order 2n×2n can be constructed as follows:
 * Each cell of an n×n magic square is associated with a corresponding 2×2 subsquare of the medjig square
 * Fill each 2×2 subsquares of the medjig square with the four numbers from 1 to 4n2 that equal the original number modulo n2, i.e. x+n2y where x is the corresponding number from the magic square and y is a number from 0 to 3 in the 2×2 subsquares.

The sums of each medjig piece along the rows, columns and diagonals, denoted in italics, are:

Doubly even squares: The smallest medjig square is of order 2 with magic constant 6. While it is possible to construct a 2×2 medjig square, we cannot construct a 4×4 magic square from it since 2×2 magic squares required to "multiply" it does not exist. Nevertheless, it is worth constructing these 2×2 medjig squares. There exists 96 such 2×2 medjig squares. In the examples below, each 2×2 medjig square is made by combining different orientations of a single medjig piece.

We can use the 2×2 medjig squares to construct larger even ordered medjig squares. One possible approach is to simply combine the 2×2 medjig squares together. Another possibility is to wrap a smaller medjig square core with a medjig border. The pieces of a 2×2 medjig square can form the corner pieces of the border. An example of an 8×8 magic square is constructed below by combining four copies of the left most 2×2 medjig squares given above:

The next example is constructed by bordering a 2×2 medjig square core.

Singly even squares: Medjig square of order 1 does not exist. As such, the smallest odd ordered medjig square is of order 3, with magic constant 9. There are only 7 ways of partitioning the integer 9, our magic constant, into three parts. If these three parts correspond to three of the medjig pieces in a row, column or diagonal, then the relevant partitions for us are


 * 9 = 1 + 3 + 5 = 1 + 4 + 4 = 2 + 3 + 4 = 2 + 2 + 5 = 3 + 3 + 3.

Once a 3×3 medjig square has been constructed, we can convert it into a 6×6 magic square. For example:

An easy approach to construct higher order odd medjig square is by wrapping an smaller odd ordered medjig square with a medjig border, just as with even ordered medjig squares.