A magic square is an   NxN   square matrix whose numbers (usually integers) consist of consecutive numbers arranged so that the sum of each row and column,   and   both long (main) diagonals are equal to the same sum (which is called the   magic number   or   magic constant).

The numbers are usually (but not always) the first   N2   positive integers.

A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square.



 8 
 1 
 6


 3 
 5 
 7


 4 
 9 
 2



Task
For any odd   N,   generate a magic square with the integers    1 ──► N,   and show the results here.

Optionally, show the magic number.

You should demonstrate the generator by showing at least a magic square for   N = 5.



 Related tasks
 Magic squares of singly even order
 Magic squares of doubly even order



 See also

 MathWorld™ entry: Magic_square 
 Odd Magic Squares (1728.org)


# construct a magic square of odd order                                      #
PROC magic square = ( INT order ) [,]INT:
    IF NOT ODD order OR order < 1
    THEN
        # can't make a magic square of the specified order                   #
        LOC [ 1 : 0, 1 : 0 ]INT
    ELSE
        # order is OK - construct the square using de la Loubère's           #
        # algorithm as in the wikipedia page                                 #
 
        [ 1 : order, 1 : order ]INT square;
        FOR i TO order DO FOR j TO order DO square[ i, j ] := 0 OD OD;
 
        # as square [ 1, 1 ] if the top-left, moving "up" reduces the row    #
        # operator to advance "up" the square                                #
        OP   PREV = ( INT pos )INT: IF pos = 1 THEN order ELSE pos - 1 FI;
        # operator to advance "across right" or "down" the square            #
        OP   NEXT = ( INT pos )INT: ( pos MOD order ) + 1;
 
        # fill in the square, starting from the middle of the top row        #
        INT col := ( order + 1 ) OVER 2;
        INT row := 1;
        FOR i TO order * order DO
            square[ row, col ] := i;
            IF square[ PREV row, NEXT col ] /= 0
            THEN
                # the up/right position is already taken, move down          #
                row := NEXT row
            ELSE
                # can move up and right                                      #
                row := PREV row;
                col := NEXT col
            FI
        OD;
 
        square
    FI # magic square # ;
 
# prints the magic square                                                    #
PROC print square = ( [,]INT square )VOID:
    BEGIN
        INT order = 1 UPB square;
        # calculate print width: negative so a leading "+" is not printed    #
        INT width := -1;
        INT mag   := order * order;
        WHILE mag >= 10 DO mag OVERAB 10; width MINUSAB 1 OD;
        # calculate the "magic sum"                                          #
        INT sum := 0;
        FOR i TO order DO sum +:= square[ 1, i ] OD;
        # print the square                                                   #
        print( ( "maqic square of order ", whole( order, 0 ), ": sum: ", whole( sum, 0 ), newline ) );
        FOR i TO order DO
            FOR j TO order DO write( ( " ", whole( square[ i, j ], width ) ) ) OD;
            write( ( newline ) )
        OD
    END # print square # ;
 
# test the magic square generation                                           #
FOR order BY 2 TO 7 DO print square( magic square( order ) ) OD