Welcome to part 6, where the magic squares are 7×7. 7 was the number of known planets in medieval times, as well as the number of known elements, and the number of days in the week. The rest is all alchemy.
The algorithm for a 7×7 matrix is the same as for 5×5. In fact, it should work for any odd prime-ordered matrix. Do the first based on a random sequence of the numbers between 1 and 7:
1 | 6 | 2 | 3 | 4 | 7 | 5 |
7 | 5 | 1 | 6 | 2 | 3 | 4 |
3 | 4 | 7 | 5 | 1 | 6 | 2 |
6 | 2 | 3 | 4 | 7 | 5 | 1 |
5 | 1 | 6 | 2 | 3 | 4 | 7 |
4 | 7 | 5 | 1 | 6 | 2 | 3 |
2 | 3 | 4 | 7 | 5 | 1 | 6 |
Now, do the second matrix using 0 and every seventh number after it up to 42:
7 | 42 | 0 | 21 | 14 | 35 | 28 |
0 | 21 | 14 | 35 | 28 | 7 | 42 |
14 | 35 | 28 | 7 | 42 | 0 | 21 |
28 | 7 | 42 | 0 | 21 | 14 | 35 |
42 | 0 | 21 | 14 | 35 | 28 | 7 |
21 | 14 | 35 | 28 | 7 | 42 | 0 |
35 | 28 | 7 | 42 | 0 | 21 | 14 |
The resulting matrix is magic, but not “hyper-magic” like the 5x5s were:
8 | 48 | 2 | 24 | 18 | 42 | 33 |
7 | 26 | 15 | 41 | 30 | 10 | 46 |
17 | 39 | 35 | 12 | 43 | 6 | 23 |
34 | 9 | 45 | 4 | 28 | 19 | 36 |
47 | 1 | 27 | 16 | 38 | 32 | 14 |
25 | 21 | 40 | 29 | 13 | 44 | 3 |
37 | 31 | 11 | 49 | 5 | 22 | 20 |
The matrix above was done using Excel, and checked using VBA, where I checked it for “magic”. The loss of hypermagic is unfortunate, but I suppose inevitable when you scale up. There are (7!)2 = 25,401,660 magic squares possible by this algorithm. The VBA proved handy later when I used dynamic programming to produce any odd-ordered magic square of any size, and investigated the results.
What if I made a mistake and got the shifting wrong, such as shifting from the second number from the left instead of the right? The result is variable; full magic can only be restored if the rightward diagonal is all 4/s:
7 | 3 | 6 | 2 | 5 | 1 | 4 |
3 | 6 | 2 | 5 | 1 | 4 | 7 |
6 | 2 | 5 | 1 | 4 | 7 | 3 |
2 | 5 | 1 | 4 | 7 | 3 | 6 |
5 | 1 | 4 | 7 | 3 | 6 | 2 |
1 | 4 | 7 | 3 | 6 | 2 | 5 |
4 | 7 | 3 | 6 | 2 | 5 | 1 |
Why does this work? This is because the sum of the numbers 1 to 7 in any order is 28. Notice that whatever number is in the opposite diagonal is repeated 7 times. There is only one number, when multiplied by 7, equals 28, and that is 4. All other numbers will result in a loss of magic. This appears to be a way to maintain magic in all odd-ordered magic squares past order 7. This can possibly generalize for squares of prime order n by taking the middle number (n+1)/2 and making it the main diagonal.
When added to the second preliminary square, both diagonals became magic, as well as the columns and rows. But because the opposite diagonal consists of all 4’s, we lose variety with this restriction, but not by much: 7!6! = 3,628,800. Still enough to occupy you on a rainy day.
9×9 magic squares won’t work so well, because the second square is shifted by 3, which is a number that divides 9. This will result in certain rows repeating in one of the pre-squares; and the result generally is that one of the diagonals won’t total the magic number (369 in this case).
13 | 66 | 78 | 59 | 27 | 8 | 34 | 38 | 46 |
74 | 55 | 22 | 3 | 33 | 41 | 54 | 17 | 70 |
26 | 7 | 29 | 37 | 49 | 12 | 69 | 77 | 63 |
32 | 45 | 53 | 16 | 65 | 73 | 58 | 21 | 6 |
48 | 15 | 68 | 81 | 62 | 25 | 2 | 28 | 40 |
64 | 76 | 57 | 24 | 5 | 36 | 44 | 52 | 11 |
61 | 20 | 1 | 31 | 39 | 51 | 14 | 72 | 80 |
9 | 35 | 43 | 47 | 10 | 67 | 75 | 60 | 23 |
42 | 50 | 18 | 71 | 79 | 56 | 19 | 4 | 30 |
This is a consequence of 9 not being a prime number. The algorithm that works so well for 5×5 and 7×7 magic squares breaks down when the odd number is composite. Thus, the algorithm is known to work for 11×11, 13×13, 17×17, and has been tried all the way to 101×101 and beyond on my Excel spreadsheet.
The magic number can be known in advance of any magic square because an order-n magic square obeys the formula (n × (n2 + 1))/2. The best bet is to look for odd-ordered n × n squares where n is prime. In my VBA program, I made double sure by sticking to magic squares where the order is prime. Here is an 11 × 11 square, where the magic number is 671:
112 | 20 | 60 | 87 | 3 | 41 | 92 | 33 | 105 | 51 | 67 |
62 | 78 | 2 | 42 | 93 | 32 | 102 | 52 | 70 | 121 | 17 |
11 | 39 | 95 | 23 | 101 | 53 | 71 | 120 | 14 | 63 | 81 |
96 | 26 | 110 | 50 | 73 | 111 | 13 | 64 | 82 | 10 | 36 |
109 | 47 | 74 | 114 | 22 | 61 | 84 | 1 | 35 | 97 | 27 |
75 | 115 | 21 | 58 | 85 | 4 | 44 | 94 | 29 | 100 | 46 |
12 | 57 | 86 | 5 | 43 | 91 | 30 | 103 | 55 | 72 | 117 |
83 | 7 | 34 | 90 | 31 | 104 | 54 | 69 | 118 | 15 | 66 |
37 | 99 | 28 | 106 | 45 | 68 | 119 | 16 | 65 | 80 | 8 |
25 | 107 | 48 | 77 | 116 | 18 | 56 | 79 | 9 | 38 | 98 |
49 | 76 | 113 | 19 | 59 | 88 | 6 | 40 | 89 | 24 | 108 |
Finally, to stretch things out to the boldly absurd, what about an order-23 square whose magic number is 6095?
92 | 369 | 279 | 275 | 108 | 466 | 226 | 403 | 125 | 327 | 360 | 316 | 55 | 457 | 485 | 14 | 192 | 182 | 234 | 425 | 36 | 145 | 524 |
283 | 271 | 115 | 461 | 210 | 413 | 131 | 328 | 364 | 311 | 56 | 442 | 498 | 17 | 193 | 181 | 232 | 428 | 31 | 159 | 510 | 80 | 381 |
103 | 473 | 214 | 409 | 138 | 323 | 348 | 321 | 62 | 443 | 502 | 12 | 194 | 166 | 245 | 431 | 32 | 158 | 508 | 83 | 376 | 297 | 257 |
228 | 395 | 126 | 335 | 352 | 317 | 69 | 438 | 486 | 22 | 200 | 167 | 249 | 426 | 33 | 143 | 521 | 86 | 377 | 296 | 255 | 106 | 468 |
129 | 330 | 366 | 303 | 57 | 450 | 490 | 18 | 207 | 162 | 233 | 436 | 39 | 144 | 525 | 81 | 378 | 281 | 268 | 109 | 469 | 227 | 393 |
365 | 301 | 60 | 445 | 504 | 4 | 195 | 174 | 237 | 432 | 46 | 139 | 509 | 91 | 384 | 282 | 272 | 104 | 470 | 212 | 406 | 132 | 331 |
63 | 446 | 503 | 2 | 198 | 169 | 251 | 418 | 34 | 151 | 513 | 87 | 391 | 277 | 256 | 114 | 476 | 213 | 410 | 127 | 332 | 350 | 314 |
488 | 15 | 201 | 170 | 250 | 416 | 37 | 146 | 527 | 73 | 379 | 289 | 260 | 110 | 483 | 208 | 394 | 137 | 338 | 351 | 318 | 58 | 447 |
196 | 171 | 235 | 429 | 40 | 147 | 526 | 71 | 382 | 284 | 274 | 96 | 471 | 220 | 398 | 133 | 345 | 346 | 302 | 68 | 453 | 489 | 19 |
236 | 433 | 35 | 148 | 511 | 84 | 385 | 285 | 273 | 94 | 474 | 215 | 412 | 119 | 333 | 358 | 306 | 64 | 460 | 484 | 3 | 206 | 177 |
45 | 154 | 512 | 88 | 380 | 286 | 258 | 107 | 477 | 216 | 411 | 117 | 336 | 353 | 320 | 50 | 448 | 496 | 7 | 202 | 184 | 231 | 417 |
507 | 72 | 390 | 292 | 259 | 111 | 472 | 217 | 396 | 130 | 339 | 354 | 319 | 48 | 451 | 491 | 21 | 188 | 172 | 243 | 421 | 41 | 161 |
386 | 299 | 254 | 95 | 482 | 223 | 397 | 134 | 334 | 355 | 304 | 61 | 454 | 492 | 20 | 186 | 175 | 238 | 435 | 27 | 149 | 519 | 76 |
266 | 99 | 478 | 230 | 392 | 118 | 344 | 361 | 305 | 65 | 449 | 493 | 5 | 199 | 178 | 239 | 434 | 25 | 152 | 514 | 90 | 372 | 287 |
464 | 218 | 404 | 122 | 340 | 368 | 300 | 49 | 459 | 499 | 6 | 203 | 173 | 240 | 419 | 38 | 155 | 515 | 89 | 370 | 290 | 261 | 113 |
399 | 136 | 326 | 356 | 312 | 53 | 455 | 506 | 1 | 187 | 183 | 246 | 420 | 42 | 150 | 516 | 74 | 383 | 293 | 262 | 112 | 462 | 221 |
324 | 359 | 307 | 67 | 441 | 494 | 13 | 191 | 179 | 253 | 415 | 26 | 160 | 522 | 75 | 387 | 288 | 263 | 97 | 475 | 224 | 400 | 135 |
308 | 66 | 439 | 497 | 8 | 205 | 165 | 241 | 427 | 30 | 156 | 529 | 70 | 371 | 298 | 269 | 98 | 479 | 219 | 401 | 120 | 337 | 362 |
452 | 500 | 9 | 204 | 163 | 244 | 422 | 44 | 142 | 517 | 82 | 375 | 294 | 276 | 93 | 463 | 229 | 407 | 121 | 341 | 357 | 309 | 51 |
10 | 189 | 176 | 247 | 423 | 43 | 140 | 520 | 77 | 389 | 280 | 264 | 105 | 467 | 225 | 414 | 116 | 325 | 367 | 315 | 52 | 456 | 495 |
180 | 242 | 424 | 28 | 153 | 523 | 78 | 388 | 278 | 267 | 100 | 481 | 211 | 402 | 128 | 329 | 363 | 322 | 47 | 440 | 505 | 16 | 190 |
430 | 29 | 157 | 518 | 79 | 373 | 291 | 270 | 101 | 480 | 209 | 405 | 123 | 343 | 349 | 310 | 59 | 444 | 501 | 23 | 185 | 164 | 252 |
141 | 528 | 85 | 374 | 295 | 265 | 102 | 465 | 222 | 408 | 124 | 342 | 347 | 313 | 54 | 458 | 487 | 11 | 197 | 168 | 248 | 437 | 24 |
That probably ran into the neighbouring columns, but it’s just for illustration. The code, which I have used to generate magic squares of as high an order as 113 (magic number 721,505) is fewer than 170 lines in VBA for Excel 2007. And yes, even the above square, and the order 113 square are magic.