Magic squares of odd order: Difference between revisions

Added Easylang
(Magic squares of odd order in BASIC-256)
(Added Easylang)
 
(9 intermediate revisions by 6 users not shown)
Line 878:
MAGIC CONSTANT: 65</pre>
 
==={{header|IS-BASICBASIC256}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "MagicN.bas"
110 DO
120 INPUT PROMPT "The square order: ":N
130 LOOP UNTIL MOD(N,2)>0 AND INT(N)=N AND N>0
140 FOR I=1 TO N
150 FOR J=1 TO N
160 PRINT USING " ###":MOD((I*2-J+N-1),N)*N+MOD(I*2+J-2,N)+1;
170 NEXT
180 PRINT
190 NEXT
200 PRINT "The magic number is:";N*(N^2+1)/2</syntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="freebasic">arraybase 1
Line 936 ⟶ 923:
print rjust(string(m[row,col]),4);
next col
#print
print
next row
Line 943 ⟶ 929:
end if
end subroutine</syntaxhighlight>
{{out}}
<pre>5 x 5 Magic Square --- Magic constant is 65
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
 
17 x 17 Magic Square --- Magic constant is 2465
155 174 193 212 231 250 269 288 1 20 39 58 77 96 115 134 153
173 192 211 230 249 268 287 17 19 38 57 76 95 114 133 152 154
191 210 229 248 267 286 16 18 37 56 75 94 113 132 151 170 172
209 228 247 266 285 15 34 36 55 74 93 112 131 150 169 171 190
227 246 265 284 14 33 35 54 73 92 111 130 149 168 187 189 208
245 264 283 13 32 51 53 72 91 110 129 148 167 186 188 207 226
263 282 12 31 50 52 71 90 109 128 147 166 185 204 206 225 244
281 11 30 49 68 70 89 108 127 146 165 184 203 205 224 243 262
10 29 48 67 69 88 107 126 145 164 183 202 221 223 242 261 280
28 47 66 85 87 106 125 144 163 182 201 220 222 241 260 279 9
46 65 84 86 105 124 143 162 181 200 219 238 240 259 278 8 27
64 83 102 104 123 142 161 180 199 218 237 239 258 277 7 26 45
82 101 103 122 141 160 179 198 217 236 255 257 276 6 25 44 63
100 119 121 140 159 178 197 216 235 254 256 275 5 24 43 62 81
118 120 139 158 177 196 215 234 253 272 274 4 23 42 61 80 99
136 138 157 176 195 214 233 252 271 273 3 22 41 60 79 98 117
137 156 175 194 213 232 251 270 289 2 21 40 59 78 97 116 135</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 sub magicsq(size,filename$ = "")
120 if (size and 1) = 0 or size < 3 then
130 print
140 print "error: size is not odd or size is smaller then 3"
160 exit sub
170 endif
180 ' filename$ <> "" then save magic square in a file
190 ' filename$ can contain directory name
200 ' if filename$ exist it will be overwriten, no error checking
210 dim sq(size,size)' array to hold square
220 ' start in the middle of the first row
230 nr = 1
240 x = size-int(size/2)
250 y = 1
260 max = size*size
270 ' create format string for using
280 for c = 1 to len(str$(max))+1 : frmt$ = frmt$+"#" : next c
290 'main loop for creating magic square
300 do
310 if sq(x,y) = 0 then
320 sq(x,y) = nr
330 if nr mod size = 0 then
340 y = y+1
350 else
360 x = x+1
370 y = y-1
380 endif
390 nr = nr+1
400 endif
410 if x > size then
420 x = 1
430 do while sq(x,y) <> 0
440 x = x+1
450 loop
460 endif
470 if y < 1 then
480 y = size
490 do while sq(x,y) <> 0
500 y = y-1
510 loop
520 endif
530 loop until nr > max
540 ' printing square's bigger than 19 result in a wrapping of the line
550 print "Odd magic square size: ";size;"*";size
560 print "The magic sum = ";int((max+1)/2)*size
570 print
580 for y = 1 to size
590 for x = 1 to size
600 print using "####";val(sq(x,y));
610 next x
620 print
630 next y
640 print
650 ' output magic square to a file with the name provided
660 if filename$ <> "" then
670 nr = freefile
680 open filename$ for output as #1
690 print #1,"Odd magic square size: ";size;"*";size
700 print #1,"The magic sum = ";int((max+1)/2)*size
710 print #1,
720 for y = 1 to size
730 for x = 1 to size
740 print #1,using frmt$;sq(x,y);
750 next x
760 print #1,
770 next y
780 endif
790 close #1
800 end sub
810 input "Enter N: ",number
820 magicsq(number)
830 end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' 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</syntaxhighlight>
{{out}}
<pre>Odd magic square size: 5 * 5 Odd magic square size: 11 * 11
The magic sum = 65 The magic sum = 671
17 24 1 8 15 68 81 94 107 120 1 14 27 40 53 66
23 5 7 14 16 80 93 106 119 11 13 26 39 52 65 67
4 6 13 20 22 92 105 118 10 12 25 38 51 64 77 79
10 12 19 21 3 104 117 9 22 24 37 50 63 76 78 91
11 18 25 2 9 116 8 21 23 36 49 62 75 88 90 103
7 20 33 35 48 61 74 87 89 102 115
19 32 34 47 60 73 86 99 101 114 6
31 44 46 59 72 85 98 100 113 5 18
43 45 58 71 84 97 110 112 4 17 30
55 57 70 83 96 109 111 3 16 29 42
Only the first 2 square shown. 56 69 82 95 108 121 2 15 28 41 54</pre>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
{{trans|IS-BASIC}}
<syntaxhighlight lang="qbasic">100 REM Magic squares of odd order
110 INPUT "The square order: ", N
115 'INPUT "The square order:"; N ' for MSX Basic
120 IF (N AND 1) = 0 OR N < 3 THEN PRINT "error: size is not odd or size is smaller then 3" : GOTO 100
130 FOR I = 1 TO N
140 FOR J = 1 TO N
150 PRINT USING " ###"; ((I*2-J+N-1) MOD N) * N + ((I*2+J-2) MOD N) + 1;
160 NEXT J
170 PRINT
180 NEXT I
190 PRINT "The magic number is: "; N * (N^2+1) / 2</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="qbasic">100 PROGRAM "MagicN.bas"
110 DO
120 INPUT PROMPT "The square order: ":N
130 LOOP UNTIL MOD(N,2)>0 AND INT(N)=N AND N>0
140 FOR I=1 TO N
150 FOR J=1 TO N
160 PRINT USING " ###":MOD((I*2-J+N-1),N)*N+MOD(I*2+J-2,N)+1;
170 NEXT
180 PRINT
190 NEXT
200 PRINT "The magic number is:";N*(N^2+1)/2</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">Dim m(1,1)
 
Call magicSquare 5
Call magicSquare 17
 
End
 
Sub magicSquare n
ReDim m(n,n)
inc = 1
count = 1
row = 1
col=(n+1)/2
While count <= n*n
m(row,col) = count
count = count + 1
If inc < n Then
inc = inc + 1
row = row - 1
col = col + 1
If row <> 0 Then
If col > n Then col = 1
Else
row = n
End If
Else
inc = 1
row = row + 1
End If
Wend
Call printSquare n
End Sub
 
Sub printSquare n
'Arbitrary limit to fit width of A4 paper
If n < 23 Then
Print n;" x ";n;" Magic Square --- ";
Print "Magic constant is ";Int((n*n+1)/2*n)
For row = 1 To n
For col = 1 To n
Print Using("####",m(row,col));
Next col
Print
Print
Next row
Else
Notice "Magic Square will not fit on one sheet of paper."
End If
End Sub </syntaxhighlight>
{{Out}}
<pre>5 x 5 Magic Square --- Magic constant is 65
17 24 1 8 15
 
23 5 7 14 16
 
4 6 13 20 22
 
10 12 19 21 3
 
11 18 25 2 9
 
17 x 17 Magic Square --- Magic constant is 2465
155 174 193 212 231 250 269 288 1 20 39 58 77 96 115 134 153
 
173 192 211 230 249 268 287 17 19 38 57 76 95 114 133 152 154
 
191 210 229 248 267 286 16 18 37 56 75 94 113 132 151 170 172
 
209 228 247 266 285 15 34 36 55 74 93 112 131 150 169 171 190
 
227 246 265 284 14 33 35 54 73 92 111 130 149 168 187 189 208
 
245 264 283 13 32 51 53 72 91 110 129 148 167 186 188 207 226
 
263 282 12 31 50 52 71 90 109 128 147 166 185 204 206 225 244
 
281 11 30 49 68 70 89 108 127 146 165 184 203 205 224 243 262
 
10 29 48 67 69 88 107 126 145 164 183 202 221 223 242 261 280
 
28 47 66 85 87 106 125 144 163 182 201 220 222 241 260 279 9
 
46 65 84 86 105 124 143 162 181 200 219 238 240 259 278 8 27
 
64 83 102 104 123 142 161 180 199 218 237 239 258 277 7 26 45
 
82 101 103 122 141 160 179 198 217 236 255 257 276 6 25 44 63
 
100 119 121 140 159 178 197 216 235 254 256 275 5 24 43 62 81
 
118 120 139 158 177 196 215 234 253 272 274 4 23 42 61 80 99
 
136 138 157 176 195 214 233 252 271 273 3 22 41 60 79 98 117
 
137 156 175 194 213 232 251 270 289 2 21 40 59 78 97 116 135
</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{trans|IS-BASIC}}
<syntaxhighlight lang="qbasic">100 REM Magic squares of odd order
110 INPUT "The square order:"; N
120 IF (N AND 1) = 0 OR N < 3 THEN PRINT "error: size is not odd or size is smaller then 3" : GOTO 100
130 FOR I = 1 TO N
140 FOR J = 1 TO N
150 PRINT USING " ###"; ((I*2-J+N-1) MOD N) * N + ((I*2+J-2) MOD N) + 1;
160 NEXT J
170 PRINT
180 NEXT I
190 PRINT "The magic number is:"; N * (N^2+1) / 2</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|Pascal}}
<syntaxhighlight lang="purebasic">#N=9
Define.i i,j
 
