' Generates Eccentric Magic Squares of order 8 for Prime Numbers
' Tested with Office 2007 under Windows 7
Sub Priem8a2()
Dim a1(2448), a(36), a8(64), b1(43300), b(43300), c(64)
y = MsgBox("Locked", vbCritical, "Routine Priem8a2")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs8"
ShtNm2 = "Lines6"
t1 = Timer
For j100 = 2 To 112
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 38).Value
MC6 = Sheets(ShtNm2).Cells(j100, 37).Value
' Read Prime Numbers From Sheet ShtNm1
Pr8 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'PairSum
s4 = 2 * Pr8 'Magic Sum 4 x 4
s6 = Sheets(ShtNm1).Cells(Rcrd1a, 3).Value 'Magic Sum 6 x 6
s8 = Sheets(ShtNm1).Cells(Rcrd1a, 4).Value 'Magic Sum 8 x 8
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
If nVar < 64 Then GoTo 1000
If MC6 <> s6 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 Eccentric Square 6 x 6
For i1 = 1 To 36
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
GoSub 950 'Remove used primes from available primes
s82 = a(4) + a(9) + a(14) + a(19)
' Store Magic Square 6 x 6 in a()
Erase a8
a8(19) = a(1): a8(20) = a(2): a8(21) = a(3): a8(22) = a(4): a8(23) = a(5): a8(24) = a(6):
a8(27) = a(7): a8(28) = a(8): a8(29) = a(9): a8(30) = a(10): a8(31) = a(11): a8(32) = a(12):
a8(35) = a(13): a8(36) = a(14): a8(37) = a(15): a8(38) = a(16): a8(39) = a(17): a8(40) = a(18):
a8(43) = a(19): a8(44) = a(20): a8(45) = a(21): a8(46) = a(22): a8(47) = a(23): a8(48) = a(24):
a8(51) = a(25): a8(52) = a(26): a8(53) = a(27): a8(54) = a(28): a8(55) = a(29): a8(56) = a(30):
a8(59) = a(31): a8(60) = a(32): a8(61) = a(33): a8(62) = a(34): a8(63) = a(35): a8(64) = a(36):
Erase a
' 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
If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
' Determine Main Diagonal and related pairs
For j1 = m1 To m2
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 10
a(1) = a1(j1)
a(5) = Pr8 - a(1): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(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
a(2) = a1(j2)
a(6) = Pr8 - a(2): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(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
a(3) = a1(j3)
a(7) = Pr8 - a(3): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
a(4) = (s8 - s82) - a(3) - a(2) - a(1)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 40:
If b1(a(4)) = 0 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
a(8) = Pr8 - a(4): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a8(8) = a(1): a8(7) = a(6)
a8(15) = a(2): a8(16) = a(5)
a8(50) = a(3): a8(49) = a(7)
a8(57) = a(4): a8(58) = a(8)
For j9 = m1 To m2 'a(9)
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 90
a(9) = a1(j9)
a(13) = Pr8 - a(9): If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130
a(10) = s4 - a(1) - a(6) - a(9)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 100:
If b1(a(10)) = 0 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(14) = Pr8 - a(10): If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
For j12 = m1 To m2
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
a(12) = a1(j12)
a(16) = Pr8 - a(12): If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
a(11) = s4 - a(4) - a(7) - a(12)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 110:
If b1(a(11)) = 0 Then GoTo 110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
a(15) = Pr8 - a(11): If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
a8(5) = a(10): a8(13) = a(14)
a8(6) = a(9): a8(14) = a(13)
a8(33) = a(11): a8(34) = a(15)
a8(41) = a(12): a8(42) = a(16)
' Determine remainder of the pairs
For j17 = m1 To m2 'a(17)
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 170
a(17) = a1(j17)
a(21) = Pr8 - a(17): If b(a(21)) = 0 Then b(a(21)) = a(21): c(21) = a(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
a(18) = a1(j18)
a(22) = Pr8 - a(18): If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(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
a(19) = a1(j19)
a(24) = Pr8 - a(19): If b(a(24)) = 0 Then b(a(24)) = a(24): c(24) = a(24) Else GoTo 240
a(20) = s4 - a(19) - a(18) - a(17)
If a(20) < a1(m1) Or a(20) > a1(m2) Then GoTo 200:
If b1(a(20)) = 0 Then GoTo 200
If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
a(23) = Pr8 - a(20): If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230
a8(1) = a(20): a8(10) = a(23)
a8(2) = a(19): a8(9) = a(24)
a8(3) = a(18): a8(11) = a(22)
a8(4) = a(17): a8(12) = a(21)
For j25 = m1 To m2
If b(a1(j25)) = 0 Then b(a1(j25)) = a1(j25): c(25) = a1(j25) Else GoTo 250
a(25) = a1(j25)
a(27) = Pr8 - a(25): If b(a(27)) = 0 Then b(a(27)) = a(27): c(27) = a(27) Else GoTo 270
a(26) = s4 - a(25) - a(24) - a(20)
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
a(28) = Pr8 - a(26): If b(a(28)) = 0 Then b(a(28)) = a(28): c(28) = a(28) Else GoTo 280
a8(17) = a(25): a8(18) = a(27)
a8(25) = a(26): a8(26) = a(28)
GoSub 800: If fl1 = 0 Then GoTo 5 'Double Check Identical Numbers a8()
n9 = n9 + 1
GoSub 2650 'Print results (squares)
' GoSub 2645 'Print results (selected numbers)
Erase b1, b, c: GoTo 1000 'Print only first square
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
b(c(15)) = 0: c(15) = 0
150 b(c(11)) = 0: c(11) = 0
110 b(c(16)) = 0: c(16) = 0
160 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(14)) = 0: c(14) = 0
140 b(c(10)) = 0: c(10) = 0
100 b(c(13)) = 0: c(13) = 0
130 b(c(9)) = 0: c(9) = 0
90 Next j9
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
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem8a2")
End
' Print results (selected numbers)
2645 For i1 = 1 To 64
Cells(n9, i1).Value = a8(i1)
Next i1
Return
' Print results (squares)
2650 n5 = n5 + 1
If n5 = 5 Then
n5 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s8)
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a8(i3)
Next i2
Next i1
Return
' Double Check Identical Numbers a8()
800 fl1 = 1
For i1 = 1 To 64
a20 = a8(i1): If a20 = 0 Then GoTo 810
For i2 = (1 + i1) To 64
If a20 = a8(i2) Then fl1 = 0: Return
Next i2
810 Next i1
Return
' Remove used pairs from b1()
950 For j1 = 1 To 36
b1(a(j1)) = 0
Next j1
Return
End Sub