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)


' version 23-06-2015
' compile with: fbc -s console
 
Sub magicsq(size As Integer, filename As String ="")
 
    If (size And 1) = 0 Or size < 3 Then
        Print : Beep ' alert
        Print "error: size is not odd or size is smaller then 3"
        Sleep 3000,1  'wait 3 seconds, ignore key press
        Exit Sub
    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
 
    Dim As Integer sq(size,size) ' array to hold square
    ' start in the middle of the first row
    Dim As Integer nr = 1, x = size - (size \ 2), y = 1
    Dim As Integer max = size * size
    ' create format string for using
    Dim As String frmt = String(Len(Str(max)) +1, "#")
 
    ' main loop for creating magic square
    Do
        If sq(x, y) = 0 Then
            sq(x, y) = nr
            If nr Mod size = 0 Then
                y += 1
            Else
                x += 1
                y -= 1
            End If
            nr += 1
        End If
        If x > size Then
            x = 1
            Do While sq(x,y) <> 0
                x += 1
            Loop
        End If
        If y < 1 Then
            y = size
            Do While sq(x,y) <> 0
                y -= 1
            Loop
        EndIf
    Loop 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) \ 2) * size
    Print
 
    For y = 1 To size
        For x = 1 To size
            Print Using frmt; sq(x,y);
        Next
        Print
    Next
    print
 
    ' output magic square to a file with the name provided
    If filename <> "" Then
        nr = FreeFile
        Open filename For Output As #nr
        Print #nr, "Odd magic square size:"; size; " *"; size
        Print #nr, "The magic sum ="; ((max +1) \ 2) * size
        Print #nr,
 
        For y = 1 To size
            For x = 1 To size
                Print #nr, Using frmt; sq(x,y);
            Next
            Print #nr,
        Next
    End If
    Close
 
End Sub
 
' ------=< MAIN >=------
 
magicsq(5)
magicsq(11)
' the next line will also print the square to a file called: magic_square_19.txt
magicsq(19, "magic_square_19.txt")
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End