'MgSqrOdd.bas hh 1990
' Construct odd order magic squares using the De la Loubere method.
' Traditionally this method starts with number 1 (the first number) in the
' center of the top row. However, many other locations may be used for the
' start by utilizing special rules (mostly self-evident).
' If you start at a different location using this program, the printout will
' show some zeros. The correct numbers must be entered manually!
' You may print out the completed magic square by copying it, then pasting
' into a word processor.
' n = size of square (the order)
' y = starting number of the series ocf consecutive numbers
' m = the array to hold the magic square
' s = the number currently being put into the square
' i-j = the array subscripts
' k = a multiple of the square size
CLS
DIM m(25, 25)
Loop5: INPUT "Enter the size of the square (the odd order=3 to 13)"; n
IF n < 3 OR n > 13 THEN GOTO Loop5
IF n MOD (2) = 0 THEN GOTO Loop5
row = 1: col = (n + 1) / 2 ' standard start position for De la Loubere method
INPUT "Enter the starting number"; Y
PRINT "You may start at a non-standard position, but read the note in the"
PRINT "listing regarding errors in the printout."
INPUT " Do you wish a non-standard starting position? ('Y' or 'Enter')"; q$
IF q$ <> "Y" THEN GOTO Loop6
INPUT "Enter starting position (row,col)"; row, col
Loop6: i = row: j = col
PRINT
s = Y
PRINT n; "by"; n; "magic square starting with"; s
PRINT : k = 1
Loop3: m(i, j) = s
s = s + 1
IF s > n ^ 2 + Y THEN GOTO Loop1 ' Is the square complete?
IF k < n THEN GOTO Loop2
k = 1: i = i + 1 ' reset k to 1 and set row index
GOTO Loop3
Loop2: i = i - 1: j = j + 1: k = k + 1 ' move position to the right & up
IF i <> 0 THEN GOTO Loop4
i = n: GOTO Loop3 ' outside of the square - reset row index to 1
Loop4: IF j <= n THEN GOTO Loop3
j = 1: GOTO Loop3 ' outside of the square - reset col. index to 1
Loop1: FOR i = 1 TO n ' print the square
FOR j = 1 TO n
PRINT m(i, j); TAB(j * 6);
NEXT j
PRINT
NEXT i
END