' Constructs 12 x 12 Primme Number Associated Magic Squares composed of
' 8 Semi Magic Anti Symmetric Squares and
' 8 Semi Magic Anti Symmetric Complementar Squares
' Tested with Office 365 under Windows 10
Sub Composed12()
Dim a1(1200), b1(49803), a(9), b(49803), c(9), a12(144)
y = MsgBox("Locked", vbCritical, "Routine Composed12")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
Sheets("Klad1").Select
t1 = Timer
For j101 = 2 To 50
j100 = Cells(j101, 22).Value 'Read Achievable Pair Collections
n10 = 0
GoSub 3010 'Read Prime Numbers From Sheet ShtNm1
If nVar < 144 Then GoTo 1000
' Generate Semi Magic Squares
For j9 = m1 To m2 'a(9)
If b1(a1(j9)) = 0 Then GoTo 1090
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 1090
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b1(a1(j8)) = 0 Then GoTo 1080
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 1080
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 1070:
If b1(a(7)) = 0 Then GoTo 1070
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 1070
For j6 = m1 To m2 'a(6)
If b1(a1(j6)) = 0 Then GoTo 1060
If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 1060
a(6) = a1(j6)
a(5) = -s1 + a(6) + a(8) + 2 * a(9)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 1050:
If b1(a(5)) = 0 Then GoTo 1050
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 1050
a(4) = 2 * s1 - 2 * a(6) - a(8) - 2 * a(9)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 1040:
If b1(a(4)) = 0 Then GoTo 1040
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 1040
a(3) = s1 - a(6) - a(9)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 1030:
If b1(a(3)) = 0 Then GoTo 1030
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 1030
a(2) = 2 * s1 - a(6) - 2 * a(8) - 2 * a(9)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 1020:
If b1(a(2)) = 0 Then GoTo 1020
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 1020
a(1) = -2 * s1 + 2 * a(6) + 2 * a(8) + 3 * a(9)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 1010:
If b1(a(1)) = 0 Then GoTo 1010
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 1010
GoSub 950: If fl1 = 0 Then GoTo 1005 ' Anti Symmetric
n10 = n10 + 1
If n10 = 8 Then
GoSub 750 ' Load Square Nr 8
GoSub 900 ' Remove used primes from available primes
GoSub 850 ' Double Check Identical Integers
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 ' Print Composed Squares
End If
Erase b, c: GoTo 1000 ' Next Magic Sum
Else
GoSub 750 ' Load Square Nr 1 ... 7
GoSub 900 ' Remove used primes from available primes
Erase b, c: GoTo 1090
End If
5
1005 b(c(1)) = 0: c(1) = 0
1010 b(c(2)) = 0: c(2) = 0
1020 b(c(3)) = 0: c(3) = 0
1030 b(c(4)) = 0: c(4) = 0
1040 b(c(5)) = 0: c(5) = 0
1050 b(c(6)) = 0: c(6) = 0
1060 Next j6
b(c(7)) = 0: c(7) = 0
1070 b(c(8)) = 0: c(8) = 0
1080 Next j8
b(c(9)) = 0: c(9) = 0
1090 Next j9
1000 n10 = 0
Erase b1, b, c
Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Composed12")
End
' Transform and Assign Sub Squares
750 Select Case n10
Case 1:
a12(1) = a(3): a12(2) = a(2): a12(3) = a(1):
a12(13) = a(6): a12(14) = a(5): a12(15) = a(4):
a12(25) = a(9): a12(26) = a(8): a12(27) = a(7):
Case 2:
a12(4) = a(1): a12(5) = a(2): a12(6) = a(3):
a12(16) = a(4): a12(17) = a(5): a12(18) = a(6):
a12(28) = a(7): a12(29) = a(8): a12(30) = a(9):
Case 3:
a12(7) = a(3): a12(8) = a(2): a12(9) = a(1):
a12(19) = a(6): a12(20) = a(5): a12(21) = a(4):
a12(31) = a(9): a12(32) = a(8): a12(33) = a(7):
Case 4:
a12(10) = a(1): a12(11) = a(2): a12(12) = a(3):
a12(22) = a(4): a12(23) = a(5): a12(24) = a(6):
a12(34) = a(7): a12(35) = a(8): a12(36) = a(9):
Case 5:
a12(37) = a(9): a12(38) = a(8): a12(39) = a(7):
a12(49) = a(6): a12(50) = a(5): a12(51) = a(4):
a12(61) = a(3): a12(62) = a(2): a12(63) = a(1):
Case 6:
a12(40) = a(7): a12(41) = a(8): a12(42) = a(9):
a12(52) = a(4): a12(53) = a(5): a12(54) = a(6):
a12(64) = a(1): a12(65) = a(2): a12(66) = a(3):
Case 7:
a12(43) = a(9): a12(44) = a(8): a12(45) = a(7):
a12(55) = a(6): a12(56) = a(5): a12(57) = a(4):
a12(67) = a(3): a12(68) = a(2): a12(69) = a(1):
Case 8:
a12(46) = a(7): a12(47) = a(8): a12(48) = a(9):
a12(58) = a(4): a12(59) = a(5): a12(60) = a(6):
a12(70) = a(1): a12(71) = a(2): a12(72) = a(3):
' Assign Associated Elements
For i1 = 1 To 72
a12(145 - i1) = Pr3 - a12(i1)
Next i1
End Select
Return
' Print results (squares 12 x 12)
650 n2 = n2 + 1
If n2 = 2 Then
n2 = 1: k1 = k1 + 13: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 13
End If
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = CStr(s12)
i3 = 0
For i1 = 1 To 12
For i2 = 1 To 12
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a12(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers a12()
850 fl1 = 1
For j1 = 1 To 144
a20 = a12(j1)
For j2 = (1 + j1) To 144
If a20 = a12(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Remove used primes a() from available primes b1()
900 For i1 = 1 To 9
b1(a(i1)) = 0: b1(Pr3 - a(i1)) = 0
Next i1
Return
' Check Pairs
950 fl1 = 1: n25 = 0
For j1 = 1 To 9
a20 = Pr3 - a(j1) 'Complement
For j2 = (1 + j1) To 9
If a20 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Read Prime Numbers From Sheet Pairs8
3010 Pr3 = Sheets(ShtNm1).Cells(j100, 1).Value 'Pair Sum
s1 = 3 * Pr3 / 2 'MC3
s12 = 6 * Pr3 'MC12
nVar = Sheets(ShtNm1).Cells(j100, 5).Value
m1 = 1: m2 = nVar
For i1 = m1 To m2
a1(i1) = Sheets(ShtNm1).Cells(j100, i1 + 6).Value
Next i1
If a1(1) = 1 Then m1 = 2
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
Return
End Sub