' Generates Bordered Magic Cubes of order 6 (Prime Numbers)
' Part II: Completes Border (Semi Magic Planes)
' Tested with Office 2007 under Windows 7
Sub PrimeCubes6b()
Dim a1(1200), b1(21803), a(72), b(21803), c(72), c6(216)
y = MsgBox("Locked", vbCritical, "Routine Priem6b")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs63": ShtNm2 = "PrtCbs6"
Sheets("Klad1").Select
t1 = Timer
For j100 = 26 To 31 ''92
' Start reading data from "PrtCbs6"
Rcrd1a = Sheets(ShtNm2).Cells(j100, 218).Value
MC6 = Sheets(ShtNm2).Cells(j100, 217).Value
GoSub 3100 'Read Prime Numbers From Sheet ShtNm1
'Reduce available prime numbers with c6()
' Complete Border (Left Square)
a(1) = c6(1): a(2) = c6(7): a(3) = c6(13): a(4) = c6(19): a(5) = c6(25): a(6) = c6(31):
a(7) = c6(37): a(12) = c6(67):
a(13) = c6(73): a(18) = c6(103):
a(19) = c6(109): a(24) = c6(139):
a(25) = c6(145): a(30) = c6(175):
a(31) = c6(181): a(32) = c6(187): a(33) = c6(193): a(34) = c6(199): a(35) = c6(205): a(36) = c6(211):
For j29 = m2 / 2 To m2 'a(29)
If b(a1(j29)) = 0 Then b(a1(j29)) = a1(j29): c(29) = a1(j29) Else GoTo 290
a(29) = a1(j29)
a(65) = Pr3 - a(29): If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650
For j28 = m1 To m2 'a(28)
If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c(28) = a1(j28) Else GoTo 280
a(28) = a1(j28)
a(64) = Pr3 - a(28): If b(a(64)) = 0 Then b(a(64)) = a(64): c(64) = a(64) Else GoTo 640
For j27 = m1 To m2 'a(27)
If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
a(27) = a1(j27)
a(63) = Pr3 - a(27): If b(a(63)) = 0 Then b(a(63)) = a(63): c(63) = a(63) Else GoTo 630
a(26) = s2 - a(27) - a(28) - a(29) - a(25) - a(30)
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(62) = Pr3 - a(26): If b(a(62)) = 0 Then b(a(62)) = a(62): c(62) = a(62) Else GoTo 620
For j23 = m1 To m2 'a(23)
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 230
a(23) = a1(j23)
a(59) = Pr3 - a(23): If b(a(59)) = 0 Then b(a(59)) = a(59): c(59) = a(59) Else GoTo 590
For j22 = m1 To m2 'a(22)
If b(a1(j22)) = 0 Then b(a1(j22)) = a1(j22): c(22) = a1(j22) Else GoTo 220
a(22) = a1(j22)
a(58) = Pr3 - a(22): If b(a(58)) = 0 Then b(a(58)) = a(58): c(58) = a(58) Else GoTo 580
For j21 = m1 To m2 'a(21)
If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c(21) = a1(j21) Else GoTo 210
a(21) = a1(j21)
a(57) = Pr3 - a(21): If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570
a(20) = s2 - a(21) - a(22) - a(23) - a(19) - a(24)
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(56) = Pr3 - a(20): If b(a(56)) = 0 Then b(a(56)) = a(56): c(56) = a(56) Else GoTo 560
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(53) = Pr3 - a(17): If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530
a(11) = s2 - a(17) - a(23) - a(29) - a(35) - a(5)
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(47) = Pr3 - a(11): If b(a(47)) = 0 Then b(a(47)) = a(47): c(47) = a(47) Else GoTo 470
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)
a(52) = Pr3 - a(16): If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520
a(10) = s2 - a(16) - a(22) - a(28) - a(34) - a(4)
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(46) = Pr3 - a(10): If b(a(46)) = 0 Then b(a(46)) = a(46): c(46) = a(46) Else GoTo 460
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)
a(51) = Pr3 - a(15): If b(a(51)) = 0 Then b(a(51)) = a(51): c(51) = a(51) Else GoTo 510
a(14) = s2 - a(15) - a(16) - a(17) - a(13) - a(18)
If a(14) < a1(m1) Or a(14) > a1(m2) Then GoTo 140
If b1(a(14)) = 0 Then GoTo 140
If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140
a(50) = Pr3 - a(14): If b(a(50)) = 0 Then b(a(50)) = a(50): c(50) = a(50) Else GoTo 500
a(8) = s2 - a(14) - a(20) - a(26) - a(32) - a(2)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 80
If b1(a(8)) = 0 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(44) = Pr3 - a(8): If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440
a(9) = s2 - a(15) - a(21) - a(27) - a(33) - a(3)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 90
If b1(a(9)) = 0 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(45) = Pr3 - a(9): If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450
' Side Plane (Left)
c6(43) = a(8): c6(49) = a(9): c6(55) = a(10): c6(61) = a(11):
c6(79) = a(14): c6(85) = a(15): c6(91) = a(16): c6(97) = a(17):
c6(115) = a(20): c6(121) = a(21): c6(127) = a(22): c6(133) = a(23):
c6(151) = a(26): c6(157) = a(27): c6(163) = a(28): c6(169) = a(29):
' Side Plane (Right)
c6(48) = Pr3 - a(8): c6(54) = Pr3 - a(9): c6(60) = Pr3 - a(10): c6(66) = Pr3 - a(11):
c6(84) = Pr3 - a(14): c6(90) = Pr3 - a(15): c6(96) = Pr3 - a(16): c6(102) = Pr3 - a(17):
c6(120) = Pr3 - a(20): c6(126) = Pr3 - a(21): c6(132) = Pr3 - a(22): c6(138) = Pr3 - a(23):
c6(156) = Pr3 - a(26): c6(162) = Pr3 - a(27): c6(168) = Pr3 - a(28): c6(174) = Pr3 - a(29):
GoSub 850 'Back Check Identical Numbers
If fl1 = 1 Then
' n9 = n9 + 1: GoSub 2650 'Print Left Square
n9 = n9 + 1: GoSub 1750 'Print Complete Border
Erase b, c: GoTo 1000
End If
b(c(45)) = 0: c(45) = 0
450 b(c(9)) = 0: c(9) = 0
90 b(c(44)) = 0: c(44) = 0
440 b(c(8)) = 0: c(8) = 0
80 b(c(50)) = 0: c(50) = 0
500 b(c(14)) = 0: c(14) = 0
140 b(c(51)) = 0: c(51) = 0
510 b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(46)) = 0: c(46) = 0
460 b(c(10)) = 0: c(10) = 0
100 b(c(52)) = 0: c(52) = 0
520 b(c(16)) = 0: c(16) = 0
160 Next j16
b(c(47)) = 0: c(47) = 0
470 b(c(11)) = 0: c(11) = 0
110 b(c(53)) = 0: c(53) = 0
530 b(c(17)) = 0: c(17) = 0
170 Next j17
b(c(56)) = 0: c(56) = 0
560 b(c(20)) = 0: c(20) = 0
200 b(c(57)) = 0: c(57) = 0
570 b(c(21)) = 0: c(21) = 0
210 Next j21
b(c(58)) = 0: c(58) = 0
580 b(c(22)) = 0: c(22) = 0
220 Next j22
b(c(59)) = 0: c(59) = 0
590 b(c(23)) = 0: c(23) = 0
230 Next j23
b(c(62)) = 0: c(62) = 0
620 b(c(26)) = 0: c(26) = 0
260 b(c(63)) = 0: c(63) = 0
630 b(c(27)) = 0: c(27) = 0
270 Next j27
b(c(64)) = 0: c(64) = 0
640 b(c(28)) = 0: c(28) = 0
280 Next j28
b(c(65)) = 0: c(65) = 0
650 b(c(29)) = 0: c(29) = 0
290 Next j29
1000 n10 = 0
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem6b")
End
' 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(s2)
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 c6()
850 fl1 = 1
For j1 = 1 To 216
a20 = c6(j1): If a20 = 0 Then GoTo 855
For j2 = (1 + j1) To 216
If a20 = c6(j2) Then fl1 = 0: Return
Next j2
855 Next j1
Return
' Check Pairs a()
950 fl1 = 1: n25 = 0
For j1 = 1 To 36
a20 = Pr3 - a(j1) 'Complement
For j2 = (1 + j1) To 36
If a20 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Print results (6 plane format)
1750 n2 = n2 + 1
If n2 = 4 Then
n2 = 1: k1 = k1 + 42: 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(s2)
For i0 = 1 To 6
i3 = (6 - i0) * 36
For i1 = 1 To 6
For i2 = 1 To 6
i3 = i3 + 1
Cells(k1 + i1 + (i0 - 1) * 7, k2 + i2).Value = c6(i3)
Next i2
Next i1
Next i0
Return
' Read Prime Numbers From Sheet ShtNm1 and ShtNm2
3100 Pr3 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value 'Pair Sum
s1 = 3 * Pr3 / 2 'MC3
s2 = 3 * Pr3 'MC6
nVar = Sheets(ShtNm1).Cells(Rcrd1a, 5).Value
nSemi3 = Sheets(ShtNm1).Cells(Rcrd1a, 6).Value 'Expected Nmbr Semi Magic Squares
m1 = 1: m2 = nVar
Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm1).Cells(Rcrd1a, 10 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 10 + nVar).Value
' Read Parly Completed Border, and remove from b1()
For i1 = 1 To 216
c6(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
b1(c6(i1)) = 0
Next i1
' Store 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
Return
End Sub