' Generates Prime Number Composed Magic Squares of order 8
' Non Symetric Corner Square, Composed Rectangles
' Tested with Office 2007 under Windows 7
Sub Priem9g2()
Dim a1(2448), a(36), a8(64), b1(43300), b(43300), c(64)
y = MsgBox("Locked", vbCritical, "Routine Priem9g2")
End
Sheets("Klad1").Select
n5 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs7"
ShtNm2 = "Lines5"
t1 = Timer
For j100 = 920 To 1010
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 27).Value
MC5 = Sheets(ShtNm2).Cells(j100, 26).Value
' Read Prime Numbers From Sheet ShtNm1
Pr8 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'PairSum
s3 = 3 * Pr8 / 2 'Magic Sum 3 x 3
s5 = 5 * Pr8 / 2 'Magic Sum 5 x 5
s8 = 4 * Pr8 'Magic Sum 8 x 8
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 9).Value
If MC5 <> s5 Then
y = MsgBox("Conflict in Data", vbCritical, "Read " + ShtNm2)
End
End If
Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm1).Cells(Rcrd1a, 9 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 9 + nVar).Value
' Read Non Symmetric Square 5 x 5
For i1 = 1 To 25
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
n32 = 25: GoSub 950 'Remove used primes from available primes
' Store Magic Square 5 x 5 in a8()
Erase a8
a8(28) = a(1): a8(29) = a(2): a8(30) = a(3): a8(31) = a(4): a8(32) = a(5):
a8(36) = a(6): a8(37) = a(7): a8(38) = a(8): a8(39) = a(9): a8(40) = a(10):
a8(44) = a(11): a8(45) = a(12): a8(46) = a(13): a8(47) = a(14): a8(48) = a(15):
a8(52) = a(16): a8(53) = a(17): a8(54) = a(18): a8(55) = a(19): a8(56) = a(20):
a8(60) = a(21): a8(61) = a(22): a8(62) = a(23): a8(63) = a(24): a8(64) = a(25):
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 Magic Square Order 3
t11 = Timer 'Time Out
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)
For j8 = m1 To m2 'a(8)
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 80
a(8) = a1(j8)
a(7) = s3 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70:
If b1(a(7)) = 0 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
a(6) = 4 * s3 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 60:
If b1(a(6)) = 0 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
a(5) = s3 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 50:
If b1(a(5)) = 0 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
a(4) = -2 * s3 / 3 + a(8) + 2 * a(9):
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(3) = -s3 / 3 + a(8) + a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 30:
If b1(a(3)) = 0 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(2) = 2 * s3 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 20:
If b1(a(2)) = 0 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
a(1) = 2 * s3 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 10:
If b1(a(1)) = 0 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
a8(1) = a(1): a8(2) = a(2): a8(3) = a(3):
a8(9) = a(4): a8(10) = a(5): a8(11) = a(6):
a8(17) = a(7): a8(18) = a(8): a8(19) = a(9):
' Remove used primes a() from available primes b1()
n32 = 9: GoSub 950
Erase b, c: GoTo 2000
b(c(1)) = 0: c(1) = 0
10 b(c(2)) = 0: c(2) = 0
20 b(c(3)) = 0: c(2) = 0
30 b(c(4)) = 0: c(4) = 0
40 b(c(5)) = 0: c(5) = 0
50 b(c(6)) = 0: c(6) = 0
60 b(c(7)) = 0: c(7) = 0
70 b(c(8)) = 0: c(8) = 0
80 Next j8
b(c(9)) = 0: c(9) = 0
90 Next j9
Erase b1, b, c: GoTo 1000 'Not Found, Next j100
2000 'Continue
For j8 = m1 To m2 'a8(8) Main Diagonal
If b1(a1(j8)) = 0 Then GoTo 2080
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 2080
a8(8) = a1(j8)
t12 = Timer: t13 = t12 - t11 'Time Out
If t13 > 60 Then Erase b1, b, c: GoTo 1000 'Time Out, Try Next
For j15 = m1 To m2 'a8(15)
If b1(a1(j15)) = 0 Then GoTo 2150
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 2150
a8(15) = a1(j15)
For j22 = m1 To m2 'a8(22)
If b1(a1(j22)) = 0 Then GoTo 2220
If b(a1(j22)) = 0 Then b(a1(j22)) = a1(j22): c(22) = a1(j22) Else GoTo 2220
a8(22) = a1(j22)
For j7 = m1 To m2 'a(7) Square 1
If b1(a1(j7)) = 0 Then GoTo 2070
If b(a1(j7)) = 0 Then b(a1(j7)) = a1(j7): c(7) = a1(j7) Else GoTo 2070
a8(7) = a1(j7)
a8(6) = s3 - a8(7) - a8(8)
If a8(6) < a1(m1) Or a8(6) > a1(m2) Then GoTo 2060
If b1(a8(6)) = 0 Then GoTo 2060
If b(a8(6)) = 0 Then b(a8(6)) = a8(6): c(6) = a8(6) Else GoTo 2060
a8(14) = -a8(22) + a8(8) + a8(7)
If a8(14) < a1(m1) Or a8(14) > a1(m2) Then GoTo 2140
If b1(a8(14)) = 0 Then GoTo 2140
If b(a8(14)) = 0 Then b(a8(14)) = a8(14): c(14) = a8(14) Else GoTo 2140
a8(16) = s3 + a8(22) - a8(15) - a8(8) - a8(7)
If a8(16) < a1(m1) Or a8(16) > a1(m2) Then GoTo 2160
If b1(a8(16)) = 0 Then GoTo 2160
If b(a8(16)) = 0 Then b(a8(16)) = a8(16): c(16) = a8(16) Else GoTo 2160
a8(23) = s3 - a8(15) - a8(7)
If a8(23) < a1(m1) Or a8(23) > a1(m2) Then GoTo 2230
If b1(a8(23)) = 0 Then GoTo 2230
If b(a8(23)) = 0 Then b(a8(23)) = a8(23): c(23) = a8(23) Else GoTo 2230
a8(24) = -a8(22) + a8(15) + a8(7)
If a8(24) < a1(m1) Or a8(24) > a1(m2) Then GoTo 2240
If b1(a8(24)) = 0 Then GoTo 2240
If b(a8(24)) = 0 Then b(a8(24)) = a8(24): c(24) = a8(24) Else GoTo 2240
For j43 = m1 To m2 'a8(43)
If b1(a1(j43)) = 0 Then GoTo 2430
If b(a1(j43)) = 0 Then b(a1(j43)) = a1(j43): c(43) = a1(j43) Else GoTo 2430
a8(43) = a1(j43)
For j50 = m1 To m2 'a8(50)
If b1(a1(j50)) = 0 Then GoTo 2500
If b(a1(j50)) = 0 Then b(a1(j50)) = a1(j50): c(50) = a1(j50) Else GoTo 2500
a8(50) = a1(j50)
a8(57) = s8 - a8(50) - a8(43) - a8(36) - a8(29) - a8(22) - a8(15) - a8(8)
If a8(57) < a1(m1) Or a8(57) > a1(m2) Then GoTo 2570
If b1(a8(57)) = 0 Then GoTo 2570
If b(a8(57)) = 0 Then b(a8(57)) = a8(57): c(57) = a8(57) Else GoTo 2570
For j42 = m1 To m2 'a8(42) Square 2
If b1(a1(j42)) = 0 Then GoTo 2420
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 2420
a8(42) = a1(j42)
a8(41) = s3 - a8(43) - a8(42)
If a8(41) < a1(m1) Or a8(41) > a1(m2) Then GoTo 2410
If b1(a8(41)) = 0 Then GoTo 2410
If b(a8(41)) = 0 Then b(a8(41)) = a8(41): c(41) = a8(41) Else GoTo 2410
a8(49) = -a8(57) + a8(43) + a8(42)
If a8(49) < a1(m1) Or a8(49) > a1(m2) Then GoTo 2490
If b1(a8(49)) = 0 Then GoTo 2490
If b(a8(49)) = 0 Then b(a8(49)) = a8(49): c(49) = a8(49) Else GoTo 2490
a8(51) = s3 + a8(57) - a8(50) - a8(43) - a8(42)
If a8(51) < a1(m1) Or a8(51) > a1(m2) Then GoTo 2510
If b1(a8(51)) = 0 Then GoTo 2510
If b(a8(51)) = 0 Then b(a8(51)) = a8(51): c(51) = a8(51) Else GoTo 2510
a8(58) = s3 - a8(50) - a8(42)
If a8(58) < a1(m1) Or a8(58) > a1(m2) Then GoTo 2580
If b1(a8(58)) = 0 Then GoTo 2580
If b(a8(58)) = 0 Then b(a8(58)) = a8(58): c(58) = a8(58) Else GoTo 2580
a8(59) = -a8(57) + a8(50) + a8(42)
If a8(59) < a1(m1) Or a8(59) > a1(m2) Then GoTo 2590
If b1(a8(59)) = 0 Then GoTo 2590
If b(a8(59)) = 0 Then b(a8(59)) = a8(59): c(59) = a8(59) Else GoTo 2590
' Remove used primes a8() from available primes b1()
n32 = 64: GoSub 900
Erase b, c: GoTo 3000
b(c(59)) = 0: c(59) = 0
2590 b(c(58)) = 0: c(58) = 0
2580 b(c(51)) = 0: c(51) = 0
2510 b(c(49)) = 0: c(49) = 0
2490 b(c(41)) = 0: c(41) = 0
2410 b(c(42)) = 0: c(42) = 0
2420 Next j42
b(c(57)) = 0: c(57) = 0
2570 b(c(50)) = 0: c(50) = 0
2500 Next j50
b(c(43)) = 0: c(43) = 0
2430 Next j43
b(c(24)) = 0: c(24) = 0
2240 b(c(23)) = 0: c(23) = 0
2230 b(c(16)) = 0: c(16) = 0
2160 b(c(14)) = 0: c(14) = 0
2140 b(c(6)) = 0: c(6) = 0
2060 b(c(7)) = 0: c(7) = 0
2070 Next j7
b(c(22)) = 0: c(22) = 0
2220 Next j22
b(c(15)) = 0: c(15) = 0
2150 Next j15
b(c(8)) = 0: c(8) = 0
2080 Next j8
Erase b1, b, c: GoTo 1000 'Not Found, Next j100
' Determine 2 Border Squares
3000 Erase a: n10 = 0 'Continue
For j6 = m1 To m2 'a(6)
If b1(a1(j6)) = 0 Then GoTo 3060
If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 3060
a(6) = a1(j6)
a(3) = Pr8 - a(6): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 3030
For j5 = m1 To m2 'a(5)
If b1(a1(j5)) = 0 Then GoTo 3050
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 3050
a(5) = a1(j5)
a(4) = s3 - a(5) - a(6):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 3040
If b1(a(4)) = 0 Then GoTo 3040
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 3040
a(2) = Pr8 - a(5): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 3020
a(1) = Pr8 - a(4): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 3010
n10 = n10 + 1
If n10 = 1 Then
a8(5) = a(1): a8(13) = a(2): a8(21) = a(3):
a8(4) = a(4): a8(12) = a(5): a8(20) = a(6):
n32 = 6: GoSub 950 'Remove used primes a() from available primes b1()
Erase b, c: GoTo 3060
Else
a8(25) = a(1): a8(26) = a(2): a8(27) = a(3):
a8(33) = a(4): a8(34) = a(5): a8(35) = a(6):
GoSub 800 'Double Check Identical Integers a8()
If fl1 = 1 Then
n9 = n9 + 1: GoSub 2650 'Print Composed Squares
End If
Erase b1, b, c: GoTo 1000
End If
b(c(1)) = 0: c(1) = 0
3010 b(c(2)) = 0: c(2) = 0
3020 b(c(4)) = 0: c(4) = 0
3040 b(c(5)) = 0: c(5) = 0
3050 Next j5
b(c(3)) = 0: c(3) = 0
3030 b(c(6)) = 0: c(6) = 0
3060 Next j6
Erase b1, b, c
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem9g2")
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 = 3 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()
900 For j1 = 1 To n32
b1(a8(j1)) = 0
b1(Pr8 - a8(j1)) = 0 'Complements
Next j1
Return
' Remove used pairs from b1()
950 For j1 = 1 To n32
b1(a(j1)) = 0
b1(Pr8 - a(j1)) = 0 'Complements
Next j1
Return
End Sub