' Generates Bordered Magic Cubes of order 7 (Prime Numbers)
' Part II: Anti Symmetric Semi Magic Back Squares (d)
' Tested with Office 2007 under Windows 7
Sub PrimeCubes7d()
Dim a1(724), a(100), b1(43291), b(43291), c(100), c7(343)
y = MsgBox("Locked", vbCritical, "Routine PrimeCubes7d")
End
n2 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "Pairs77"
ShtNm2 = "TopSqrs7"
ShtNm3 = "BackSqr1"
Sheets("Klad1").Select
t1 = Timer
For j100 = 2 To 7
' Start Reading Data ShtNm2/ShtNm3
Rcrd1a = Sheets(ShtNm2).Cells(j100, 51).Value
MC7 = Sheets(ShtNm2).Cells(j100, 50).Value
Rcrd1b = Sheets(ShtNm3).Cells(j100, 51).Value
If Rcrd1a <> Rcrd1b Then
y = MsgBox("Conflicting Data", vbCritical, "Check Input: " + CStr(j100))
End
End If
' Read Available Prime Numbers From Sheet ShtNm1
GoSub 2015
For j39 = (m2 - 1) / 2 To m1 Step -1 'a(39)
If b1(a1(j39)) = 0 Then GoTo 390
If b(a1(j39)) = 0 Then b(a1(j39)) = a1(j39): c(39) = a1(j39) Else GoTo 390
a(39) = a1(j39)
a(89) = Pr7 - a(39): If b(a(89)) = 0 Then b(a(89)) = a(89): c(89) = a(89) Else GoTo 890
For j32 = (m2 - 1) / 2 To m1 Step -1 'a(32)
If b1(a1(j32)) = 0 Then GoTo 320
If b(a1(j32)) = 0 Then b(a1(j32)) = a1(j32): c(32) = a1(j32) Else GoTo 320
a(32) = a1(j32)
a(82) = Pr7 - a(32): If b(a(82)) = 0 Then b(a(82)) = a(82): c(82) = a(82) Else GoTo 820
For j25 = (m2 - 1) / 2 To m1 Step -1 'a(25)
If b1(a1(j25)) = 0 Then GoTo 250
If b(a1(j25)) = 0 Then b(a1(j25)) = a1(j25): c(25) = a1(j25) Else GoTo 250
a(25) = a1(j25)
a(75) = Pr7 - a(25): If b(a(75)) = 0 Then b(a(75)) = a(75): c(75) = a(75) Else GoTo 750
For j18 = m1 To m2 'a(18)
If b1(a1(j18)) = 0 Then GoTo 180
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
a(18) = a1(j18)
a(68) = Pr7 - a(18): If b(a(68)) = 0 Then b(a(68)) = a(68): c(68) = a(68) Else GoTo 680
a(11) = s7 - a(4) - a(18) - a(25) - a(32) - a(39) - a(46)
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(61) = Pr7 - a(11): If b(a(61)) = 0 Then b(a(61)) = a(61): c(61) = a(61) Else GoTo 610
For j38 = m1 To m2 'a(38)
If b1(a1(j38)) = 0 Then GoTo 380
If b(a1(j38)) = 0 Then b(a1(j38)) = a1(j38): c(38) = a1(j38) Else GoTo 380
a(38) = a1(j38)
a(88) = Pr7 - a(38): If b(a(88)) = 0 Then b(a(88)) = a(88): c(88) = a(88) Else GoTo 880
For j31 = m1 To m2 'a(31)
If b1(a1(j31)) = 0 Then GoTo 310
If b(a1(j31)) = 0 Then b(a1(j31)) = a1(j31): c(31) = a1(j31) Else GoTo 310
a(31) = a1(j31)
a(81) = Pr7 - a(31): If b(a(81)) = 0 Then b(a(81)) = a(81): c(81) = a(81) Else GoTo 810
For j24 = m1 To m2 'a(24)
If b1(a1(j24)) = 0 Then GoTo 240
If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 240
a(24) = a1(j24)
a(74) = Pr7 - a(24): If b(a(74)) = 0 Then b(a(74)) = a(74): c(74) = a(74) Else GoTo 740
For j17 = m1 To m2 'a(17)
If b1(a1(j17)) = 0 Then GoTo 170
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 170
a(17) = a1(j17)
a(67) = Pr7 - a(17): If b(a(67)) = 0 Then b(a(67)) = a(67): c(67) = a(67) Else GoTo 670
a(10) = s7 - a(3) - a(17) - a(24) - a(31) - a(38) - a(45)
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(60) = Pr7 - a(10): If b(a(60)) = 0 Then b(a(60)) = a(60): c(60) = a(60) Else GoTo 600
For j37 = m1 To m2 'a(37)
If b1(a1(j37)) = 0 Then GoTo 370
If b(a1(j37)) = 0 Then b(a1(j37)) = a1(j37): c(37) = a1(j37) Else GoTo 370
a(37) = a1(j37)
a(87) = Pr7 - a(37): If b(a(87)) = 0 Then b(a(87)) = a(87): c(87) = a(87) Else GoTo 870
a(36) = s4 - a(37) - a(38) - a(39)
If a(36) < a1(m1) Or a(36) > a1(m2) Then GoTo 360
If b1(a(36)) = 0 Then GoTo 360
If b(a(36)) = 0 Then b(a(36)) = a(36): c(36) = a(36) Else GoTo 360
a(86) = Pr7 - a(36): If b(a(86)) = 0 Then b(a(86)) = a(86): c(86) = a(86) Else GoTo 860
For j30 = m1 To m2 'a(30)
If b1(a1(j30)) = 0 Then GoTo 300
If b(a1(j30)) = 0 Then b(a1(j30)) = a1(j30): c(30) = a1(j30) Else GoTo 300
a(30) = a1(j30)
a(80) = Pr7 - a(30): If b(a(80)) = 0 Then b(a(80)) = a(80): c(80) = a(80) Else GoTo 800
a(29) = s4 - a(30) - a(31) - a(32)
If a(29) < a1(m1) Or a(29) > a1(m2) Then GoTo 290
If b1(a(29)) = 0 Then GoTo 290
If b(a(29)) = 0 Then b(a(29)) = a(29): c(29) = a(29) Else GoTo 290
a(79) = Pr7 - a(29): If b(a(79)) = 0 Then b(a(79)) = a(79): c(79) = a(79) Else GoTo 790
For j23 = m1 To m2 'a(23)
If b1(a1(j23)) = 0 Then GoTo 230
If b(a1(j23)) = 0 Then b(a1(j23)) = a1(j23): c(23) = a1(j23) Else GoTo 230
a(23) = a1(j23)
a(73) = Pr7 - a(23): If b(a(73)) = 0 Then b(a(73)) = a(73): c(73) = a(73) Else GoTo 730
a(22) = s4 - a(23) - a(24) - a(25)
If a(22) < a1(m1) Or a(22) > a1(m2) Then GoTo 220
If b1(a(22)) = 0 Then GoTo 220
If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
a(72) = Pr7 - a(22): If b(a(72)) = 0 Then b(a(72)) = a(72): c(72) = a(72) Else GoTo 720
For j16 = m1 To m2 'a(16)
If b1(a1(j16)) = 0 Then GoTo 160
If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 160
a(16) = a1(j16)
a(66) = Pr7 - a(16): If b(a(66)) = 0 Then b(a(66)) = a(66): c(66) = a(66) Else GoTo 660
a(15) = s4 - a(16) - a(17) - a(18)
If a(15) < a1(m1) Or a(15) > a1(m2) Then GoTo 150
If b1(a(15)) = 0 Then GoTo 150
If b(a(15)) = 0 Then b(a(15)) = a(15): c(15) = a(15) Else GoTo 150
a(65) = Pr7 - a(15): If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650
a(9) = s7 - a(2) - a(16) - a(23) - a(30) - a(37) - a(44)
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(59) = Pr7 - a(9): If b(a(59)) = 0 Then b(a(59)) = a(59): c(59) = a(59) Else GoTo 590
a(8) = s7 - a(9) - a(10) - a(11) - a(12) - a(13) - a(14)
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(58) = Pr7 - a(8): If b(a(58)) = 0 Then b(a(58)) = a(58): c(58) = a(58) Else GoTo 580
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(58)) = 0: c(58) = 0
580 b(c(8)) = 0: c(8) = 0
80 b(c(59)) = 0: c(59) = 0
590 b(c(9)) = 0: c(9) = 0
90 b(c(65)) = 0: c(65) = 0
650 b(c(15)) = 0: c(15) = 0
150 b(c(66)) = 0: c(66) = 0
660 b(c(16)) = 0: c(16) = 0
160 Next j16
b(c(72)) = 0: c(72) = 0
720 b(c(22)) = 0: c(22) = 0
220 b(c(73)) = 0: c(73) = 0
730 b(c(23)) = 0: c(23) = 0
230 Next j23
b(c(79)) = 0: c(79) = 0
790 b(c(29)) = 0: c(29) = 0
290 b(c(80)) = 0: c(80) = 0
800 b(c(30)) = 0: c(30) = 0
300 Next j30
b(c(86)) = 0: c(86) = 0
860 b(c(36)) = 0: c(36) = 0
360 b(c(87)) = 0: c(87) = 0
870 b(c(37)) = 0: c(37) = 0
370 Next j37
b(c(60)) = 0: c(60) = 0
600 b(c(10)) = 0: c(10) = 0
100 b(c(67)) = 0: c(67) = 0
670 b(c(17)) = 0: c(17) = 0
170 Next j17
b(c(74)) = 0: c(74) = 0
740 b(c(24)) = 0: c(24) = 0
240 Next j24
b(c(81)) = 0: c(81) = 0
810 b(c(31)) = 0: c(31) = 0
310 Next j31
b(c(88)) = 0: c(88) = 0
880 b(c(38)) = 0: c(38) = 0
380 Next j38
b(c(61)) = 0: c(61) = 0
610 b(c(11)) = 0: c(11) = 0
110 b(c(68)) = 0: c(68) = 0
680 b(c(18)) = 0: c(18) = 0
180 Next j18
b(c(75)) = 0: c(75) = 0
750 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(82)) = 0: c(82) = 0
820 b(c(32)) = 0: c(32) = 0
320 Next j32
b(c(89)) = 0: c(89) = 0
890 b(c(39)) = 0: c(39) = 0
390 Next j39
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 PrimeCubes7d")
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
' Remove used primes and complements from available primes
950 For i1 = 1 To 49
b1(a(i1)) = 0
b1(Pr7 - a(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
Erase a
For i1 = 1 To 49
a(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
Next i1
GoSub 950 'Remove used primes and complements from available primes
' Read Back Square 7 x 7 (Partly Completed)
Erase a
For i1 = 1 To 49
a(i1) = Sheets(ShtNm3).Cells(j100, i1).Value
Next i1
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