Magic squares of odd order: Difference between revisions
Content added Content deleted
(add PicoLisp) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 34: | Line 34: | ||
* MathWorld™ entry: [http://mathworld.wolfram.com/MagicSquare.html Magic_square] |
* MathWorld™ entry: [http://mathworld.wolfram.com/MagicSquare.html Magic_square] |
||
* [http://www.1728.org/magicsq1.htm Odd Magic Squares (1728.org)]<br><br> |
* [http://www.1728.org/magicsq1.htm Odd Magic Squares (1728.org)]<br><br> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
Line 148: | Line 147: | ||
55 57 70 83 96 109 111 3 16 29 42 |
55 57 70 83 96 109 111 3 16 29 42 |
||
56 69 82 95 108 121 2 15 28 41 54</pre> |
56 69 82 95 108 121 2 15 28 41 54</pre> |
||
=={{header|ALGOL 68}}== |
|||
<lang algol68># 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</lang> |
|||
{{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|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Line 250: | Line 333: | ||
22 31 40 49 2 11 20 |
22 31 40 49 2 11 20 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
|||
<lang algol68># 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</lang> |
|||
{{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|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 809: | Line 807: | ||
The magic number is: 369 |
The magic number is: 369 |
||
Press any key to continue ...</pre> |
Press any key to continue ...</pre> |
||
=={{header|bc}}== |
=={{header|bc}}== |
||
Line 1,238: | Line 1,235: | ||
10 40 21 44 25 6 29 |
10 40 21 44 25 6 29 |
||
</pre> |
</pre> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
Line 1,504: | Line 1,500: | ||
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104 |
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104 |
||
Magic number = 1695</pre> |
Magic number = 1695</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<lang FreeBASIC>' version 23-06-2015 |
<lang FreeBASIC>' version 23-06-2015 |
||
Line 2,021: | Line 2,018: | ||
Magic constant: 65 </pre> |
Magic constant: 65 </pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 2,893: | Line 2,889: | ||
Magic constant is : 65 |
Magic constant is : 65 |
||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
|||
{{trans|Perl}} |
|||
The index-fiddling differs from Perl since GP vectors start at 1. |
|||
<lang parigp>magicSquare(n)={ |
|||
my(M=matrix(n,n),j=n\2+1,i=1); |
|||
for(l=1,n^2, |
|||
M[i,j]=l; |
|||
if(M[(i-2)%n+1,j%n+1], |
|||
i=i%n+1 |
|||
, |
|||
i=(i-2)%n+1; |
|||
j=j%n+1 |
|||
) |
|||
); |
|||
M; |
|||
} |
|||
magicSquare(7)</lang> |
|||
{{out}} |
|||
<pre>[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|Pascal}}== |
=={{header|Pascal}}== |
||
Line 3,043: | Line 3,071: | ||
TRUE</pre> |
TRUE</pre> |
||
=={{header|PARI/GP}}== |
|||
{{trans|Perl}} |
|||
The index-fiddling differs from Perl since GP vectors start at 1. |
|||
<lang parigp>magicSquare(n)={ |
|||
my(M=matrix(n,n),j=n\2+1,i=1); |
|||
for(l=1,n^2, |
|||
M[i,j]=l; |
|||
if(M[(i-2)%n+1,j%n+1], |
|||
i=i%n+1 |
|||
, |
|||
i=(i-2)%n+1; |
|||
j=j%n+1 |
|||
) |
|||
); |
|||
M; |
|||
} |
|||
magicSquare(7)</lang> |
|||
{{out}} |
|||
<pre>[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|Perl}}== |
=={{header|Perl}}== |
||
Line 3,080: | Line 3,076: | ||
See [[Magic_squares/Perl|Magic squares/Perl]] for a general magic square generator. |
See [[Magic_squares/Perl|Magic squares/Perl]] for a general magic square generator. |
||
<lang perl></lang> |
<lang perl></lang> |
||
=={{header|Perl 6}}== |
|||
See [[Magic_squares/Perl_6|Magic squares/Perl 6]] for a general magic square generator. |
|||
{{out}} |
|||
With a parameter of 5: |
|||
<pre>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 |
|||
The magic number is 65</pre> |
|||
With a parameter of 19: |
|||
<pre>192 213 234 255 276 297 318 339 360 1 22 43 64 85 106 127 148 169 190 |
|||
212 233 254 275 296 317 338 359 19 21 42 63 84 105 126 147 168 189 191 |
|||
232 253 274 295 316 337 358 18 20 41 62 83 104 125 146 167 188 209 211 |
|||
252 273 294 315 336 357 17 38 40 61 82 103 124 145 166 187 208 210 231 |
|||
272 293 314 335 356 16 37 39 60 81 102 123 144 165 186 207 228 230 251 |
|||
292 313 334 355 15 36 57 59 80 101 122 143 164 185 206 227 229 250 271 |
|||
312 333 354 14 35 56 58 79 100 121 142 163 184 205 226 247 249 270 291 |
|||
332 353 13 34 55 76 78 99 120 141 162 183 204 225 246 248 269 290 311 |
|||
352 12 33 54 75 77 98 119 140 161 182 203 224 245 266 268 289 310 331 |
|||
11 32 53 74 95 97 118 139 160 181 202 223 244 265 267 288 309 330 351 |
|||
31 52 73 94 96 117 138 159 180 201 222 243 264 285 287 308 329 350 10 |
|||
51 72 93 114 116 137 158 179 200 221 242 263 284 286 307 328 349 9 30 |
|||
71 92 113 115 136 157 178 199 220 241 262 283 304 306 327 348 8 29 50 |
|||
91 112 133 135 156 177 198 219 240 261 282 303 305 326 347 7 28 49 70 |
|||
111 132 134 155 176 197 218 239 260 281 302 323 325 346 6 27 48 69 90 |
|||
131 152 154 175 196 217 238 259 280 301 322 324 345 5 26 47 68 89 110 |
|||
151 153 174 195 216 237 258 279 300 321 342 344 4 25 46 67 88 109 130 |
|||
171 173 194 215 236 257 278 299 320 341 343 3 24 45 66 87 108 129 150 |
|||
172 193 214 235 256 277 298 319 340 361 2 23 44 65 86 107 128 149 170 |
|||
The magic number is 3439</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,651: | Line 3,612: | ||
37 48 59 70 81 2 13 24 35 |
37 48 59 70 81 2 13 24 35 |
||
Magic Number:369</pre> |
Magic Number:369</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
See [[Magic_squares/Perl_6|Magic squares/Perl 6]] for a general magic square generator. |
|||
{{out}} |
|||
With a parameter of 5: |
|||
<pre>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 |
|||
The magic number is 65</pre> |
|||
With a parameter of 19: |
|||
<pre>192 213 234 255 276 297 318 339 360 1 22 43 64 85 106 127 148 169 190 |
|||
212 233 254 275 296 317 338 359 19 21 42 63 84 105 126 147 168 189 191 |
|||
232 253 274 295 316 337 358 18 20 41 62 83 104 125 146 167 188 209 211 |
|||
252 273 294 315 336 357 17 38 40 61 82 103 124 145 166 187 208 210 231 |
|||
272 293 314 335 356 16 37 39 60 81 102 123 144 165 186 207 228 230 251 |
|||
292 313 334 355 15 36 57 59 80 101 122 143 164 185 206 227 229 250 271 |
|||
312 333 354 14 35 56 58 79 100 121 142 163 184 205 226 247 249 270 291 |
|||
332 353 13 34 55 76 78 99 120 141 162 183 204 225 246 248 269 290 311 |
|||
352 12 33 54 75 77 98 119 140 161 182 203 224 245 266 268 289 310 331 |
|||
11 32 53 74 95 97 118 139 160 181 202 223 244 265 267 288 309 330 351 |
|||
31 52 73 94 96 117 138 159 180 201 222 243 264 285 287 308 329 350 10 |
|||
51 72 93 114 116 137 158 179 200 221 242 263 284 286 307 328 349 9 30 |
|||
71 92 113 115 136 157 178 199 220 241 262 283 304 306 327 348 8 29 50 |
|||
91 112 133 135 156 177 198 219 240 261 282 303 305 326 347 7 28 49 70 |
|||
111 132 134 155 176 197 218 239 260 281 302 323 325 346 6 27 48 69 90 |
|||
131 152 154 175 196 217 238 259 280 301 322 324 345 5 26 47 68 89 110 |
|||
151 153 174 195 216 237 258 279 300 321 342 344 4 25 46 67 88 109 130 |
|||
171 173 194 215 236 257 278 299 320 341 343 3 24 45 66 87 108 129 150 |
|||
172 193 214 235 256 277 298 319 340 361 2 23 44 65 86 107 128 149 170 |
|||
The magic number is 3439</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,990: | Line 3,987: | ||
The magic number is: 65 |
The magic number is: 65 |
||
</pre> |
</pre> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
Line 4,372: | Line 4,370: | ||
End Sub 'magicsquare |
End Sub 'magicsquare |
||
</lang> |
</lang> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Line 4,459: | Line 4,456: | ||
The magic number is: 369 |
The magic number is: 369 |
||
</pre> |
</pre> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |