' Constructs 10 x 10 Bordered Magic Squares (Prime Numbers)
' Associated Magic Center Squares Order 4
' Tested with Office 2007 under Windows 7
Sub Priem10f2()
Dim a1(2448), a(169), a10(100), b1(43300), b(43300), c(100)
y = MsgBox("Locked", vbCritical, "Routine Priem10f2")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Brdr10"
t1 = Timer
For j100 = 2 To 100 ''2031
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 102).Value
MC10 = Sheets(ShtNm2).Cells(j100, 101).Value
' Read Prime Numbers From Sheet ShtNm1
Pr3 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value
s1 = 2 * Pr3 'MC4
s10 = 5 * Pr3 'MC10
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
If nVar < 136 Then GoTo 1000
If MC10 <> s10 Then
y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2)
End
End If
Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm1).Cells(Rcrd1a, 6 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 6 + nVar).Value
' Read Border 10 x 10
For i1 = 1 To 100
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
a10(i1) = a(i1)
Next i1
n32 = 100: GoSub 950 'Remove used primes from available primes
' Restore available pairs in a1()
n10 = 0
For j1 = 1 To pMax
If b1(j1) <> 0 Then
n10 = n10 + 1
a1(n10) = b1(j1)
End If
Next j1
m1 = 1: m2 = n10: n10 = 0
If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
t11 = Timer 'Time Out
' Generate Center Squares
For j16 = m1 To m2 'a(16)
If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 160
a(16) = a1(j16)
For j15 = m1 To m2 'a(15)
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a(15) = a1(j15)
For j14 = m1 To m2 'a(14)
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
a(14) = a1(j14)
a(13) = s1 - a(14) - a(15) - a(16)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 130
If b1(a(13)) = 0 Then GoTo 130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
For j12 = m1 To m2 'a(12)
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
a(12) = a1(j12)
a(11) = s1 - a(12) - a(15) - a(16)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 70
If b1(a(11)) = 0 Then GoTo 70
a(10) = s1 - a(12) - a(14) - a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 70
If b1(a(10)) = 0 Then GoTo 70
a(9) = s1 - a(10) - a(11) - a(12)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 70
If b1(a(9)) = 0 Then GoTo 70
a(8) = 0.5 * s1 - a(9)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 70
If b1(a(8)) = 0 Then GoTo 70
a(7) = 0.5 * s1 - a(10)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70:
If b1(a(7)) = 0 Then GoTo 70
a(6) = 0.5 * s1 - a(11)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 70:
If b1(a(6)) = 0 Then GoTo 70
a(5) = 0.5 * s1 - a(12)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 70:
If b1(a(5)) = 0 Then GoTo 70
a(4) = 0.5 * s1 - a(13)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 70:
If b1(a(4)) = 0 Then GoTo 70
a(3) = 0.5 * s1 - a(14)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 70:
If b1(a(3)) = 0 Then GoTo 70
a(2) = 0.5 * s1 - a(15)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 70:
If b1(a(2)) = 0 Then GoTo 70
a(1) = 0.5 * s1 - a(16)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 70:
If b1(a(1)) = 0 Then GoTo 70
' Exclude solutions with identical numbers
GoSub 850: If fl1 = 0 Then GoTo 70
' Transfer to a10()
a10(34) = a(1): a10(35) = a(2): a10(36) = a(3): a10(37) = a(4):
a10(44) = a(5): a10(45) = a(6): a10(46) = a(7): a10(47) = a(8):
a10(54) = a(9): a10(55) = a(10): a10(56) = a(11): a10(57) = a(12):
a10(64) = a(13): a10(65) = a(14): a10(66) = a(15): a10(67) = a(16):
GoSub 800: 'Back Check Identical Numbers a10()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 1650 'Print results (squares)
' n9 = n9 + 1: GoSub 1640 'Print results (selected numbers)
End If
Erase b1, b, c: GoTo 1000
70 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(16)) = 0: c(16) = 0
160 Next j16
Erase b1, b, c
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem10f2")
End
' Check Identical Numbers a()
850 fl1 = 1
For i1 = 1 To 16
a20 = a(i1)
For i2 = (1 + i1) To 16
If a20 = a(i2) Then fl1 = 0: Return
Next i2
Next i1
Return
' Double Check Identical Numbers a10()
800 fl1 = 1
For i1 = 1 To 100
a20 = a10(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 100
If a20 = a10(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Remove used pairs from b1()
950 For i1 = 1 To n32
b1(a(i1)) = 0
Next i1
Return
' Print results (lines)
1640 Cells(n9, 1).Select
For i1 = 1 To 100
Cells(n9, i1).Value = a10(i1)
Next i1
Cells(n9, 101).Value = s10 'MC10
Cells(n9, 102).Value = Rcrd1a 'Record
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 11: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 11
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s10)
i3 = 0
For i1 = 1 To 10
For i2 = 1 To 10
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a10(i3)
Next i2
Next i1
Return
End Sub