' Generates Bordered Magic Cubes of order 7 (Prime Numbers)
' Part II: Anti Symmetric Semi Magic Back Squares (c)
' Tested with Office 2007 under Windows 7
Sub PrimeCubes7c()
Dim a1(724), a(100), b1(43291), b(43291), c(100), a7(49), c7(343)
y = MsgBox("Locked", vbCritical, "Routine PrimeCubes7c")
End
n2 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs77"
ShtNm2 = "TopSqrs7"
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 7
' Start Reading Data ShtNm2
Rcrd1a = Sheets(ShtNm2).Cells(j100, 51).Value
MC7 = Sheets(ShtNm2).Cells(j100, 50).Value
' Read Available Prime Numbers From Sheet ShtNm1
GoSub 2015
' Define Back Square
Erase a 'Clear Scratch Areas
For i1 = 1 To 7
a(i1) = c7(i1): a(i1 + 42) = c7(i1 + 294)
Next i1
b(Cntr7) = Cntr7 'Block Center Element
For j42 = (m2 - 1) / 2 To m1 Step -1 'a(42)
If b1(a1(j42)) = 0 Then GoTo 420
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c(42) = a1(j42) Else GoTo 420
a(42) = a1(j42)
a(92) = Pr7 - a(42): If b(a(92)) = 0 Then b(a(92)) = a(92): c(92) = a(92) Else GoTo 920
For j41 = m1 To m2 'a(41)
If b1(a1(j41)) = 0 Then GoTo 410
If b(a1(j41)) = 0 Then b(a1(j41)) = a1(j41): c(41) = a1(j41) Else GoTo 410
a(41) = a1(j41)
a(91) = Pr7 - a(41): If b(a(91)) = 0 Then b(a(91)) = a(91): c(91) = a(91) Else GoTo 910
a(40) = s3 - a(41) - a(42)
If a(40) < a1(m1) Or a(40) > a1(m2) Then GoTo 400
If b1(a(40)) = 0 Then GoTo 400
If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400
a(90) = Pr7 - a(40): If b(a(90)) = 0 Then b(a(90)) = a(90): c(90) = a(90) Else GoTo 900
For j35 = m1 To m2 'a(35)
If b1(a1(j35)) = 0 Then GoTo 350
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c(35) = a1(j35) Else GoTo 350
a(35) = a1(j35)
a(85) = Pr7 - a(35): If b(a(85)) = 0 Then b(a(85)) = a(85): c(85) = a(85) Else GoTo 850
For j34 = m1 To m2 'a(34)
If b1(a1(j34)) = 0 Then GoTo 340
If b(a1(j34)) = 0 Then b(a1(j34)) = a1(j34): c(34) = a1(j34) Else GoTo 340
a(34) = a1(j34)
a(84) = Pr7 - a(34): If b(a(84)) = 0 Then b(a(84)) = a(84): c(84) = a(84) Else GoTo 840
a(33) = s3 - a(34) - a(35)
If a(33) < a1(m1) Or a(33) > a1(m2) Then GoTo 330
If b1(a(33)) = 0 Then GoTo 330
If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330
a(83) = Pr7 - a(33): If b(a(83)) = 0 Then b(a(83)) = a(83): c(83) = a(83) Else GoTo 830
For j28 = m1 To m2 'a(28)
If b1(a1(j28)) = 0 Then GoTo 280
If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c(28) = a1(j28) Else GoTo 280
a(28) = a1(j28)
a(78) = Pr7 - a(28): If b(a(78)) = 0 Then b(a(78)) = a(78): c(78) = a(78) Else GoTo 780
For j27 = m1 To m2 'a(27)
If b1(a1(j27)) = 0 Then GoTo 263
If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 263
a(27) = a1(j27)
a(77) = Pr7 - a(27): If b(a(77)) = 0 Then b(a(77)) = a(77): c(77) = a(77) Else GoTo 770
a(26) = s3 - a(27) - a(28)
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(76) = Pr7 - a(26): If b(a(76)) = 0 Then b(a(76)) = a(76): c(76) = a(76) Else GoTo 760
For j21 = m1 To m2 'a(21)
If b1(a1(j21)) = 0 Then GoTo 210
If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c(21) = a1(j21) Else GoTo 210
a(21) = a1(j21)
a(71) = Pr7 - a(21): If b(a(71)) = 0 Then b(a(71)) = a(71): c(71) = a(71) Else GoTo 710
a(14) = s7 - a(7) - a(21) - a(28) - a(35) - a(42) - a(49)
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(64) = Pr7 - a(14): If b(a(64)) = 0 Then b(a(64)) = a(64): c(64) = a(64) Else GoTo 640
For j20 = m1 To m2 'a(20)
If b1(a1(j20)) = 0 Then GoTo 193
If b(a1(j20)) = 0 Then b(a1(j20)) = a1(j20): c(20) = a1(j20) Else GoTo 193
a(20) = a1(j20)
a(70) = Pr7 - a(20): If b(a(70)) = 0 Then b(a(70)) = a(70): c(70) = a(70) Else GoTo 700
a(19) = s3 - a(20) - a(21)
If a(19) < a1(m1) Or a(19) > a1(m2) Then GoTo 190
If b1(a(19)) = 0 Then GoTo 190
If b(a(19)) = 0 Then b(a(19)) = a(19): c(19) = a(19) Else GoTo 190
a(69) = Pr7 - a(19): If b(a(69)) = 0 Then b(a(69)) = a(69): c(69) = a(69) Else GoTo 690
a(13) = s7 - a(6) - a(20) - a(27) - a(34) - a(41) - a(48)
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
a(63) = Pr7 - a(13): If b(a(63)) = 0 Then b(a(63)) = a(63): c(63) = a(63) Else GoTo 630
a(12) = s7 - a(5) - a(19) - a(26) - a(33) - a(40) - a(47)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 120
If b1(a(12)) = 0 Then GoTo 120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120
a(62) = Pr7 - a(12): If b(a(62)) = 0 Then b(a(62)) = a(62): c(62) = a(62) Else GoTo 620
n9 = n9 + 1: GoSub 1740 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 1750 'Print results (squares)
Erase b, c: GoTo 1000 'Print only first Back Square
5
b(c(62)) = 0: c(62) = 0
620 b(c(12)) = 0: c(12) = 0
120 b(c(63)) = 0: c(63) = 0
630 b(c(13)) = 0: c(13) = 0
130 b(c(69)) = 0: c(69) = 0
690 b(c(19)) = 0: c(19) = 0
190 b(c(70)) = 0: c(70) = 0
700 b(c(20)) = 0: c(20) = 0
193 Next j20
b(c(64)) = 0: c(64) = 0
640 b(c(14)) = 0: c(14) = 0
140 b(c(71)) = 0: c(71) = 0
710 b(c(21)) = 0: c(21) = 0
210 Next j21
b(c(76)) = 0: c(76) = 0
760 b(c(26)) = 0: c(26) = 0
260 b(c(77)) = 0: c(77) = 0
770 b(c(27)) = 0: c(27) = 0
263 Next j27
b(c(78)) = 0: c(78) = 0
780 b(c(28)) = 0: c(28) = 0
280 Next j28
b(c(83)) = 0: c(83) = 0
830 b(c(33)) = 0: c(33) = 0
330 b(c(84)) = 0: c(84) = 0
840 b(c(34)) = 0: c(34) = 0
340 Next j34
b(c(85)) = 0: c(85) = 0
850 b(c(35)) = 0: c(35) = 0
350 Next j35
b(c(90)) = 0: c(90) = 0
900 b(c(40)) = 0: c(40) = 0
400 b(c(91)) = 0: c(91) = 0
910 b(c(41)) = 0: c(41) = 0
410 Next j41
b(c(92)) = 0: c(92) = 0
920 b(c(42)) = 0: c(42) = 0
420 Next j42
1000 Erase b1, b, c
Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s7)
y = MsgBox(t10, 0, "Routine PrimeCubes7c")
End
' Print results (selected numbers)
1740 For i1 = 1 To 49
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 50).Select
Cells(n9, 50).Value = s7
Cells(n9, 51).Value = Rcrd1a 'Record
Return
' Print results, back square a()
1750 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 8: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 8
End If
Cells(k1 + 1, k2 + 1).Select
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = s7
i3 = 0
For i1 = 1 To 7
For i2 = 1 To 7
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Fill Cube c7()
750
' Top Square
For i1 = 1 To 49
c7(i1) = a7(i1)
Next i1
' Bottom Square
c7(295) = Pr7 - c7(49): c7(296) = Pr7 - c7(44): c7(297) = Pr7 - c7(45): c7(298) = Pr7 - c7(46): c7(299) = Pr7 - c7(47):
c7(300) = Pr7 - c7(48): c7(301) = Pr7 - c7(43):
c7(302) = Pr7 - c7(14): c7(303) = Pr7 - c7(9): c7(304) = Pr7 - c7(10): c7(305) = Pr7 - c7(11): c7(306) = Pr7 - c7(12):
c7(307) = Pr7 - c7(13): c7(308) = Pr7 - c7(8):
c7(309) = Pr7 - c7(21): c7(310) = Pr7 - c7(16): c7(311) = Pr7 - c7(17): c7(312) = Pr7 - c7(18): c7(313) = Pr7 - c7(19):
c7(314) = Pr7 - c7(20): c7(315) = Pr7 - c7(15):
c7(316) = Pr7 - c7(28): c7(317) = Pr7 - c7(23): c7(318) = Pr7 - c7(24): c7(319) = Pr7 - c7(25): c7(320) = Pr7 - c7(26):
c7(321) = Pr7 - c7(27): c7(322) = Pr7 - c7(22):
c7(323) = Pr7 - c7(35): c7(324) = Pr7 - c7(30): c7(325) = Pr7 - c7(31): c7(326) = Pr7 - c7(32): c7(327) = Pr7 - c7(33):
c7(328) = Pr7 - c7(34): c7(329) = Pr7 - c7(29):
c7(330) = Pr7 - c7(42): c7(331) = Pr7 - c7(37): c7(332) = Pr7 - c7(38): c7(333) = Pr7 - c7(39): c7(334) = Pr7 - c7(40):
c7(335) = Pr7 - c7(41): c7(336) = Pr7 - c7(36):
c7(337) = Pr7 - c7(7): c7(338) = Pr7 - c7(2): c7(339) = Pr7 - c7(3): c7(340) = Pr7 - c7(4): c7(341) = Pr7 - c7(5):
c7(342) = Pr7 - c7(6): c7(343) = Pr7 - c7(1):
Return
' Remove used primes and complements from available primes
950 For i1 = 1 To 49
b1(a7(i1)) = 0
b1(Pr7 - a7(i1)) = 0
Next i1
Return
' Read Prime Numbers From sheet ShtNm1
2015 Pr7 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value ' Pairsum
nVar1 = Sheets(ShtNm1).Cells(Rcrd1a, 9).Value
s4 = 2 * Pr7 ' MC4
Cntr7 = Pr7 / 2 ' Center
s3 = 3 * Cntr7 ' MC3
s7 = 7 * Cntr7 ' MC7
Erase b1
For j1 = 1 To nVar1
x = Sheets(ShtNm1).Cells(Rcrd1a, 9 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm1).Cells(Rcrd1a, 9 + nVar1).Value
' Read Top Square 7 x 7
For i1 = 1 To 49
a7(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
GoSub 750 'Fill Top and Bottom c7()
GoSub 950 'Remove used primes and complements 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
Return
End Sub