This is a program to generate a magic square using the Siamese method
(as described above).
It begins with placeing 1 in any location (in the code it was placed
at the top center).

#!/usr/local/bin/perl5

$n = shift @ARGV;
if(not $n % 2) { die "$n is not odd.\n"; }

$x = int($n/2); $y = 0;

my @S;

for ($i = 1; $i <= ($n * $n); $i++)
{
    if($S[$x][$y] == 0)
      { $S[$x][$y] = $i; }
    else
    { 
        $x ++;
        $x %= $n;
        $y += 2;
        $y %= $n;
        $S[$x][$y] = $i;
    }   
    $x--;
    $x %= $n;
    $y--;
    $y %= $n;
}   
    
for ($y = 0; $y < $n; $y++)
{
    for($x = 0; $x < $n; $x++)
      { print "$S[$x][$y]\t"; }
    print "\n";
}