Juerd Waalboer

# NAME

Math::MagicSquare::Generator - Magic Square Generator

# SYNOPSIS

``````    use Math::MagicSquare::Generator

my \$square = Math::MagicSquare::Generator->new(size => 5,
step => 3,
start=> 6);
for (\$square, \$square->vflip, \$square->hflip) {
print \$_->as_string;
print "-----\n";
}

\$square->[0][0] = -15; # Break magic :)
print \$square->check ? "Magic square\n" : "Just a square\n";

print '<html><body>';
print Math::MagicSquare::Generator->new->hflip->vflip->as_html;
print '</body></html>';``````

# DESCRIPTION

This module creates magic squares. A magic square is a square in which all numbers are different and the sums of all rows, all columns and the two diagonals are equal. Math::MagicSquare::Generator cannot create panmagic squares, or squares that have an even size. (A panmagic square is magic square where the "wrapped" diagonals are also equal.)

# EXAMPLE

``````     3 16  9 22 15  This square is the output of
20  8 21 14  2  print Math::MagicSquare::Generator->new->as_string;
7 25 13  1 19
24 12  5 18  6
11  4 17 10 23

The sums of the rows are 65.
The sums of the columns are 65.
The sums of the diagonals are 65.``````

# METHODS

new

The constructor that generates the square immediately. It creates an object using the given named arguments. Valid arguments are `size`, `step` and `start`. `size` has to be positive, odd and integer.

check

A checker - returns the common sum if the square is magic, or undef if it's not. Because the sum can never be 0, you can use this as a boolean value. (Well, the sum in a 1x1 square can be 0, if the single number is 0.) You can use this method to check if the square has been tampered with.

sum

Returns the common sum of the rows, columns and diagonals.

vflip, hflip

These methods return a vertically or horizontally flipped clone of the square. The clone is a Math::MagicSquare::Generator, so stacking these methods is possible.

as_string, as_html, as_csv

DWYM - return the square as a formatted string, piece of html or in CSV format.

# THIS MODULE AND Math::MagicSquare

Math::MagicSquare is a module that checks if a square is magical. It takes a list in its `new` method, so you'll have to dereference the generated square:

``````    use Math::MagicSquare;
use Math::MagicSquare::Generator;

my \$square = Math::MagicSquare::Generator->new;
print Math::MagicSquare->new( @\$square )->check, "\n"; # 2``````

Its `check` will always return 2 for squares generated using this module (or 3 if it's a 1x1 square).

None yet.

# AUTHOR

Juerd <juerd@juerd.nl>