' Generates Order 8 Associated Magic Squares of Subtraction, s8 = 260, Res8 = 32
' Medjig Solutions
' Tested with Office 365 under Windows 10
Sub MgcSqr8a1()
Dim a(64), b1(16), c1(64), b(16, 4), c(16, 4), s(32)
Dim b8(8), Res8(18)
' 4 x 4 Associated Square for corresponding 8 x 8 Magic Squares
b1(1) = 16: b1(2) = 9: b1(3) = 5: b1(4) = 4
b1(5) = 3: b1(6) = 6: b1(7) = 10: b1(8) = 15
b1(9) = 2: b1(10) = 7: b1(11) = 11: b1(12) = 14
b1(13) = 13: b1(14) = 12: b1(15) = 8: b1(16) = 1
y = MsgBox("Locked", vbCritical, "Routine MgcSqr8a1")
End
k1 = 1: k2 = 1: n2 = 0: n3 = 0: n9 = 0
r8 = 32
' Generate data
Sheets("Klad1").Select
t1 = Timer
For j64 = 1 To 4 'a(64)
If b(16, j64) = 0 Then b(16, j64) = j64: c(16, 4) = j64 Else GoTo 640
a(64) = j64 - 1
For j63 = 1 To 4 'a(63)
If b(16, j63) = 0 Then b(16, j63) = j63: c(16, 3) = j63 Else GoTo 630
a(63) = j63 - 1
For j62 = 1 To 4 'a(62)
If b(15, j62) = 0 Then b(15, j62) = j62: c(15, 4) = j62 Else GoTo 620
a(62) = j62 - 1
For j61 = 1 To 4 'a(61)
If b(15, j61) = 0 Then b(15, j61) = j61: c(15, 3) = j61 Else GoTo 610
a(61) = j61 - 1
For j60 = 1 To 4 'a(60)
If b(14, j60) = 0 Then b(14, j60) = j60: c(14, 4) = j60 Else GoTo 600
a(60) = j60 - 1
For j59 = 1 To 4 'a(59)
If b(14, j59) = 0 Then b(14, j59) = j59: c(14, 3) = j59 Else GoTo 590
a(59) = j59 - 1
For j58 = 1 To 4 'a(58)
If b(13, j58) = 0 Then b(13, j58) = j58: c(13, 4) = j58 Else GoTo 580
a(58) = j58 - 1
a(57) = 12 - a(58) - a(59) - a(60) - a(61) - a(62) - a(63) - a(64)
j57 = a(57) + 1
If j57 <= 0 Or j57 > 4 Then GoTo 570
If b(13, j57) = 0 Then b(13, j57) = j57: c(13, 3) = j57 Else GoTo 570
c1(57) = b1(13) + 16 * a(57): c1(58) = b1(13) + 16 * a(58): c1(59) = b1(14) + 16 * a(59): c1(60) = b1(14) + 16 * a(60)
c1(61) = b1(15) + 16 * a(61): c1(62) = b1(15) + 16 * a(62): c1(63) = b1(16) + 16 * a(63): c1(64) = b1(16) + 16 * a(64)
i10 = 8: GoSub 1000: If Res8(i10) <> r8 Then GoTo 565
For j56 = 1 To 4 'a(56)
If b(16, j56) = 0 Then b(16, j56) = j56: c(16, 2) = j56 Else GoTo 560
a(56) = j56 - 1
a(55) = 6 - a(56) - a(63) - a(64)
j55 = a(55) + 1
If j55 <= 0 Or j55 > 4 Then GoTo 550
If b(16, j55) = 0 Then b(16, j55) = j55: c(16, 1) = j55 Else GoTo 550
For j54 = 1 To 4 'a(54)
If b(15, j54) = 0 Then b(15, j54) = j54: c(15, 2) = j54 Else GoTo 540
a(54) = j54 - 1
a(53) = 6 - a(54) - a(61) - a(62)
j53 = a(53) + 1
If j53 <= 0 Or j53 > 4 Then GoTo 530
If b(15, j53) = 0 Then b(15, j53) = j53: c(15, 1) = j53 Else GoTo 530
For j52 = 1 To 4 'a(52)
If b(14, j52) = 0 Then b(14, j52) = j52: c(14, 2) = j52 Else GoTo 520
a(52) = j52 - 1
a(51) = 6 - a(52) - a(59) - a(60)
j51 = a(51) + 1
If j51 <= 0 Or j51 > 4 Then GoTo 510
If b(14, j51) = 0 Then b(14, j51) = j51: c(14, 1) = j51 Else GoTo 510
For j50 = 1 To 4 'a(50)
If b(13, j50) = 0 Then b(13, j50) = j50: c(13, 2) = j50 Else GoTo 500
a(50) = j50 - 1
a(49) = 6 - a(50) - a(57) - a(58)
j49 = a(49) + 1
If j49 <= 0 Or j49 > 4 Then GoTo 490
If b(13, j49) = 0 Then b(13, j49) = j49: c(13, 1) = j49 Else GoTo 490
c1(49) = b1(13) + 16 * a(49): c1(50) = b1(13) + 16 * a(50): c1(51) = b1(14) + 16 * a(51): c1(52) = b1(14) + 16 * a(52)
c1(53) = b1(15) + 16 * a(53): c1(54) = b1(15) + 16 * a(54): c1(55) = b1(16) + 16 * a(55): c1(56) = b1(16) + 16 * a(56)
i10 = 7: GoSub 1000: If Res8(i10) <> r8 Then GoTo 485
For j48 = 1 To 4 'a(48)
If b(12, j48) = 0 Then b(12, j48) = j48: c(12, 4) = j48 Else GoTo 480
a(48) = j48 - 1
For j47 = 1 To 4 'a(47)
If b(12, j47) = 0 Then b(12, j47) = j47: c(12, 3) = j47 Else GoTo 470
a(47) = j47 - 1
For j46 = 1 To 4 'a(46)
If b(11, j46) = 0 Then b(11, j46) = j46: c(11, 4) = j46 Else GoTo 460
a(46) = j46 - 1
For j45 = 1 To 4 'a(45)
If b(11, j45) = 0 Then b(11, j45) = j45: c(11, 3) = j45 Else GoTo 450
a(45) = j45 - 1
For j44 = 1 To 4 'a(44)
If b(10, j44) = 0 Then b(10, j44) = j44: c(10, 4) = j44 Else GoTo 440
a(44) = j44 - 1
For j43 = 1 To 4 'a(43)
If b(10, j43) = 0 Then b(10, j43) = j43: c(10, 3) = j43 Else GoTo 430
a(43) = j43 - 1
For j42 = 1 To 4 'a(42)
If b(9, j42) = 0 Then b(9, j42) = j42: c(9, 4) = j42 Else GoTo 420
a(42) = j42 - 1
a(41) = 12 - a(42) - a(43) - a(44) - a(45) - a(46) - a(47) - a(48)
j41 = a(41) + 1
If j41 <= 0 Or j41 > 4 Then GoTo 410
If b(9, j41) = 0 Then b(9, j41) = j41: c(9, 3) = j41 Else GoTo 410
c1(41) = b1(9) + 16 * a(41): c1(42) = b1(9) + 16 * a(42): c1(43) = b1(10) + 16 * a(43): c1(44) = b1(10) + 16 * a(44)
c1(45) = b1(11) + 16 * a(45): c1(46) = b1(11) + 16 * a(46): c1(47) = b1(12) + 16 * a(47): c1(48) = b1(12) + 16 * a(48)
i10 = 6: GoSub 1000: If Res8(i10) <> r8 Then GoTo 405
For j40 = 1 To 4 'a(40)
If b(12, j40) = 0 Then b(12, j40) = j40: c(12, 2) = j40 Else GoTo 400
a(40) = j40 - 1
a(39) = 6 - a(40) - a(47) - a(48)
j39 = a(39) + 1
If j39 <= 0 Or j39 > 4 Then GoTo 390
If b(12, j39) = 0 Then b(12, j39) = j39: c(12, 1) = j39 Else GoTo 390
For j38 = 1 To 4 'a(38)
If b(11, j38) = 0 Then b(11, j38) = j38: c(11, 2) = j38 Else GoTo 380
a(38) = j38 - 1
a(37) = 6 - a(38) - a(45) - a(46)
j37 = a(37) + 1
If j37 <= 0 Or j37 > 4 Then GoTo 370
If b(11, j37) = 0 Then b(11, j37) = j37: c(11, 1) = j37 Else GoTo 370
a(36) = 12 - a(38) - a(44) - a(46) - a(52) - a(54) - a(60) - a(62)
j36 = a(36) + 1
If j36 <= 0 Or j36 > 4 Then GoTo 360
If b(10, j36) = 0 Then b(10, j36) = j36: c(10, 2) = j36 Else GoTo 360
a(35) = 6 - a(36) - a(43) - a(44)
j35 = a(35) + 1
If j35 <= 0 Or j35 > 4 Then GoTo 350
If b(10, j35) = 0 Then b(10, j35) = j35: c(10, 1) = j35 Else GoTo 350
a(34) = 12 - a(40) - a(42) - a(48) - a(50) - a(56) - a(58) - a(64)
j34 = a(34) + 1
If j34 <= 0 Or j34 > 4 Then GoTo 340
If b(9, j34) = 0 Then b(9, j34) = j34: c(9, 2) = j34 Else GoTo 340
a(33) = 6 - a(34) - a(41) - a(42)
j33 = a(33) + 1
If j33 <= 0 Or j33 > 4 Then GoTo 330
If b(9, j33) = 0 Then b(9, j33) = j33: c(9, 1) = j33 Else GoTo 330
c1(33) = b1(9) + 16 * a(33): c1(34) = b1(9) + 16 * a(34): c1(35) = b1(10) + 16 * a(35): c1(36) = b1(10) + 16 * a(36)
c1(37) = b1(11) + 16 * a(37): c1(38) = b1(11) + 16 * a(38): c1(39) = b1(12) + 16 * a(39): c1(40) = b1(12) + 16 * a(40)
i10 = 5: GoSub 1000: If Res8(i10) <> r8 Then GoTo 5
' Complete Square a()
For i1 = 1 To 32
a(i1) = 3 - a(65 - i1)
Next i1
GoSub 700 'Calculate Magic Squares
' Check Subtractive Columns and Diagonals
For i10 = 9 To 18
GoSub 1000: If Res8(i10) <> r8 Then GoTo 5
Next i10
' n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 650 'Print results (Medjig Squares)
' n9 = n9 + 1: GoSub 650 'Print results (Medjig Squares)
n9 = n9 + 1: GoSub 710 'Print results (Magic Squares)
5
b(9, c(9, 1)) = 0: c(9, 1) = 0
330 b(9, c(9, 2)) = 0: c(9, 2) = 0
340
b(10, c(10, 1)) = 0: c(10, 1) = 0
350 b(10, c(10, 2)) = 0: c(10, 2) = 0
360
b(11, c(11, 1)) = 0: c(11, 1) = 0
370 b(11, c(11, 2)) = 0: c(11, 2) = 0
380 Next j38
b(12, c(12, 1)) = 0: c(12, 1) = 0
390 b(12, c(12, 2)) = 0: c(12, 2) = 0
400 Next j40
405 b(9, c(9, 3)) = 0: c(9, 3) = 0
410 b(9, c(9, 4)) = 0: c(9, 4) = 0
420 Next j42
b(10, c(10, 3)) = 0: c(10, 3) = 0
430 Next j43
b(10, c(10, 4)) = 0: c(10, 4) = 0
440 Next j44
b(11, c(11, 3)) = 0: c(11, 3) = 0
450 Next j45
b(11, c(11, 4)) = 0: c(11, 4) = 0
460 Next j46
b(12, c(12, 3)) = 0: c(12, 3) = 0
470 Next j47
b(12, c(12, 4)) = 0: c(12, 4) = 0
480 Next j48
485 b(13, c(13, 1)) = 0: c(13, 1) = 0
490 b(13, c(13, 2)) = 0: c(13, 2) = 0
500 Next j50
b(14, c(14, 1)) = 0: c(14, 1) = 0
510 b(14, c(14, 2)) = 0: c(14, 2) = 0
520 Next j52
b(15, c(15, 1)) = 0: c(15, 1) = 0
530 b(15, c(15, 2)) = 0: c(15, 2) = 0
540 Next j54
b(16, c(16, 1)) = 0: c(16, 1) = 0
550 b(16, c(16, 2)) = 0: c(16, 2) = 0
560 Next j56
565 b(13, c(13, 3)) = 0: c(13, 3) = 0
570 b(13, c(13, 4)) = 0: c(13, 4) = 0
580 Next j58
b(14, c(14, 3)) = 0: c(14, 3) = 0
590 Next j59
b(14, c(14, 4)) = 0: c(14, 4) = 0
600 Next j60
b(15, c(15, 3)) = 0: c(15, 3) = 0
610 Next j61
b(15, c(15, 4)) = 0: c(15, 4) = 0
620 Next j62
b(16, c(16, 3)) = 0: c(16, 3) = 0
630 Next j63
b(16, c(16, 4)) = 0: c(16, 4) = 0
640 Next j64
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Combinations"
y = MsgBox(t10, 0, "Routine MgcSqr8a1")
End
' Print results (selected numbers)
645 For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (Medjig Squares)
650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Calculate Magic Square
700 c1(1) = b1(1) + 16 * a(1): c1(2) = b1(1) + 16 * a(2): c1(3) = b1(2) + 16 * a(3): c1(4) = b1(2) + 16 * a(4)
c1(5) = b1(3) + 16 * a(5): c1(6) = b1(3) + 16 * a(6): c1(7) = b1(4) + 16 * a(7): c1(8) = b1(4) + 16 * a(8)
c1(9) = b1(1) + 16 * a(9): c1(10) = b1(1) + 16 * a(10): c1(11) = b1(2) + 16 * a(11): c1(12) = b1(2) + 16 * a(12)
c1(13) = b1(3) + 16 * a(13): c1(14) = b1(3) + 16 * a(14): c1(15) = b1(4) + 16 * a(15): c1(16) = b1(4) + 16 * a(16)
c1(17) = b1(5) + 16 * a(17): c1(18) = b1(5) + 16 * a(18): c1(19) = b1(6) + 16 * a(19): c1(20) = b1(6) + 16 * a(20)
c1(21) = b1(7) + 16 * a(21): c1(22) = b1(7) + 16 * a(22): c1(23) = b1(8) + 16 * a(23): c1(24) = b1(8) + 16 * a(24)
c1(25) = b1(5) + 16 * a(25): c1(26) = b1(5) + 16 * a(26): c1(27) = b1(6) + 16 * a(27): c1(28) = b1(6) + 16 * a(28)
c1(29) = b1(7) + 16 * a(29): c1(30) = b1(7) + 16 * a(30): c1(31) = b1(8) + 16 * a(31): c1(32) = b1(8) + 16 * a(32)
c1(33) = b1(9) + 16 * a(33): c1(34) = b1(9) + 16 * a(34): c1(35) = b1(10) + 16 * a(35): c1(36) = b1(10) + 16 * a(36)
c1(37) = b1(11) + 16 * a(37): c1(38) = b1(11) + 16 * a(38): c1(39) = b1(12) + 16 * a(39): c1(40) = b1(12) + 16 * a(40)
c1(41) = b1(9) + 16 * a(41): c1(42) = b1(9) + 16 * a(42): c1(43) = b1(10) + 16 * a(43): c1(44) = b1(10) + 16 * a(44)
c1(45) = b1(11) + 16 * a(45): c1(46) = b1(11) + 16 * a(46): c1(47) = b1(12) + 16 * a(47): c1(48) = b1(12) + 16 * a(48)
c1(49) = b1(13) + 16 * a(49): c1(50) = b1(13) + 16 * a(50): c1(51) = b1(14) + 16 * a(51): c1(52) = b1(14) + 16 * a(52)
c1(53) = b1(15) + 16 * a(53): c1(54) = b1(15) + 16 * a(54): c1(55) = b1(16) + 16 * a(55): c1(56) = b1(16) + 16 * a(56)
c1(57) = b1(13) + 16 * a(57): c1(58) = b1(13) + 16 * a(58): c1(59) = b1(14) + 16 * a(59): c1(60) = b1(14) + 16 * a(60)
c1(61) = b1(15) + 16 * a(61): c1(62) = b1(15) + 16 * a(62): c1(63) = b1(16) + 16 * a(63): c1(64) = b1(16) + 16 * a(64)
Return
' Print Magic Square c1()
710 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1, k2 + 1).Value = n9
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = c1(i3)
Next i2
Next i1
Return
' Determine Residu line i10
1000
Select Case i10
' Rows
Case 1:
b8(1) = c1(1): b8(2) = c1(2): b8(3) = c1(3): b8(4) = c1(4): b8(5) = c1(5): b8(6) = c1(6): b8(7) = c1(7): b8(8) = c1(8):
GoSub 1200
Case 2:
b8(1) = c1(9): b8(2) = c1(10): b8(3) = c1(11): b8(4) = c1(12): b8(5) = c1(13): b8(6) = c1(14): b8(7) = c1(15): b8(8) = c1(16):
GoSub 1200
Case 3:
b8(1) = c1(17): b8(2) = c1(18): b8(3) = c1(19): b8(4) = c1(20): b8(5) = c1(21): b8(6) = c1(22): b8(7) = c1(23): b8(8) = c1(24):
GoSub 1200
Case 4:
b8(1) = c1(25): b8(2) = c1(26): b8(3) = c1(27): b8(4) = c1(28): b8(5) = c1(29): b8(6) = c1(30): b8(7) = c1(31): b8(8) = c1(32):
GoSub 1200
Case 5:
b8(1) = c1(33): b8(2) = c1(34): b8(3) = c1(35): b8(4) = c1(36): b8(5) = c1(37): b8(6) = c1(38): b8(7) = c1(39): b8(8) = c1(40):
GoSub 1200
Case 6:
b8(1) = c1(41): b8(2) = c1(42): b8(3) = c1(43): b8(4) = c1(44): b8(5) = c1(45): b8(6) = c1(46): b8(7) = c1(47): b8(8) = c1(48):
GoSub 1200
Case 7:
b8(1) = c1(49): b8(2) = c1(50): b8(3) = c1(51): b8(4) = c1(52): b8(5) = c1(53): b8(6) = c1(54): b8(7) = c1(55): b8(8) = c1(56):
GoSub 1200
Case 8:
b8(1) = c1(57): b8(2) = c1(58): b8(3) = c1(59): b8(4) = c1(60): b8(5) = c1(61): b8(6) = c1(62): b8(7) = c1(63): b8(8) = c1(64):
GoSub 1200
' Columns
Case 9:
b8(1) = c1(1): b8(2) = c1(9): b8(3) = c1(17): b8(4) = c1(25): b8(5) = c1(33): b8(6) = c1(41): b8(7) = c1(49): b8(8) = c1(57):
GoSub 1200
Case 10:
b8(1) = c1(2): b8(2) = c1(10): b8(3) = c1(18): b8(4) = c1(26): b8(5) = c1(34): b8(6) = c1(42): b8(7) = c1(50): b8(8) = c1(58):
GoSub 1200
Case 11:
b8(1) = c1(3): b8(2) = c1(11): b8(3) = c1(19): b8(4) = c1(27): b8(5) = c1(35): b8(6) = c1(43): b8(7) = c1(51): b8(8) = c1(59):
GoSub 1200
Case 12:
b8(1) = c1(4): b8(2) = c1(12): b8(3) = c1(20): b8(4) = c1(28): b8(5) = c1(36): b8(6) = c1(44): b8(7) = c1(52): b8(8) = c1(60):
GoSub 1200
Case 13:
b8(1) = c1(5): b8(2) = c1(13): b8(3) = c1(21): b8(4) = c1(29): b8(5) = c1(37): b8(6) = c1(45): b8(7) = c1(53): b8(8) = c1(61):
GoSub 1200
Case 14:
b8(1) = c1(6): b8(2) = c1(14): b8(3) = c1(22): b8(4) = c1(30): b8(5) = c1(38): b8(6) = c1(46): b8(7) = c1(54): b8(8) = c1(62):
GoSub 1200
Case 15:
b8(1) = c1(7): b8(2) = c1(15): b8(3) = c1(23): b8(4) = c1(31): b8(5) = c1(39): b8(6) = c1(47): b8(7) = c1(55): b8(8) = c1(63):
GoSub 1200
Case 16:
b8(1) = c1(8): b8(2) = c1(16): b8(3) = c1(24): b8(4) = c1(32): b8(5) = c1(40): b8(6) = c1(48): b8(7) = c1(56): b8(8) = c1(64):
GoSub 1200
' Diagonals
Case 17:
b8(1) = c1(1): b8(2) = c1(10): b8(3) = c1(19): b8(4) = c1(28): b8(5) = c1(37): b8(6) = c1(46): b8(7) = c1(55): b8(8) = c1(64):
GoSub 1200
Case 18:
b8(1) = c1(8): b8(2) = c1(15): b8(3) = c1(22): b8(4) = c1(29): b8(5) = c1(36): b8(6) = c1(43): b8(7) = c1(50): b8(8) = c1(57):
GoSub 1200
End Select
Return
1200
For i2 = 1 To 7
For i1 = i2 To 8
If b8(i2) < b8(i1) Then
x = b8(i1)
b8(i1) = b8(i2)
b8(i2) = x
End If
Next i1
Next i2
Res8(i10) = b8(1) - b8(2) + b8(3) - b8(4) + b8(5) - b8(6) + b8(7) - b8(8)
Return
End Sub