' Generates Concentric Pan Magic Squares (6 x 6) with Associated Center Squares (4 x 4) for Prime Numbers
' Tested with Office 2007 under Windows 7
Sub Priem6g()
Dim a1(658), a(36), a4(16), b1(9923), b(9923), c(36)
y = MsgBox("Locked", vbCritical, "Routine Priem6g")
End
n2 = 0: n3 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sht1 = "Pairs6"
Sheets("Klad1").Select
t1 = Timer
For j100 = 828 To 8360
' Define variables
s1 = Sheets(Sht1).Cells(j100, 4).Value 'MC6
nVar1 = Sheets(Sht1).Cells(j100, 5).Value
' Read Prime Numbers From sheet Sht1
For i1 = 1 To nVar1
a1(i1) = Sheets(Sht1).Cells(j100, 10 + i1).Value
Next i1
m1 = 1: m2 = nVar1
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
For j29 = m1 To m2 'a(29) PM4
If b(a1(j29)) = 0 Then b(a1(j29)) = a1(j29): c(29) = a1(j29) Else GoTo 290
a(29) = a1(j29)
For j28 = m1 To m2 'a(28) PM4
If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c(28) = a1(j28) Else GoTo 280
a(28) = a1(j28)
For j27 = m1 To m2 'a(27) PM4
If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
a(27) = a1(j27)
a(26) = 2 * s1 / 3 - a(27) - a(28) - a(29)
If a(26) < a1(m1) Or a(26) > a1(m2) Then GoTo 260
If b1(a(26)) = 0 Then GoTo 260
If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
For j23 = m1 To m2 'a(23) PM4
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 230
a(23) = a1(j23)
a(22) = 2 * s1 / 3 - a(23) - a(28) - a(29)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220
If b1(a(22)) = 0 Then GoTo 220
a(21) = 2 * s1 / 3 - a(23) - a(27) - a(29)
If a(21) < a1(m1) Or a(21) > a1(m2) Then GoTo 220
If b1(a(21)) = 0 Then GoTo 220
a(20) = -2 * s1 / 3 + a(23) + a(27) + a(28) + 2 * a(29)
If a(20) < a1(m1) Or a(20) > a1(m2) Then GoTo 220
If b1(a(20)) = 0 Then GoTo 220
a(17) = s1 - a(23) - a(27) - a(28) - 2 * a(29)
If a(17) < a1(m1) Or a(17) > a1(m2) Then GoTo 220
If b1(a(17)) = 0 Then GoTo 220
a(16) = -s1 / 3 + a(23) + a(27) + a(29)
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 220
If b1(a(16)) = 0 Then GoTo 220
a(15) = -s1 / 3 + a(23) + a(28) + a(29)
If a(15) < a1(m1) Or a(15) > a1(m2) Then GoTo 220
If b1(a(15)) = 0 Then GoTo 220
a(14) = s1 / 3 - a(23)
If a(14) < a1(m1) Or a(14) > a1(m2) Then GoTo 220
If b1(a(14)) = 0 Then GoTo 220
a(11) = -s1 / 3 + a(27) + a(28) + a(29)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 220
If b1(a(11)) = 0 Then GoTo 220
a(10) = s1 / 3 - a(27)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 220
If b1(a(10)) = 0 Then GoTo 220
a(9) = s1 / 3 - a(28)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 220
If b1(a(9)) = 0 Then GoTo 220
a(8) = s1 / 3 - a(29)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 220
If b1(a(8)) = 0 Then GoTo 220
a(36) = 5 * s1 / 6 - a(23) - a(28) - 2 * a(29)
If a(36) < a1(m1) Or a(36) > a1(m2) Then GoTo 220
If b1(a(36)) = 0 Then GoTo 220
a(31) = s1 / 6 - a(23) + a(28)
If a(31) < a1(m1) Or a(31) > a1(m2) Then GoTo 220
If b1(a(31)) = 0 Then GoTo 220
a(6) = s1 / 6 + a(23) - a(28)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 220
If b1(a(6)) = 0 Then GoTo 220
a(1) = -s1 / 2 + a(23) + a(28) + 2 * a(29)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 220
If b1(a(1)) = 0 Then GoTo 220
GoSub 850: If fl1 = 0 Then GoTo 220 'Check Identical Numbers Subsquare
For j35 = m1 To m2 'a(35) = 1787
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c(35) = a1(j35) Else GoTo 350
a(35) = a1(j35)
a(32) = -2 * s1 / 3 - a(35) + 2 * a(23) + a(27) + a(28) + 2 * a(29)
If a(32) < a1(m1) Or a(32) > a1(m2) Then GoTo 345
If b1(a(32)) = 0 Then GoTo 345
a(30) = -s1 / 6 - a(35) + a(23) + a(28) + a(29)
If a(30) < a1(m1) Or a(30) > a1(m2) Then GoTo 345
If b1(a(30)) = 0 Then GoTo 345
a(25) = s1 / 2 + a(35) - a(23) - a(28) - a(29)
If a(25) < a1(m1) Or a(25) > a1(m2) Then GoTo 345
If b1(a(25)) = 0 Then GoTo 345
a(12) = s1 / 2 + a(35) - a(23) - a(27) - a(29)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 345
If b1(a(12)) = 0 Then GoTo 345
a(7) = -s1 / 6 - a(35) + a(23) + a(27) + a(29)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 345
If b1(a(7)) = 0 Then GoTo 345
a(5) = s1 / 3 - a(35)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 345
If b1(a(5)) = 0 Then GoTo 345
a(2) = s1 + a(35) - 2 * a(23) - a(27) - a(28) - 2 * a(29)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 345
If b1(a(2)) = 0 Then GoTo 345
For j34 = m1 To m2 'a(34) = 2417
If b(a1(j34)) = 0 Then b(a1(j34)) = a1(j34): c(34) = a1(j34) Else GoTo 340
a(34) = a1(j34)
a(33) = 2 * s1 / 3 - a(34) - a(27) - a(28)
If a(33) < a1(m1) Or a(33) > a1(m2) Then GoTo 330
If b1(a(33)) = 0 Then GoTo 330
a(24) = s1 / 6 - a(34) + a(29)
If a(24) < a1(m1) Or a(24) > a1(m2) Then GoTo 330
If b1(a(24)) = 0 Then GoTo 330
a(19) = s1 / 6 + a(34) - a(29)
If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 330
If b1(a(19)) = 0 Then GoTo 330
a(18) = -s1 / 2 + a(34) + 1 * a(27) + 1 * a(28) + 1 * a(29)
If a(18) < a1(m1) Or a(18) > a1(m2) Then GoTo 330
If b1(a(18)) = 0 Then GoTo 330
a(13) = 5 * s1 / 6 - a(34) - a(27) - a(28) - a(29)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 330
If b1(a(13)) = 0 Then GoTo 330
a(4) = s1 / 3 - a(34)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 330
If b1(a(4)) = 0 Then GoTo 330
a(3) = -s1 / 3 + a(34) + 1 * a(27) + a(28)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 330
If b1(a(3)) = 0 Then GoTo 330
GoSub 800: If fl1 = 0 Then GoTo 330 'Check Identical Numbers
n9 = n9 + 1: n10 = n10 + 1
GoSub 2650 'Print results (squares)
' GoSub 2645 'Print results (selected numbers)
Erase b, c: GoTo 500 'Print only first square
330 b(c(34)) = 0: c(34) = 0
340 Next j34
345 b(c(35)) = 0: c(35) = 0
350 Next j35
220 b(c(23)) = 0: c(23) = 0
230 Next j23
b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(28)) = 0: c(28) = 0
280 Next j28
b(c(29)) = 0: c(29) = 0
290 Next j29
n10 = 0: Erase b, c:
500 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine Priem6g")
End
' Print results (selected numbers)
2645 For i1 = 1 To 36
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
2650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 7: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 7
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s1) 'n9
i3 = 0
For i1 = 1 To 6
For i2 = 1 To 6
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers
800 fl1 = 1
For j1 = 1 To 36
a2 = a(j1)
For j2 = (1 + j1) To 36
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers in Center Square
850 fl1 = 1
For j1 = 1 To 16
Select Case j1
Case 1, 2, 3, 4
a4(j1) = a(j1 + 7)
Case 5, 6, 7, 8
a4(j1) = a(j1 + 9)
Case 9, 19, 11, 12
a4(j1) = a(j1 + 11)
Case 13, 14, 15, 16
a4(j1) = a(j1 + 13)
End Select
Next j1
For j1 = 1 To 16
a2 = a4(j1)
For j2 = (1 + j1) To 16
If a2 = a4(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
End Sub