# Magic Square - Summary

**#11947**

**magic-square**

**Magic Square**

**non-GNU software and documentation**

This project is not part of the GNU Project.

Magic Square solves magic squares. For example, take the definition below:

This is the description. It is optional. Below the description is the

definition of the square. It can be optionally indented any amount (as

long as it is consistent).

Anything that cannot be parsed is considered part of the

description. So this:

+--+--+

|AB|CD|

+--+--+

|EF|GH|

+--+--+

is part of the description since letters are invalid.

After the square, there is a list of numbers in parentheses. Those are

to specify the other valid numbers not already in the square. They may

or may not include numbers already in the square. However, if they do,

they must include *all* the numbers.

If a square is completely full, it need not have the parentheses, but

it may optionally have a set of empty parentheses, "()".

If a square is *not* completely full, it MUST have the parentheses, or

it will be invalid and considered part of the description. For

example, the following is considered part of the description:

+--+--+

|12| |

+--+--+

| |34|

+--+--+

Since it does not have parentheses.

Numbers may also be specified in the standard hexadecimal format

(prefixed with "0x") and in octal (prefixed with "0"). They may also

run across multiple lines. All of these features are shown in the

square below.

The square is solvable--go on, try it yourself!

+--+--+--+

| |7 | |

| | | |

+--+--+--+

|01| |0x|

|1 | |1 |

+--+--+--+

| |3 | |

| | | |

+--+--+--+

(1, 0x2, 03, 4, 0x5, 06, 7, 0x8, 011)

If you solve the square, it will print out the description, and then the solution. In this case, we will pipe it to `tail' to get rid of the long description which you have already seen.

$ square example.square | tail -n 7

+--+--+--+

|2 |7 |6 |

+--+--+--+

|9 |5 |1 |

+--+--+--+

|4 |3 |8 |

+--+--+--+

If you pass the `-k' option, it will find all solutions.

$ square -k example.square | tail -n 9

Solution 1:

+--+--+--+

|2 |7 |6 |

+--+--+--+

|9 |5 |1 |

+--+--+--+

|4 |3 |8 |

+--+--+--+

As you can see, the square has only one solution. Let's try one with multiple solutions:

$ square -k <<EOF

> A square with multiple solutions.

>

> +--+--+--+

> | | |6 |

> +--+--+--+

> | | | |

> +--+--+--+

> |4 | | |

> +--+--+--+

>

> (1, 2, 3, 4, 5, 6, 7, 8, 9)

> EOF

A square with multiple solutions.

Solution 1:

+--+--+--+

|2 |7 |6 |

+--+--+--+

|9 |5 |1 |

+--+--+--+

|4 |3 |8 |

+--+--+--+

Solution 2:

+--+--+--+

|8 |1 |6 |

+--+--+--+

|3 |5 |7 |

+--+--+--+

|4 |9 |2 |

+--+--+--+

As you can see, there are two solutions. If you look carefully, you will notice that the two are just mirror images of each other, flipped along the diagonal from the 6 to the 4. Currently, Magic Square considers trivial differences like these to be different.

Magic Square can also use a binary format, compiled with `squarec'.

$ cat negative.square

A magic square with negative numbers.

+-+-+-+

| | | |

+-+-+-+

| | | |

+-+-+-+

| | | |

+-+-+-+

(-1, -2, -3, -4, -5, -6, -7, -8, -9)

$ squarec -o negative.bin negative.square

And we can use these as regular squares (if they don't have the .bin extension, you must specify the format with "--format=binary" or, shortened, "-fb").

$ square negative.bin

A magic square with negative numbers.

+--+--+--+

|-2|-7|-6|

+--+--+--+

|-9|-5|-1|

+--+--+--+

|-4|-3|-8|

+--+--+--+

We can also convert back to the text format. Let's convert it to stdout so we can view it.

$ squarec negative.bin

A magic square with negative numbers.

+--+--+--+

| | | |

+--+--+--+

| | | |

+--+--+--+

| | | |

+--+--+--+

(-1, -2, -3, -4, -5, -6, -7, -8, -9)

Registration Date: Sun 21 Jul 2019 07:31:33 PM UTC

License: GNU General Public License v3 or later

Development Status: 2 - Pre-Alpha

**1**member)

**2**public mailing lists)