' Generates Eccentric Magic Squares of order 6 for Prime Numbers
' Tested with Office 2007 under Windows 7
Sub Priem6d2()
Dim a1(2448), a(36), a4(36), b1(43300), b(43300), c(36)
y = MsgBox("Locked", vbCritical, "Routine Priem6d2")
End
n2 = 0: n3 = 0: n9 = 0: k1 = 1: k2 = 1
Sht1 = "Pairs8"
Sht2 = "PM4"
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 2237
Rcrd1a = Sheets(Sht2).Cells(j100, 18).Value
' Define variables
Pr6 = Sheets(Sht1).Cells(Rcrd1a, 1).Value 'PairSum
s1 = 3 * Pr6 'MC6
s3 = s1 / 2
nVar1 = Sheets(Sht1).Cells(Rcrd1a, 5).Value
' Read Prime Numbers From sheet Sht1
For i1 = 1 To nVar1
a1(i1) = Sheets(Sht1).Cells(Rcrd1a, 6 + i1).Value
Next i1
pMax = Sheets(Sht1).Cells(Rcrd1a, 6 + nVar1).Value
m1 = 1: m2 = nVar1
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Read Border Square and remove from Available Primes
For i1 = 1 To 16
a4(i1) = Sheets(Sht2).Cells(j100, i1).Value
b1(a4(i1)) = 0
Next i1
' Load Corner Square
a(15) = a4(1): a(16) = a4(2): a(17) = a4(3): a(18) = a4(4):
a(21) = a4(5): a(22) = a4(6): a(23) = a4(7): a(24) = a4(8):
a(27) = a4(9): a(28) = a4(10): a(29) = a4(11): a(30) = a4(12):
a(33) = a4(13): a(34) = a4(14): a(35) = a4(15): a(36) = a4(16):
' 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
' Determine Main Diagonal and related pairs
Erase a4
For j1 = m1 To m2
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 10
a4(1) = a1(j1)
a4(5) = Pr6 - a4(1): If b(a4(5)) = 0 Then b(a4(5)) = a4(5): c(5) = a4(5) Else GoTo 50
For j2 = m1 To m2
If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 20
a4(2) = a1(j2)
a4(6) = Pr6 - a4(2): If b(a4(6)) = 0 Then b(a4(6)) = a4(6): c(6) = a4(6) Else GoTo 60
For j3 = m1 To m2
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 30
a4(3) = a1(j3)
a4(7) = Pr6 - a4(3): If b(a4(7)) = 0 Then b(a4(7)) = a4(7): c(7) = a4(7) Else GoTo 70
a4(4) = s1 - a4(3) - a4(2) - a4(1) - a(21) - a(16)
If a4(4) < a1(m1) Or a4(4) > a1(m2) Then GoTo 40:
If b1(a4(4)) = 0 Then GoTo 40
If b(a4(4)) = 0 Then b(a4(4)) = a4(4): c(4) = a4(4) Else GoTo 40
a4(8) = Pr6 - a4(4): If b(a4(8)) = 0 Then b(a4(8)) = a4(8): c(8) = a4(8) Else GoTo 80
a(5) = a4(6): a(6) = a4(1):
a(11) = a4(2): a(12) = a4(5):
a(25) = a4(7): a(26) = a4(3):
a(31) = a4(4): a(32) = a4(8):
' Determine remainder of the pairs
For j17 = m1 To m2 'a4(17)
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 170
a4(17) = a1(j17)
a4(21) = Pr6 - a4(17): If b(a4(21)) = 0 Then b(a4(21)) = a4(21): c(21) = a4(21) Else GoTo 210
For j18 = m1 To m2
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
a4(18) = a1(j18)
a4(22) = Pr6 - a4(18): If b(a4(22)) = 0 Then b(a4(22)) = a4(22): c(22) = a4(22) Else GoTo 220
For j19 = m1 To m2
If b(a1(j19)) = 0 Then b(a1(j19)) = a1(j19): c(19) = a1(j19) Else GoTo 190
a4(19) = a1(j19)
a4(24) = Pr6 - a4(19): If b(a4(24)) = 0 Then b(a4(24)) = a4(24): c(24) = a4(24) Else GoTo 240
a4(20) = s1 - a4(19) - a4(18) - a4(17) - a4(6) - a4(1)
If a4(20) < a1(m1) Or a4(20) > a1(m2) Then GoTo 200:
If b1(a4(20)) = 0 Then GoTo 200
If b(a4(20)) = 0 Then b(a4(20)) = a4(20): c(20) = a4(20) Else GoTo 200
a4(23) = Pr6 - a4(20): If b(a4(23)) = 0 Then b(a4(23)) = a4(23): c(23) = a4(23) Else GoTo 230
a(1) = a4(20): a(2) = a4(19): a(3) = a4(18): a(4) = a4(17):
a(7) = a4(24): a(8) = a4(23): a(9) = a4(22): a(10) = a4(21):
For j25 = m1 To m2
If b(a1(j25)) = 0 Then b(a1(j25)) = a1(j25): c(25) = a1(j25) Else GoTo 250
a4(25) = a1(j25)
a4(27) = Pr6 - a4(25): If b(a4(27)) = 0 Then b(a4(27)) = a4(27): c(27) = a4(27) Else GoTo 270
a4(26) = s1 - a4(25) - a4(24) - a4(20) - a4(7) - a4(4)
If a4(26) < a1(m1) Or a4(26) > a1(m2) Then GoTo 260:
If b1(a4(26)) = 0 Then GoTo 260
If b(a4(26)) = 0 Then b(a4(26)) = a4(26): c(26) = a4(26) Else GoTo 260
a4(28) = Pr6 - a4(26): If b(a4(28)) = 0 Then b(a4(28)) = a4(28): c(28) = a4(28) Else GoTo 280
a(13) = a4(25): a(14) = a4(27):
a(19) = a4(26): a(20) = a4(28):
n9 = n9 + 1
' GoSub 2650 'Print results (squares)
GoSub 2645 'Print results (selected numbers)
Erase b, c: GoTo 500
5 b(c(28)) = 0: c(28) = 0
280 b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(23)) = 0: c(23) = 0
230 b(c(20)) = 0: c(20) = 0
200 b(c(24)) = 0: c(24) = 0
240 b(c(19)) = 0: c(19) = 0
190 Next j19
b(c(22)) = 0: c(22) = 0
220 b(c(18)) = 0: c(18) = 0
180 Next j18
b(c(21)) = 0: c(21) = 0
210 b(c(17)) = 0: c(17) = 0
170 Next j17
90 b(c(8)) = 0: c(8) = 0
80 b(c(4)) = 0: c(4) = 0
40 b(c(7)) = 0: c(7) = 0
70 b(c(3)) = 0: c(3) = 0
30 Next j3
b(c(6)) = 0: c(6) = 0
60 b(c(2)) = 0: c(2) = 0
20 Next j2
b(c(5)) = 0: c(5) = 0
50 b(c(1)) = 0: c(1) = 0
10 Next j1
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 Priem6d2")
End
' Print results (selected numbers)
2645 For i1 = 1 To 36
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 37).Select
Cells(n9, 37).Value = s1
Cells(n9, 38).Value = Rcrd1a
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)
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
End Sub