If OpenConsole("Magic squares")
PrintN("The square order is: "+Str(#N))
For i=1 To #N
For j=1 To #N
Print(RSet(Str((i*2-j+#N-1) % #N*#N + (i*2+j-2) % #N+1),5))
Next
PrintN("")
Next
PrintN("The magic number is: "+Str(#N*(#N*#N+1)/2))
EndIf
Input()</syntaxhighlight>
{{out}}
<pre>The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369</pre>
 
==={{header|QB64}}===
<syntaxhighlight lang="qb64">_Title "Magic Squares of Odd Order"
'$Dynamic
DefLng A-Z
Dim Shared As Long m(1, 1)
 
Call magicSquare(5)
Call magicSquare(15)
 
Sleep
System
 
Sub magicSquare (n As Integer)
Dim As Integer inc, count, row, col
 
If (n < 3) Or (n And 1) <> 1 Then n = 3
ReDim m(n, n)
inc = 1
count = 1
row = 1
col = (n + 1) / 2
While count <= n * n
m(row, col) = count
count = count + 1
If inc < n Then
inc = inc + 1
row = row - 1
col = col + 1
If row <> 0 Then
If col > n Then col = 1
Else
row = n
End If
Else
inc = 1
row = row + 1
End If
Wend
Call printSquare(n)
End Sub
 
Sub printSquare (n As Integer)
Dim As Integer row, col
'Arbitrary limit ensures a fit within console window
'Can be any size that fits within your computers memory limits
If n < 21 Then
Print "Order "; n; " Magic Square constant is "; Str$(Int((n * n + 1) / 2 * n))
For row = 1 To n
For col = 1 To n
Print Using "####"; m(row, col);
Next col
Print
' Print
Next row
End If
End Sub</syntaxhighlight>
{{Out}}
<pre>Order 5 Magic Square constant is 65
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Order 15 Magic Square constant is 1695
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120
138 155 172 189 206 223 15 17 34 51 68 85 102 119 121
154 171 188 205 222 14 16 33 50 67 84 101 118 135 137
170 187 204 221 13 30 32 49 66 83 100 117 134 136 153
186 203 220 12 29 31 48 65 82 99 116 133 150 152 169
202 219 11 28 45 47 64 81 98 115 132 149 151 168 185
218 10 27 44 46 63 80 97 114 131 148 165 167 184 201
9 26 43 60 62 79 96 113 130 147 164 166 183 200 217
25 42 59 61 78 95 112 129 146 163 180 182 199 216 8
41 58 75 77 94 111 128 145 162 179 181 198 215 7 24
57 74 76 93 110 127 144 161 178 195 197 214 6 23 40
73 90 92 109 126 143 160 177 194 196 213 5 22 39 56
89 91 108 125 142 159 176 193 210 212 4 21 38 55 72
105 107 124 141 158 175 192 209 211 3 20 37 54 71 88
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104
</pre>
 
==={{header|uBasic/4tH}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="text">' ------=< MAIN >=------
Proc _magicsq(5)
Proc _magicsq(11)
End
 
_magicsq Param (1) Local (4)
 
' reset the array
For b@ = 0 to 255
@(b@) = 0
Next
 
If ((a@ % 2) = 0) + (a@ < 3) + (a@ > 15) Then
Print "error: size is not odd or size is smaller then 3 or bigger than 15"
Return
EndIf
 
' start in the middle of the first row
b@ = 1
c@ = a@ - (a@ / 2)
d@ = 1
e@ = a@ * a@
 
' main loop for creating magic square
Do
If @(c@*a@+d@) = 0 Then
@(c@*a@+d@) = b@
If (b@ % a@) = 0 Then
d@ = d@ + 1
Else
c@ = c@ + 1
d@ = d@ - 1
EndIf
b@ = b@ + 1
EndIf
If c@ > a@ Then
c@ = 1
Do While @(c@*a@+d@) # 0
c@ = c@ + 1
Loop
EndIf
If d@ < 1 Then
d@ = a@
Do While @(c@*a@+d@) # 0
d@ = d@ - 1
Loop
EndIf
Until b@ > e@
Loop
 
Print "Odd magic square size: "; a@; " * "; a@
Print "The magic sum = "; ((e@+1) / 2) * a@
Print
 
For d@ = 1 To a@
For c@ = 1 To a@
Print Using "____"; @(c@*a@+d@);
Next
Print
Next
Print
Return</syntaxhighlight>
{{out}}
<pre>Odd magic square size: 5 * 5
The magic sum = 65
 
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
 
Odd magic square size: 11 * 11
The magic sum = 671
 
68 81 94 107 120 1 14 27 40 53 66
80 93 106 119 11 13 26 39 52 65 67
92 105 118 10 12 25 38 51 64 77 79
104 117 9 22 24 37 50 63 76 78 91
116 8 21 23 36 49 62 75 88 90 103
7 20 33 35 48 61 74 87 89 102 115
19 32 34 47 60 73 86 99 101 114 6
31 44 46 59 72 85 98 100 113 5 18
43 45 58 71 84 97 110 112 4 17 30
55 57 70 83 96 109 111 3 16 29 42
56 69 82 95 108 121 2 15 28 41 54
 
 
0 OK, 0:64</pre>
 
==={{header|VBA}}===
{{trans|C}}
Works with Excel VBA.
<syntaxhighlight lang="vb">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i As Integer, j As Integer, v As Integer
Debug.Print "The square order is: " & n
For i = 1 To n
For j = 1 To n
Cells(i, j) = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Next j
Next i
Debug.Print "The magic number of"; n; "x"; n; "square is:"; n * (n * n + 1) \ 2
End Sub 'magicsquare</syntaxhighlight>
 
==={{header|Visual Basic}}===
{{trans|C}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i As Integer, j As Integer, v As Integer
Debug.Print "The square order is: " & n
For i = 1 To n
For j = 1 To n
v = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Debug.Print Right(Space(5) & v, 5);
Next j
Debug.Print
Next i
Debug.Print "The magic number is: " & n * (n * n + 1) \ 2
End Sub 'magicsquare
</syntaxhighlight>
{{out}}
<pre>The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369</pre>
 
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .NET|2011}}
<syntaxhighlight lang="vbnet">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i, j, v As Integer
Console.WriteLine("The square order is: " & n)
For i = 1 To n
For j = 1 To n
v = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Console.Write(" " & Right(Space(5) & v, 5))
Next j
Console.WriteLine("")
Next i
Console.WriteLine("The magic number is: " & n * (n * n + 1) \ 2)
End Sub 'magicsquare</syntaxhighlight>
{{out}}
<pre>The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369</pre>
 
==={{header|Yabasic}}===
{{trans|Liberty BASIC}}
<syntaxhighlight lang="freebasic">magicSquare(5)
magicSquare(17)
end
 
sub magicSquare(n)
redim m(n,n)
inc = 1
cont = 1
row = 1
col = (n+1) / 2
while cont <= n*n
m(row,col) = cont
cont = cont + 1
if inc < n then
inc = inc + 1
row = row - 1
col = col + 1
if row <> 0 then
if col > n col = 1
else
row = n
end if
else
inc = 1
row = row + 1
end if
end while
printSquare(n)
end sub
 
sub printSquare(n)
//Arbitrary limit to fit width of A4 paper
if n < 23 then
print "\n", n, " x ", n, " Magic Square --- ";
print "Magic constant is ", int((n*n+1)/2*n)
for row = 1 to n
for col = 1 to n
print m(row,col) using("####");
next col
print
next row
else
print "Magic Square will not fit on one sheet of paper."
end if
end sub</syntaxhighlight>
{{out}}
<pre>5 x 5 Magic Square --- Magic constant is 65
Line 1,654 ⟶ 2,325:
21 23 32 41 43 3 12
22 31 40 49 2 11 20</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func f n x y .
return (x + y * 2 + 1) mod n
.
numfmt 0 3
proc msqr n . .
for i = 0 to n - 1
for j = 0 to n - 1
write f n (n - j - 1) i * n + f n j i + 1
.
print ""
.
.
msqr 5
</syntaxhighlight>
{{out}}
<pre>
2 23 19 15 6
14 10 1 22 18
21 17 13 9 5
8 4 25 16 12
20 11 7 3 24
</pre>
 
=={{header|EchoLisp}}==
Line 1,735 ⟶ 2,432:
28 2 32 13 36 17 47
10 40 21 44 25 6 29
</pre>
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">
[Magic squares of odd order, for Rosetta Code.
EDSAC program, Initial Orders 2.]
 
[The entries in a magic square of side n can be written as n*u + v + 1,
where u and v range independently over 0, ..., n - 1.
Let the cells be labelled by (x, y) coordinates, where
x = column (left = 0), y = row (bottom = 0).
If n is odd then magic squares can be constructed by setting
u = c*x + d*y + h (mod n)
v = e*x + f*y + k (mod n)
where c, d, e, f, h, k are suitable constants.
Define m = (n - 1)/2. The values of c, ..., k for various methods
of construction are as follows:
c, d, e, f, h, k
Bachet: m + 1, m, m + 1, m + 1, m, 0
De la Loubere: 1, 2m, 2, 2m, m, 0
Conway (lozenge): 1, 2m, 1, 1, m, m + 1
Rosetta Code C: 2m, 2m - 1, 1, 2m - 1, 2m - 1, 2m
------------------------------------------------------------------------------]
[Arrange the storage]
T45K P56F [H parameter: subroutine to print string]
T46K P100F [N parameter: subroutine to print number]
T47K P200F [M parameter: main routine + high-level subroutine]
 
[Main routine + non-library subroutine]
E25K TM GK
[Rows are printed in the order y = n - 1 (top) to y = 0 (bottom).
Row y = n is a fictitious row used during initialization.]
[Locations set up by main routine; some are changed by subroutine]
[0] PF [m]
[1] PF [n]
[2] PF [n^2]
[3] PF [n * 2^11]
[4] PF [c, changed to n*(n - c) = dec to n*u on inc(x)]
[5] PF [d, changed to n*d = dec to n*u on dec(y)]
[6] PF [e, changed to n - e = dec to v on inc(x)]
[7] PF [f = dec to v when dec(y)]
[8] PF [h, changed to n*u for start of current row]
[9] PF [k, changed to v for start of current row]
[Locations used only by subroutine]
[10] PF [n*u]
[11] PF [v]
[12] PF [x count]
[13] PF [y count]
 
[Subroutine to print magic square, using parameters set up by main routine.]
[14] A3F T77@ [plant return link as usual]
A80@ T1F [set to print leading zeros as spaces]
A1@ S6@ T6@ [replace e by n - e]
A1@ S4@ T4@ [replace c by n - c]
[Multiply certain values by n. To maintain the integer scaling,
products have to be shifted 16 left before storing.]
H3@ V8@ [acc := (n << 11)*h]
L8F T8@ [shift 5 more left and store n*h]
V5@ L8F T5@ [similarly n*d]
V4@ L8F T4@ [similarly n*(n - c)]
[Loop round rows y := n - 1 down to 0. At the moment y = n.]
S1@ T13@ [initialize negative count of rows (y values)]
[Start of a row. Here acc = 0]
[36] S1@ T12@ [inititialize negative count of columns (x values)]
A8@ S5@ [decrement n*u by n*d]
E42@ [skip if n*u >= 0]
A2@ [else inc n*u by n^2]
[42] U8@ [store updated n*u for next time]
T10@ [also copy to initialize this row]
A9@ S7@ [decrement v by f]
E48@ [skip if v >= 0]
A1@ [else inc v by n]
[48] U9@ [store updated v at for next time]
U11@ [also copy to initialize this row]
[Next column. Here acc = v]
[50] A10@ A78@ TF [cell value v + n*u + 1 to 0F for printing]
[53] A53@ GN [call subroutine to print cell value]
A12@ A78@ [increment negative column count]
E70@ [jump if row is complete]
T12@ [else update count]
A10@ S4@ [dec n*u by n*n - c)]
E63@ [skip if n*U >= 0]
A2@ [else inc n*u by n^2]
[63] T10@ [store updated n*u for next time]
A11@ S6@ [dec v by n - e]
E68@ [skip if v >= 0]
A1@ [else inc v by n]
[68] U11@ [store updated v, keep v in acc]
E50@ [loop back for next cell in row]
[Row finished]
[70] O81@ O82@ [print CR LF]
A13@ A78@ [inc negative row count]
E77@ [exit if done all rows]
T13@ E36@ [else update count and loop back]
[77] ZF [(planted) jump back to caller]
[Constants]
[78] PD [17-bit 1]
[79] K4096F [null]
[80] !F [space]
[81] @F [carriage return]
[82] &F [line feed]
[83] P10F [for testing number of phone pulses]
[Strings for printing. K2048F sets letters mode; K4096F is EDSAC null.]
[84] K2048FMFAFGFIFCF!FSFQFUFAFRFEF!FOFFF!FOFRFDFEFRF!F#FWF*FMF#FZFQF@F&FK4096F
[117] K2048FDFIFAFLF!FMF!F#FKFPF!F*FTFOF!FCFAFNFCFEFLF#FLF@F&FK4096F
[144] K2048FBFAFCFHFEFTF#FCF@F&FK4096F
[156] K2048FDFEF!FLFAF!FLFOFUFBFEFRFEF#FCF@F&FK4096F
[175] K2048FCFOFNFWFAFYF#FCF@F&FK4096F
[187] K2048FRFOFSFEFTFTFAF!FCFOFDFEF!FCF#FCF@F&FK4096F
 
[Enter with acc = 0]
[207] A207@ GH A84@ [print heading]
[210] A210@ GH A117@ [prompt user to dial m, where n = 2m + 1]
ZF [halt program; restarts when user dials]
[Here acc holds number of pulses in address field.
Number of pulses = 10 if user dialled '0', else = number that user dialled.]
S83@ E292@ [test for '0', jump to exit if so]
A83@ [restore acc after test]
L512F [shift m to top 5 bits for printing]
UF [temp to 0F]
OF O81@ O82@ [print digit m, plus CR LF]
R512F [restore m in address field, same as 2m right-justified]
A78@ U1@ [make and store n = 2m + 1, right justified]
RD T@ [make and store m right-justified]
A1@ L512F T3@ [make and store n << 11]
H3@ V1@ [acc := (n << 11)*n]
L8F [shift 5 left for integer scaling]
T2@ [store n^2]
[Bachet's method]
[234] A234@ GH A144@ [print name of method]
A@ U5@ U8@ [d, h := m]
A78@ U4@ U6@ T7@ [c, e, f := m + 1]
T9@ [k := 0]
[245] A245@ G14@ [call s/r to print square]
[De la Loubere's (miscalled Siamese) method]
[247] A247@ GH A156@ [print name of method]
A78@ U4@ [c := 1]
LD T6@ [e := 2]
A@ U8@ [h := m]
LD U5@ T7@ [d, f := 2m]
T9@ [k := 0 ]
[260] A260@ G14@ [call s/r to print square]
[Conway's lozenge method turns out to be of this type]
[262] A262@ GH A175@ [print name of method]
A78@ U4@ U6@ U7@ [c, e, f := 1]
A@ T9@ [k := m + 1]
A@ U8@ [h := m]
LD T5@ [d := 2m]
[275] A275@ G14@ [call s/r to print square]
[C solution on Rosetta Code website]
[277] A277@ GH A187@ [print name of method]
A78@ T6@ [e := 1]
A@ LD U4@ U9@ [c, k := 2m]
S78@ U5@ U7@ T8@ [d, f, h := 2m - 1]
[290] A290@
G14@ [call s/r to print square]
[292] O79@ [done; print null to flush teleprinter buffer]
ZF [halt the machine]
E25K TH
[Subroutine to print a string.
Input: A order for first character must follow subroutine call (G order).
String is terminated with EDSAC null, which is sent to the teleprinter.]
GKA18@U17@S19@T4@AFT6@AFUFOFE12@A20@G16@TFA6@A2FG5@TFZFU3FU1FK2048F
 
E25K TN
[Subroutine to print non-negative 17-bit integer.
Parameters: 0F = integer to be printed (not preserved)
1F = character for leading zero (preserved)
Workspace: 4F..7F, 38 locations]
GKA3FT34@A1FT7FS35@T6FT4#FAFT4FH36@V4FRDA4#FR1024FH37@E23@O7FA2F
T6FT5FV4#FYFL8FT4#FA5FL1024FUFA6FG16@OFTFT7FA6FG17@ZFP4FZ219DTF
 
[================ M parameter again ================]
E25K TM GK
E207Z [define entry point]
PF [acc = 0 on entry]
</syntaxhighlight>
{{out}}
<pre>
MAGIC SQUARE OF ORDER 2M+1
DIAL M (0 TO CANCEL)
2
BACHET:
3 16 9 22 15
20 8 21 14 2
7 25 13 1 19
24 12 5 18 6
11 4 17 10 23
DE LA LOUBERE:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
CONWAY:
18 24 5 6 12
22 3 9 15 16
1 7 13 19 25
10 11 17 23 4
14 20 21 2 8
ROSETTA CODE C:
2 23 19 15 6
14 10 1 22 18
21 17 13 9 5
8 4 25 16 12
20 11 7 3 24
</pre>
 
Line 2,001 ⟶ 2,904:
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104
Magic number = 1695</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' 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</syntaxhighlight>
{{out}}
<pre>Odd magic square size: 5 * 5 Odd magic square size: 11 * 11
The magic sum = 65 The magic sum = 671
17 24 1 8 15 68 81 94 107 120 1 14 27 40 53 66
23 5 7 14 16 80 93 106 119 11 13 26 39 52 65 67
4 6 13 20 22 92 105 118 10 12 25 38 51 64 77 79
10 12 19 21 3 104 117 9 22 24 37 50 63 76 78 91
11 18 25 2 9 116 8 21 23 36 49 62 75 88 90 103
7 20 33 35 48 61 74 87 89 102 115
19 32 34 47 60 73 86 99 101 114 6
31 44 46 59 72 85 98 100 113 5 18
43 45 58 71 84 97 110 112 4 17 30
55 57 70 83 96 109 111 3 16 29 42
Only the first 2 square shown. 56 69 82 95 108 121 2 15 28 41 54</pre>
 
=={{header|Frink}}==
Line 3,162 ⟶ 3,954:
Sample input/output:
{{out}}
<pre>Enter the order of the magic square : 9
<pre>
Enter the order of the magic square : 9
 
2 75 67 59 51 43 35 27 10
Line 3,175 ⟶ 3,966:
72 55 47 39 31 23 15 7 80
 
The magic constant is 369</pre>
</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
Dim m(1,1)
 
Call magicSquare 5
Call magicSquare 17
 
End
 
Sub magicSquare n
ReDim m(n,n)
inc = 1
count = 1
row = 1
col=(n+1)/2
While count <= n*n
m(row,col) = count
count = count + 1
If inc < n Then
inc = inc + 1
row = row - 1
col = col + 1
If row <> 0 Then
If col > n Then col = 1
Else
row = n
End If
Else
inc = 1
row = row + 1
End If
Wend
Call printSquare n
End Sub
 
Sub printSquare n
'Arbitrary limit to fit width of A4 paper
If n < 23 Then
Print n;" x ";n;" Magic Square --- ";
Print "Magic constant is ";Int((n*n+1)/2*n)
For row = 1 To n
For col = 1 To n
Print Using("####",m(row,col));
Next col
Print
Print
Next row
Else
Notice "Magic Square will not fit on one sheet of paper."
End If
End Sub
</syntaxhighlight>
{{Out}}
<pre>
5 x 5 Magic Square --- Magic constant is 65
17 24 1 8 15
 
23 5 7 14 16
 
4 6 13 20 22
 
10 12 19 21 3
 
11 18 25 2 9
 
17 x 17 Magic Square --- Magic constant is 2465
155 174 193 212 231 250 269 288 1 20 39 58 77 96 115 134 153
 
173 192 211 230 249 268 287 17 19 38 57 76 95 114 133 152 154
 
191 210 229 248 267 286 16 18 37 56 75 94 113 132 151 170 172
 
209 228 247 266 285 15 34 36 55 74 93 112 131 150 169 171 190
 
227 246 265 284 14 33 35 54 73 92 111 130 149 168 187 189 208
 
245 264 283 13 32 51 53 72 91 110 129 148 167 186 188 207 226
 
263 282 12 31 50 52 71 90 109 128 147 166 185 204 206 225 244
 
281 11 30 49 68 70 89 108 127 146 165 184 203 205 224 243 262
 
10 29 48 67 69 88 107 126 145 164 183 202 221 223 242 261 280
 
28 47 66 85 87 106 125 144 163 182 201 220 222 241 260 279 9
 
46 65 84 86 105 124 143 162 181 200 219 238 240 259 278 8 27
 
64 83 102 104 123 142 161 180 199 218 237 239 258 277 7 26 45
 
82 101 103 122 141 160 179 198 217 236 255 257 276 6 25 44 63
 
100 119 121 140 159 178 197 216 235 254 256 275 5 24 43 62 81
 
118 120 139 158 177 196 215 234 253 272 274 4 23 42 61 80 99
 
136 138 157 176 195 214 233 252 271 273 3 22 41 60 79 98 117
 
137 156 175 194 213 232 251 270 289 2 21 40 59 78 97 116 135
</pre>
 
=={{header|Lua}}==
Line 3,950 ⟶ 4,639:
20 11 2 49 40 31 22
The magic number is 175</pre>
 
=={{header|PureBasic}}==
{{trans|Pascal}}
<syntaxhighlight lang="purebasic">#N=9
Define.i i,j
 
If OpenConsole("Magic squares")
PrintN("The square order is: "+Str(#N))
For i=1 To #N
For j=1 To #N
Print(RSet(Str((i*2-j+#N-1) % #N*#N + (i*2+j-2) % #N+1),5))
Next
PrintN("")
Next
PrintN("The magic number is: "+Str(#N*(#N*#N+1)/2))
EndIf
Input()</syntaxhighlight>
{{out}}
<pre>
The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369
</pre>
 
=={{header|Python}}==
Line 4,245 ⟶ 4,903:
21 23 32 41 43 3 12
22 31 40 49 2 11 20</pre>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">_Title "Magic Squares of Odd Order"
'$Dynamic
DefLng A-Z
Dim Shared As Long m(1, 1)
 
Call magicSquare(5)
Call magicSquare(15)
 
Sleep
System
 
Sub magicSquare (n As Integer)
Dim As Integer inc, count, row, col
 
If (n < 3) Or (n And 1) <> 1 Then n = 3
ReDim m(n, n)
inc = 1
count = 1
row = 1
col = (n + 1) / 2
While count <= n * n
m(row, col) = count
count = count + 1
If inc < n Then
inc = inc + 1
row = row - 1
col = col + 1
If row <> 0 Then
If col > n Then col = 1
Else
row = n
End If
Else
inc = 1
row = row + 1
End If
Wend
Call printSquare(n)
End Sub
 
Sub printSquare (n As Integer)
Dim As Integer row, col
'Arbitrary limit ensures a fit within console window
'Can be any size that fits within your computers memory limits
If n < 21 Then
Print "Order "; n; " Magic Square constant is "; Str$(Int((n * n + 1) / 2 * n))
For row = 1 To n
For col = 1 To n
Print Using "####"; m(row, col);
Next col
Print
' Print
Next row
End If
End Sub</syntaxhighlight>
{{Out}}
<pre>Order 5 Magic Square constant is 65
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Order 15 Magic Square constant is 1695
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120
138 155 172 189 206 223 15 17 34 51 68 85 102 119 121
154 171 188 205 222 14 16 33 50 67 84 101 118 135 137
170 187 204 221 13 30 32 49 66 83 100 117 134 136 153
186 203 220 12 29 31 48 65 82 99 116 133 150 152 169
202 219 11 28 45 47 64 81 98 115 132 149 151 168 185
218 10 27 44 46 63 80 97 114 131 148 165 167 184 201
9 26 43 60 62 79 96 113 130 147 164 166 183 200 217
25 42 59 61 78 95 112 129 146 163 180 182 199 216 8
41 58 75 77 94 111 128 145 162 179 181 198 215 7 24
57 74 76 93 110 127 144 161 178 195 197 214 6 23 40
73 90 92 109 126 143 160 177 194 196 213 5 22 39 56
89 91 108 125 142 159 176 193 210 212 4 21 38 55 72
105 107 124 141 158 175 192 209 211 3 20 37 54 71 88
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104
</pre>
 
=={{header|R}}==
 
See [http://rosettacode.org/wiki/Magic_squares_of_doubly_even_order#R here] for the solution for all three cases.
 
Line 4,535 ⟶ 5,111:
 
the magic number is : 369
</pre>
 
=={{header|RPL}}==
{{trans|IS-BASIC}}
≪ → n
≪ n DUP 2 →LIST 0 CON
1 n '''FOR''' j
1 n '''FOR''' k
j k 2 →LIST
j 2 * k - n + 1 - n MOD n *
j 2 * k + 2 - n MOD 1 +
+ PUT
'''NEXT NEXT'''
n DUP SQ 1 + * 2 /
≫ ≫ '<span style="color:blue">ODDMAGIC</span>' STO
 
5 <span style="color:blue">ODDMAGIC</span>
{{out}}
<pre>
2: [[2 23 19 15 6]
[14 10 1 22 18]
[21 17 13 9 5]
[8 4 25 16 12]
[20 11 7 3 24]]
1: 65
</pre>
 
Line 4,736 ⟶ 5,337:
<syntaxhighlight lang="ruby">func magic_square(n {.is_pos && .is_odd}) {
var i = 0
var j = intidiv(n/, 2)
 
var magic_square = []
Line 4,761 ⟶ 5,362:
}
 
var(n=5) = ARGV»to_i»()»...
var sq = magic_square(n)
print_square(sq)
Line 5,025 ⟶ 5,626:
</pre>
 
==={{header|TI-83 BASIC}}===
{{trans|C}}
{{works with|TI-83 BASIC|TI-84Plus 2.55MP}}
Line 5,048 ⟶ 5,649:
[72 55 47 39 31 23 15 7 80]]
</pre>
 
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="text">' ------=< MAIN >=------
Proc _magicsq(5)
Proc _magicsq(11)
End
 
_magicsq Param (1) Local (4)
 
' reset the array
For b@ = 0 to 255
@(b@) = 0
Next
 
If ((a@ % 2) = 0) + (a@ < 3) + (a@ > 15) Then
Print "error: size is not odd or size is smaller then 3 or bigger than 15"
Return
EndIf
 
' start in the middle of the first row
b@ = 1
c@ = a@ - (a@ / 2)
d@ = 1
e@ = a@ * a@
 
' main loop for creating magic square
Do
If @(c@*a@+d@) = 0 Then
@(c@*a@+d@) = b@
If (b@ % a@) = 0 Then
d@ = d@ + 1
Else
c@ = c@ + 1
d@ = d@ - 1
EndIf
b@ = b@ + 1
EndIf
If c@ > a@ Then
c@ = 1
Do While @(c@*a@+d@) # 0
c@ = c@ + 1
Loop
EndIf
If d@ < 1 Then
d@ = a@
Do While @(c@*a@+d@) # 0
d@ = d@ - 1
Loop
EndIf
Until b@ > e@
Loop
 
Print "Odd magic square size: "; a@; " * "; a@
Print "The magic sum = "; ((e@+1) / 2) * a@
Print
 
For d@ = 1 To a@
For c@ = 1 To a@
Print Using "____"; @(c@*a@+d@);
Next
Print
Next
Print
Return
</syntaxhighlight>
{{out}}
<pre>Odd magic square size: 5 * 5
The magic sum = 65
 
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
 
Odd magic square size: 11 * 11
The magic sum = 671
 
68 81 94 107 120 1 14 27 40 53 66
80 93 106 119 11 13 26 39 52 65 67
92 105 118 10 12 25 38 51 64 77 79
104 117 9 22 24 37 50 63 76 78 91
116 8 21 23 36 49 62 75 88 90 103
7 20 33 35 48 61 74 87 89 102 115
19 32 34 47 60 73 86 99 101 114 6
31 44 46 59 72 85 98 100 113 5 18
43 45 58 71 84 97 110 112 4 17 30
55 57 70 83 96 109 111 3 16 29 42
56 69 82 95 108 121 2 15 28 41 54
 
 
0 OK, 0:64
</pre>
 
=={{header|VBA}}==
{{trans|C}}
Works with Excel VBA.
<syntaxhighlight lang="vb">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i As Integer, j As Integer, v As Integer
Debug.Print "The square order is: " & n
For i = 1 To n
For j = 1 To n
Cells(i, j) = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Next j
Next i
Debug.Print "The magic number of"; n; "x"; n; "square is:"; n * (n * n + 1) \ 2
End Sub 'magicsquare
</syntaxhighlight>
 
=={{header|VBScript}}==
Line 5,202 ⟶ 5,691:
End Sub
 
magic_square(5)</syntaxhighlight>
</syntaxhighlight>
 
{{Out}}
<pre>17 24 1 8 15
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9</pre>
</pre>
 
=={{header|Visual Basic}}==
{{trans|C}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i As Integer, j As Integer, v As Integer
Debug.Print "The square order is: " & n
For i = 1 To n
For j = 1 To n
v = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Debug.Print Right(Space(5) & v, 5);
Next j
Debug.Print
Next i
Debug.Print "The magic number is: " & n * (n * n + 1) \ 2
End Sub 'magicsquare
</syntaxhighlight>
{{out}}
<pre>
The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369
</pre>
 
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<syntaxhighlight lang="vbnet">Sub magicsquare()
'Magic squares of odd order
Const n = 9
Dim i, j, v As Integer
Console.WriteLine("The square order is: " & n)
For i = 1 To n
For j = 1 To n
v = ((i * 2 - j + n - 1) Mod n) * n + ((i * 2 + j - 2) Mod n) + 1
Console.Write(" " & Right(Space(5) & v, 5))
Next j
Console.WriteLine("")
Next i
Console.WriteLine("The magic number is: " & n * (n * n + 1) \ 2)
End Sub 'magicsquare</syntaxhighlight>
{{out}}
<pre>
The square order is: 9
2 75 67 59 51 43 35 27 10
22 14 6 79 71 63 46 38 30
42 34 26 18 1 74 66 58 50
62 54 37 29 21 13 5 78 70
73 65 57 49 41 33 25 17 9
12 4 77 69 61 53 45 28 20
32 24 16 8 81 64 56 48 40
52 44 36 19 11 3 76 68 60
72 55 47 39 31 23 15 7 80
The magic number is: 369
</pre>
 
=={{header|VTL-2}}==
Line 5,326 ⟶ 5,747:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var ms = Fn.new { |n|
Line 5,353 ⟶ 5,774:
var m = res[1]
for (i in 0...n) {
for (j in 0...n) SystemFmt.write(Fmt.d(4"$4d", m[i*n+j]))
System.print()
}
Line 5,368 ⟶ 5,789:
Magic number : 65
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \Construct a magic square of odd order - as a procedure can't return an
\ array, the caller must supply one that is big enough.
function MagicSquare( Square, Order );
integer Square, Order;
integer Row, Col, I, J;
 
\Ensure a row/col position is on the square
function InSquare; int Pos ;
return if Pos < 1 then Order else if Pos > Order then 1 else Pos;
 
\move "up" a row in the square
function Up; int Row; return InSquare( Row - 1 );
 
\move "across right" in the square
function Right; int Col ; return InSquare( Col + 1 );
 
if (Order&1) = 0 or Order < 1 then begin
\can't make a magic square of the specified order
return false
end
else begin
\Order is OK - construct the square using de la Loubere's
\ algorithm as in the Wikipedia page
 
\initialise square
for I := 1 to Order do for J := 1 to Order do Square( I, J ) := 0;
 
\initial position is the middle of the top row
Col := ( Order + 1 ) / 2;
Row := 1;
\construct square
for I := 1 to ( Order * Order ) do begin
Square( Row, Col ) := I;
if Square( Up( Row ), Right( Col ) ) # 0 then begin
\the up/right position is already taken, move down
Row := Row + 1;
end
else begin
\can move up/right
Row := Up( Row );
Col := Right( Col );
end
end; \for_i
\sucessful result
return true
end; \magicSquare
 
\prints the magic square
procedure PrintSquare( Square, Order );
integer Square, Order;
integer Sum, W, I_W, I, J;
begin
\set integer width to accomodate the largest number in the square
W := ( Order * Order ) / 10;
I_W := 1;
while W > 0 do begin I_W := I_W + 1; W := W / 10 end;
Format(I_W+1, 0);
Sum:= 0;
for I := 1 to Order do Sum := Sum + Square( 1, I );
Text(0, "maqic square of order "); IntOut(0, Order);
Text(0, " : Sum: "); IntOut(0, Sum );
for I := 1 to Order do begin
CrLf(0);
RlOut(0, float(Square( I, 1 )) );
for J := 2 to Order do RlOut(0, float(Square( I, J )) )
end; \for_I
CrLf(0);
end; \printSquare
 
\test the magic square generation
integer Sq ( 1+11, 1+11 ), L, I;
begin
L:= [1, 3, 5, 7];
for I := 0 to 3 do begin
if MagicSquare( Sq, L(I) ) then PrintSquare( Sq, L(I) )
else Text(0, "can't generate square^m^j" );
end \for_I
end]</syntaxhighlight>
{{out}}
<pre>maqic square of order 1 : Sum: 1
1
maqic square of order 3 : Sum: 15
8 1 6
3 5 7
4 9 2
maqic square of order 5 : Sum: 65
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
maqic square of order 7 : Sum: 175
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20</pre>
 
=={{header|zkl}}==
Line 5,383 ⟶ 5,906:
});</syntaxhighlight>
{{out}}
<pre>Size 3, magic sum 15
<pre>
Size 3, magic sum 15
8 1 6
3 5 7
Line 5,405 ⟶ 5,927:
20 31 42 53 55 66 77 7 18
30 41 52 63 65 76 6 17 19
40 51 62 64 75 5 16 27 29</pre>
</pre>
1,995

edits