' Constructs 15 x 15 Primme Number Associated Magic Squares composed of One Magic Center Square,
' Eight Semi Magic Anti Symmetric Diagonal Squares and Sixteen Semi Magic Anti Symmetric Border Squares
' Tested with Office 2007 under Windows 7
Sub Priem3f3()
Dim a1(1944), a15(225), a(9), b1(43300), b(43300), c(16), a2(9), b2(43300), c2(16), a3(225)
y = MsgBox("Locked", vbCritical, "Routine Priem3d3")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
Sht1 = "Pairs3" 'Pairs3, Pairs7 for 9 x 9, 15 x 15
'Pairs6, Pairs8 for 6 x 6, 12 x 12, 18 x 18
' Generate Squares
Sheets("Klad1").Select
t1 = Timer
For j100 = 1870 To 1870 ''2410
GoSub 2010 'Read Prime Numbers From Sheet Sht1
If nVar < 225 Then GoTo 1000
' Determine Center Square
For j9 = m1 To m2 'a(9)
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 160
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
If b1(a(7)) = 0 Then GoTo 110
a(6) = 4 * s1 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
If b1(a(6)) = 0 Then GoTo 110
a(5) = s1 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 110:
If b1(a(5)) = 0 Then GoTo 110
a(4) = -2 * s1 / 3 + 1 * a(8) + 2 * a(9):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 110:
If b1(a(4)) = 0 Then GoTo 110
a(3) = -s1 / 3 + 1 * a(8) + 1 * a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
If b1(a(3)) = 0 Then GoTo 110
a(2) = 2 * s1 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 110:
If b1(a(2)) = 0 Then GoTo 110
a(1) = 2 * s1 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
If b1(a(1)) = 0 Then GoTo 110
' Exclude solutions with identical numbers a()
GoSub 800: If fl1 = 0 Then GoTo 110
GoSub 700 'Assign Center Square
GoSub 900 'Remove used primes a() from available primes b1()
GoSub 2000 'Determine 12 Diagonal Squares
If n10 < 4 Then GoTo 70
GoSub 3000 'Determine 12 Border Squares
If n10 >= 12 Then n10 = 0: n3 = 0: Erase b, c: GoTo 1000
70 GoSub 905 'Reassign primes a() to available primes b1()
110 b(c(8)) = 0: c(8) = 0
120 Next j8
b(c(9)) = 0: c(9) = 0
160 Next j9
n10 = 0
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine Priem3d3")
End
' Determine 4 Corner Squares
2000 Erase b2, c2
For jj9 = m1 To m2 'a2(9)
If b1(a1(jj9)) = 0 Then GoTo 165
If b2(a1(jj9)) = 0 Then b2(a1(jj9)) = a1(jj9): c2(9) = a1(jj9) Else GoTo 165
a2(9) = a1(jj9)
For jj8 = m1 To m2 'a2(8)
If b1(a1(jj8)) = 0 Then GoTo 125
If b2(a1(jj8)) = 0 Then b2(a1(jj8)) = a1(jj8): c2(8) = a1(jj8) Else GoTo 125
a2(8) = a1(jj8)
a2(7) = s1 - a2(8) - a2(9):
If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 115:
If b1(a2(7)) = 0 Then GoTo 115
For jj6 = m1 To m2 'a2(6)
If b1(a1(jj6)) = 0 Then GoTo 100
If b2(a1(jj6)) = 0 Then b2(a1(jj6)) = a1(jj6): c2(6) = a1(jj6) Else GoTo 100
a2(6) = a1(jj6)
a2(5) = -s1 + a2(6) + a2(8) + 2 * a2(9)
If a2(5) < a1(m1) Or a2(5) > a1(m2) Then GoTo 80:
If b1(a2(5)) = 0 Then GoTo 80
a2(4) = 2 * s1 - 2 * a2(6) - a2(8) - 2 * a2(9)
If a2(4) < a1(m1) Or a2(4) > a1(m2) Then GoTo 80:
If b1(a2(4)) = 0 Then GoTo 80
a2(3) = s1 - a2(6) - a2(9)
If a2(3) < a1(m1) Or a2(3) > a1(m2) Then GoTo 80:
If b1(a2(3)) = 0 Then GoTo 80
a2(2) = 2 * s1 - a2(6) - 2 * a2(8) - 2 * a2(9)
If a2(2) < a1(m1) Or a2(2) > a1(m2) Then GoTo 80:
If b1(a2(2)) = 0 Then GoTo 80
a2(1) = -2 * s1 + 2 * a2(6) + 2 * a2(8) + 3 * a2(9)
If a2(1) < a1(m1) Or a2(1) > a1(m2) Then GoTo 80:
If b1(a2(1)) = 0 Then GoTo 80
' Exclude solutions with identical numbers a2()
GoSub 810: If fl1 = 0 Then GoTo 80
GoSub 1900: If fl1 = 0 Then GoTo 80 'Anti Symmetric
n10 = n10 + 1
If n10 < 4 Then
GoSub 750 'Transform and Assign Corner Squares
GoSub 910 'Remove used primes a2() from available primes b1()
Erase b2, c2: GoTo 165
Else
GoSub 750 'Transform and Assign Corner Squares
GoSub 910 'Remove used primes a2() from available primes b1()
End If
If n10 = 4 Then Erase b2, c2: Return 'Only six squares required
80 b2(c2(6)) = 0: c2(6) = 0
100 Next jj6
115 b2(c2(8)) = 0: c2(8) = 0
125 Next jj8
b2(c2(9)) = 0: c2(9) = 0
165 Next jj9
GoSub 915 'Reassign Primes a3()
Return
' Determine 4 Border Squares
3000 Erase b2, c2
' Determine Border Square
For jjj9 = m1 To m2 'a2(9)
If b1(a1(jjj9)) = 0 Then GoTo 155
If b2(a1(jjj9)) = 0 Then b2(a1(jjj9)) = a1(jjj9): c2(9) = a1(jjj9) Else GoTo 155
a2(9) = a1(jjj9)
For jjj8 = m1 To m2 'a2(8)
If b1(a1(jjj8)) = 0 Then GoTo 135
If b2(a1(jjj8)) = 0 Then b2(a1(jjj8)) = a1(jjj8): c2(8) = a1(jjj8) Else GoTo 135
a2(8) = a1(jjj8)
a2(7) = s1 - a2(8) - a2(9):
If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 130:
If b1(a2(7)) = 0 Then GoTo 130
For jjj6 = m1 To m2 'a2(6)
If b1(a1(jjj6)) = 0 Then GoTo 105
If b2(a1(jjj6)) = 0 Then b2(a1(jjj6)) = a1(jjj6): c2(6) = a1(jjj6) Else GoTo 105
a2(6) = a1(jjj6)
For jjj5 = m1 To m2 'a2(5)
If b1(a1(jjj5)) = 0 Then GoTo 95
If b2(a1(jjj5)) = 0 Then b2(a1(jjj5)) = a1(jjj5): c2(5) = a1(jjj5) Else GoTo 95
a2(5) = a1(jjj5)
a2(4) = s1 - a2(5) - a2(6)
If a2(4) < a1(m1) Or a2(4) > a1(m2) Then GoTo 85:
If b1(a2(4)) = 0 Then GoTo 85
a2(3) = -a2(6) + a2(7) + a2(8)
If a2(3) < a1(m1) Or a2(3) > a1(m2) Then GoTo 85:
If b1(a2(3)) = 0 Then GoTo 85
a2(2) = s1 - a2(5) - a2(8)
If a2(2) < a1(m1) Or a2(2) > a1(m2) Then GoTo 85:
If b1(a2(2)) = 0 Then GoTo 85
a2(1) = a2(5) + a2(6) - a2(7)
If a2(1) < a1(m1) Or a2(1) > a1(m2) Then GoTo 85:
If b1(a2(1)) = 0 Then GoTo 85
' Exclude solutions with identical numbers
GoSub 810: If fl1 = 0 Then GoTo 85
GoSub 1900: If fl1 = 0 Then GoTo 85 'Anti Symmetric
n10 = n10 + 1
If n10 < 12 Then
GoSub 750 'Transform and Assign Border Squares
GoSub 910 'Remove used primes a2() from available primes b1()
Erase b2, c2: GoTo 155
Else
GoSub 750 'Transform and Assign Border Squares
GoSub 850 'Double Check Identical Integers
If fl1 = 1 Then
n9 = n9 + 1: GoSub 650 'Print Composed Squares
End If
End If
If n10 = 12 Then Erase b2, c2: Return 'Only twelve squares required
85 b2(c2(5)) = 0: c2(5) = 0
95 Next jjj5
b2(c2(6)) = 0: c2(6) = 0
105 Next jjj6
130 b2(c2(8)) = 0: c2(8) = 0
135 Next jjj8
b2(c2(9)) = 0: c2(9) = 0
155 Next jjj9
GoSub 915 'Reassign Primes a3()
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 2 Then
n2 = 1: k1 = k1 + 16: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 16
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s2)
i3 = 0
For i1 = 1 To 15
For i2 = 1 To 15
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a15(i3)
Next i2
Next i1
Return
' Assign Center Square
700 a15(97) = a(1): a15(98) = a(2): a15(99) = a(3):
a15(112) = a(4): a15(113) = a(5): a15(114) = a(6):
a15(127) = a(7): a15(128) = a(8): a15(129) = a(9):
Return
' Transform and Assign Diagonal and Border Squares
750 Select Case n10
Case 1: 'Left Left Top
a15(1) = a2(3): a15(2) = a2(2): a15(3) = a2(1):
a15(16) = a2(6): a15(17) = a2(5): a15(18) = a2(4):
a15(31) = a2(9): a15(32) = a2(8): a15(33) = a2(7):
Case 2: 'Left Top
a15(49) = a2(3): a15(50) = a2(2): a15(51) = a2(1):
a15(64) = a2(6): a15(65) = a2(5): a15(66) = a2(4):
a15(79) = a2(9): a15(80) = a2(8): a15(81) = a2(7):
Case 3: 'Right Right Top
a15(13) = a2(1): a15(14) = a2(2): a15(15) = a2(3):
a15(28) = a2(4): a15(29) = a2(5): a15(30) = a2(6):
a15(43) = a2(7): a15(44) = a2(8): a15(45) = a2(9):
Case 4: 'Right Top
a15(55) = a2(1): a15(56) = a2(2): a15(57) = a2(3):
a15(70) = a2(4): a15(71) = a2(5): a15(72) = a2(6):
a15(85) = a2(7): a15(86) = a2(8): a15(87) = a2(9):
' Remaing Squares (Left - Right / Top - Bottom)
Case 5:
a15(4) = a2(1): a15(5) = a2(2): a15(6) = a2(3):
a15(19) = a2(4): a15(20) = a2(5): a15(21) = a2(6):
a15(34) = a2(7): a15(35) = a2(8): a15(36) = a2(9):
Case 6:
a15(7) = a2(1): a15(8) = a2(2): a15(9) = a2(3):
a15(22) = a2(4): a15(23) = a2(5): a15(24) = a2(6):
a15(37) = a2(7): a15(38) = a2(8): a15(39) = a2(9):
Case 7:
a15(10) = a2(1): a15(11) = a2(2): a15(12) = a2(3):
a15(25) = a2(4): a15(26) = a2(5): a15(27) = a2(6):
a15(40) = a2(7): a15(41) = a2(8): a15(42) = a2(9):
Case 8:
a15(46) = a2(1): a15(47) = a2(2): a15(48) = a2(3):
a15(61) = a2(4): a15(62) = a2(5): a15(63) = a2(6):
a15(76) = a2(7): a15(77) = a2(8): a15(78) = a2(9):
Case 9:
a15(52) = a2(1): a15(53) = a2(2): a15(54) = a2(3):
a15(67) = a2(4): a15(68) = a2(5): a15(69) = a2(6):
a15(82) = a2(7): a15(83) = a2(8): a15(84) = a2(9):
Case 10:
a15(58) = a2(1): a15(59) = a2(2): a15(60) = a2(3):
a15(73) = a2(4): a15(74) = a2(5): a15(75) = a2(6):
a15(88) = a2(7): a15(89) = a2(8): a15(90) = a2(9):
Case 11:
a15(91) = a2(1): a15(92) = a2(2): a15(93) = a2(3):
a15(106) = a2(4): a15(107) = a2(5): a15(108) = a2(6):
a15(121) = a2(7): a15(122) = a2(8): a15(123) = a2(9):
Case 12:
a15(94) = a2(1): a15(95) = a2(2): a15(96) = a2(3):
a15(109) = a2(4): a15(110) = a2(5): a15(111) = a2(6):
a15(124) = a2(7): a15(125) = a2(8): a15(126) = a2(9):
' Assign Associated Elements
For i1 = 1 To 96
a15(226 - i1) = Pr3 - a15(i1)
Next i1
For i1 = 106 To 111
a15(226 - i1) = Pr3 - a15(i1)
Next i1
For i1 = 121 To 126
a15(226 - i1) = Pr3 - a15(i1)
Next i1
End Select
Return
' Exclude solutions with identical numbers a()
800 fl1 = 1
For j1 = 1 To 9
a20 = a(j1)
For j2 = (1 + j1) To 9
If a20 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers a2()
810 fl1 = 1
For j1 = 1 To 9
a20 = a2(j1)
For j2 = (1 + j1) To 9
If a20 = a2(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers a15()
850 fl1 = 1
For j1 = 1 To 225
a20 = a15(j1)
For j2 = (1 + j1) To 225
If a20 = a15(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
Next i1
Return
' Reassign primes a() to available primes b1()
905 For i1 = 1 To 9
b1(a(i1)) = a(i1)
Next i1
Return
' Remove used primes a2() and complements from available primes b1()
' Store used primes a2() and complements temporarely in a3()
910 For i1 = 1 To 9
b1(a2(i1)) = 0: b1(Pr3 - a2(i1)) = 0
n3 = n3 + 1: a3(n3) = a2(i1)
Next i1
For i1 = 1 To 9
n3 = n3 + 1: a3(n3) = Pr3 - a2(i1)
Next i1
Return
' Reassign primes a3() to available primes b1()
915 For i1 = 1 To n3
b1(a3(i1)) = a3(i1)
Next i1
Erase a3: n3 = 0: n10 = 0
Return
' Check Pairs
1900 fl1 = 1: n25 = 0
For j1 = 1 To 9
a20 = Pr3 - a2(j1) 'Complement
For j2 = (1 + j1) To 9
If a20 = a2(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Read Prime Numbers From Sheet Sht1
2010 Cntr3 = Sheets(Sht1).Cells(j100, 4).Value 'Center Element
s1 = 3 * Cntr3 'MC3
s2 = 5 * s1 'MC15
nVar = Sheets(Sht1).Cells(j100, 5).Value
Pr3 = Sheets(Sht1).Cells(j100, 1).Value 'Pair Sum
nSemi3 = Sheets(Sht1).Cells(j100, 6).Value 'Expected Nmbr Semi Magic Squares
m1 = 1: m2 = nVar
For i1 = m1 To m2
a1(i1) = Sheets(Sht1).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