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)


 
PROGRAM MAGIC_SQUARE
 
!$INTEGER
 
PROCEDURE Magicsq(size,filename$)
 
LOCAL DIM sq[25,25] ! array to hold square
 
IF (size AND 1)=0 OR size<3 THEN
     PRINT PRINT(CHR$(7)) ! beep
     PRINT("error: size is not odd or size is smaller then 3")
     PAUSE(3)
     EXIT PROCEDURE
END IF
 
! filename$ <> "" then save magic square in a file
! filename$ can contain directory name
! if filename$ exist it will be overwriten, no error checking
 
! start in the middle of the first row
   nr=1   x=size-(size DIV 2) y=1
   max=size*size
 
! create format string for using
   frmt$=STRING$(LEN(STR$(max)),"#")
 
! main loop for creating magic square
   REPEAT
      IF sq[x,y]=0 THEN
        sq[x,y]=nr
        IF nr MOD size=0 THEN
           y=y+1
         ELSE
           x=x+1
           y=y-1
        END IF
        nr=nr+1
      END IF
      IF x>size THEN
         x=1
         WHILE sq[x,y]<>0 DO
            x=x+1
         END WHILE
      END IF
      IF y<1 THEN
         y=size
         WHILE sq[x,y]<>0 DO
            y=y-1
         END WHILE
      END IF
   UNTIL nr>max
 
! printing square's bigger than 19 result in a wrapping of the line
   PRINT("Odd magic square size:";size;"*";size)
   PRINT("The magic sum =";((max+1) DIV 2)*size)
   PRINT
 
   FOR y=1 TO size DO
      FOR x=1 TO size DO
          WRITE(frmt$;sq[x,y];)
      END FOR
      PRINT
   END FOR
 
  ! output magic square to a file with the name provided
  IF filename$<>"" THEN
        OPEN("O",1,filename$)
           PRINT(#1,"Odd magic square size:";size;" *";size)
           PRINT(#1,"The magic sum =";((max+1) DIV 2)*size)
           PRINT(#1,)
 
           FOR y=1 TO size DO
             FOR x=1 TO size DO
               WRITE(#1,frmt$;sq[x,y];)
             END FOR
             PRINT(#1,)
           END FOR
  END IF
  CLOSE(1)
 
END PROCEDURE
 
BEGIN
PRINT(CHR$(12);)  ! CLS
Magicsq(5,"")
Magicsq(11,"")
!----------------------------------------------------
! the next line will also print the square to a file
! called 'magic_square_19txt'
!----------------------------------------------------
Magicsq(19,"msq_19.txt")
 
END PROGRAM
 