' Constructs 10 x 10 Magic Squares with Non Overlapping Subsquares (Distinct Prime Numbers):
' - Reads Prime Number (Symmetrical) Magic Squares (6 x 6)
' - Generates additional Prime Number Pan Magic Squares (4 x 4)
' - Completes the 10 x 10 Magic Squares
' Tested with Office 2007 under Windows 7
Sub Priem10a1()
Dim a1(1944), a(100), b1(43300), b(43300), c(100)
Dim a4(16), b4(16), c4(16), d6(36) 'Sub Squares
y = MsgBox("Locked", vbCritical, "Routine Priem10a1")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0
Sht1 = "Pairs8"
' Generate Squares
Sheets("Klad1").Select
t1 = Timer
For j100 = 9 To 88
' Start Reading Data
Rcrd1 = Sheets("Lines6").Cells(j100, 38).Value
MC6 = Sheets("Lines6").Cells(j100, 37).Value
MC10 = 5 * MC6 / 3
' Read Prime Numbers used for PM6
For i1 = 1 To 36
a(i1) = Sheets("Lines6").Cells(j100, i1).Value
Next i1
GoSub 2010 'Read Prime Numbers From Sheet Sht1
n8 = 36: GoSub 950 'Remove used primes from available primes
For i1 = 1 To 36: d6(i1) = a(i1): Next i1
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)
For j15 = m1 To m2 'a(15)
If b1(a1(j15)) = 0 Then GoTo 150
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a(15) = a1(j15)
For j14 = m1 To m2 'a(14)
If b1(a1(j14)) = 0 Then GoTo 140
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
a(14) = a1(j14)
a(13) = s1 - a(14) - a(15) - a(16)
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
For j12 = m1 To m2 'a(12)
If b1(a1(j12)) = 0 Then GoTo 120
If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
a(12) = a1(j12)
a(11) = s1 - a(12) - a(15) - a(16)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 70
If b1(a(11)) = 0 Then GoTo 70
a(10) = a(12) - a(14) + a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 70
If b1(a(10)) = 0 Then GoTo 70
a(9) = -a(12) + a(14) + a(15)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 70
If b1(a(9)) = 0 Then GoTo 70
a(8) = 0.5 * s1 - a(14)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 70
If b1(a(8)) = 0 Then GoTo 70
a(7) = -0.5 * s1 + a(14) + a(15) + a(16)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70:
If b1(a(7)) = 0 Then GoTo 70
a(6) = 0.5 * s1 - a(16)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 70:
If b1(a(6)) = 0 Then GoTo 70
a(5) = 0.5 * s1 - a(15)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 70:
If b1(a(5)) = 0 Then GoTo 70
a(4) = 0.5 * s1 - a(12) + a(14) - a(16)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 70:
If b1(a(4)) = 0 Then GoTo 70
a(3) = 0.5 * s1 + a(12) - a(14) - a(15)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 70:
If b1(a(3)) = 0 Then GoTo 70
a(2) = 0.5 * s1 - a(12)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 70:
If b1(a(2)) = 0 Then GoTo 70
a(1) = -0.5 * s1 + a(12) + a(15) + a(16)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 70:
If b1(a(1)) = 0 Then GoTo 70
' Exclude solutions with identical numbers (PM4)
n8 = 16: GoSub 800: If fl1 = 0 Then GoTo 70
n10 = n10 + 1
Select Case n10
Case 1: n8 = 16: For i1 = 1 To 16: a4(i1) = a(i1): Next i1: GoSub 950
Case 2: n8 = 16: For i1 = 1 To 16: b4(i1) = a(i1): Next i1: GoSub 950
Case 3: n8 = 16: For i1 = 1 To 16: c4(i1) = a(i1): Next i1: GoSub 950
End Select
If n10 = 3 Then
GoSub 600 'Compose Main Square
GoSub 1200 'Complete Main Square
If fl2 = 1 Then
n8 = 100: GoSub 800 'Double Check Identical Integers
If fl1 = 1 Then
n9 = n9 + 1: GoSub 1650 'Print Composed Square
End If
End If
Erase b, c: n10 = 0: GoTo 10
Else
Erase b, c: GoTo 160 'Continue search for next Sub Square
End If
70 b(c(12)) = 0: c(12) = 0
120 Next j12
b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 Next j14
b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(16)) = 0: c(16) = 0
160 Next j16
n10 = 0
10 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s10)
y = MsgBox(t10, 0, "Routine Priem10a1")
End
' Compose Main Square
600 Erase a
' Symmetric Sub Square 6 x 6
a(45) = d6(1): a(46) = d6(2): a(47) = d6(3): a(48) = d6(4): a(49) = d6(5): a(50) = d6(6):
a(55) = d6(7): a(56) = d6(8): a(57) = d6(9): a(58) = d6(10): a(59) = d6(11): a(60) = d6(12):
a(65) = d6(13): a(66) = d6(14): a(67) = d6(15): a(68) = d6(16): a(69) = d6(17): a(70) = d6(18):
a(75) = d6(19): a(76) = d6(20): a(77) = d6(21): a(78) = d6(22): a(79) = d6(23): a(80) = d6(24):
a(85) = d6(25): a(86) = d6(26): a(87) = d6(27): a(88) = d6(28): a(89) = d6(29): a(90) = d6(30):
a(95) = d6(31): a(96) = d6(32): a(97) = d6(33): a(98) = d6(34): a(99) = d6(35): a(100) = d6(36):
' Pan Magic Sub Squares 4 x 4
a(41) = a4(1): a(42) = a4(2): a(43) = a4(3): a(44) = a4(4):
a(51) = a4(5): a(52) = a4(6): a(53) = a4(7): a(54) = a4(8):
a(61) = a4(9): a(62) = a4(10): a(63) = a4(11): a(64) = a4(12):
a(71) = a4(13): a(72) = a4(14): a(73) = a4(15): a(74) = a4(16):
a(1) = b4(1): a(2) = b4(2): a(3) = b4(3): a(4) = b4(4):
a(11) = b4(5): a(12) = b4(6): a(13) = b4(7): a(14) = b4(8):
a(21) = b4(9): a(22) = b4(10): a(23) = b4(11): a(24) = b4(12):
a(31) = b4(13): a(32) = b4(14): a(33) = b4(15): a(34) = b4(16):
a(5) = c4(1): a(6) = c4(2): a(7) = c4(3): a(8) = c4(4):
a(15) = c4(5): a(16) = c4(6): a(17) = c4(7): a(18) = c4(8):
a(25) = c4(9): a(26) = c4(10): a(27) = c4(11): a(28) = c4(12):
a(35) = c4(13): a(36) = c4(14): a(37) = c4(15): a(38) = c4(16):
Return
' Exclude solutions with identical numbers a()
800 fl1 = 1
For j1 = 1 To n8
a2 = a(j1)
For j2 = (1 + j1) To n8
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Remove used primes from available primes
950 For j1 = 1 To n8
b1(a(j1)) = 0
Next j1
' Restore available primes in a1()
n20 = 0
For j1 = 1 To a20
If b1(j1) <> 0 Then
n20 = n20 + 1
a1(n20) = b1(j1)
End If
Next j1
m1 = 1: m2 = n20
Return
' Complete Main Square
1200 fl2 = 1
s2 = a(73) + a(64) + a(55) + a(46) + a(37) + a(28)
For j10 = m1 To m2 'a(10)
If b1(a1(j10)) = 0 Then GoTo 100
If b(a1(j10)) = 0 Then b(a1(j10)) = a1(j10): c(10) = a1(j10) Else GoTo 100
a(10) = a1(j10)
a(9) = s1 / 2 - a(10): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
For j19 = m1 To m2 'a(19)
If b1(a1(j19)) = 0 Then GoTo 190
If b(a1(j19)) = 0 Then b(a1(j19)) = a1(j19): c(19) = a1(j19) Else GoTo 190
a(19) = a1(j19)
a(20) = s1 / 2 - a(19): If b(a(20)) = 0 Then b(a(20)) = a(20): c(20) = a(20) Else GoTo 200
For j91 = m1 To m2 'a(91)
If b1(a1(j91)) = 0 Then GoTo 910
If b(a1(j91)) = 0 Then b(a1(j91)) = a1(j91): c(91) = a1(j91) Else GoTo 910
a(91) = a1(j91)
a(81) = s1 / 2 - a(91): If b(a(81)) = 0 Then b(a(81)) = a(81): c(81) = a(81) Else GoTo 810
For j82 = m1 To m2 'a(82)
If b1(a1(j82)) = 0 Then GoTo 820
If b(a1(j82)) = 0 Then b(a1(j82)) = a1(j82): c(82) = a1(j82) Else GoTo 820
a(82) = a1(j82)
If a(91) + a(82) + a(19) + a(10) <> s10 - s2 Then GoTo 920
a(92) = s1 / 2 - a(82): If b(a(92)) = 0 Then b(a(92)) = a(92): c(92) = a(92) Else GoTo 920
For j40 = m1 To m2 'a(40)
If b1(a1(j40)) = 0 Then GoTo 400
If b(a1(j40)) = 0 Then b(a1(j40)) = a1(j40): c(40) = a1(j40) Else GoTo 400
a(40) = a1(j40)
a(39) = s1 / 2 - a(40): If b(a(39)) = 0 Then b(a(39)) = a(39): c(39) = a(39) Else GoTo 390
a(29) = a(40) - a(19) + a(10)
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(30) = s1 / 2 - a(29): If b(a(30)) = 0 Then b(a(30)) = a(30): c(30) = a(30) Else GoTo 300
For j94 = m1 To m2 'a(94)
If b1(a1(j94)) = 0 Then GoTo 940
If b(a1(j94)) = 0 Then b(a1(j94)) = a1(j94): c(94) = a1(j94) Else GoTo 940
a(94) = a1(j94)
a(84) = s1 / 2 - a(94): If b(a(84)) = 0 Then b(a(84)) = a(84): c(84) = a(84) Else GoTo 840
a(83) = a(94) + a(91) - a(82)
If a(83) < a1(m1) Or a(83) > a1(m2) Then GoTo 830
If b1(a(83)) = 0 Then GoTo 830
If b(a(83)) = 0 Then b(a(83)) = a(83): c(83) = a(83) Else GoTo 830
a(93) = s1 / 2 - a(83): If b(a(93)) = 0 Then b(a(93)) = a(93): c(93) = a(93) Else GoTo 930
Return
b(c(93)) = 0: c(93) = 0
930 b(c(83)) = 0: c(83) = 0
830 b(c(84)) = 0: c(84) = 0
840 b(c(94)) = 0: c(94) = 0
940 Next j94
b(c(30)) = 0: c(30) = 0
300 b(c(29)) = 0: c(29) = 0
290 b(c(39)) = 0: c(39) = 0
390 b(c(40)) = 0: c(40) = 0
400 Next j40
b(c(92)) = 0: c(92) = 0
920 b(c(82)) = 0: c(82) = 0
820 Next j82
b(c(81)) = 0: c(81) = 0
810 b(c(91)) = 0: c(91) = 0
910 Next j91
b(c(20)) = 0: c(20) = 0
200 b(c(19)) = 0: c(19) = 0
190 Next j19
b(c(9)) = 0: c(9) = 0
90 b(c(10)) = 0: c(10) = 0
100 Next j10
fl2 = 0 'No solution found
Return
' Print results (lines)
1640 Cells(n9, 100).Select
For i1 = 1 To 100
Cells(n9, i1).Value = a(i1)
Next i1
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 11: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 11
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(MC10)
i3 = 0
For i1 = 1 To 10
For i2 = 1 To 10
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Read Prime Numbers From Sheet Sht1
2010 s1 = 2 * Sheets(Sht1).Cells(Rcrd1, 1).Value 'PM4
s10 = 10 * s1 / 4 'PM10
nVar = Sheets(Sht1).Cells(Rcrd1, 5).Value
nPM4 = Sheets(Sht1).Cells(Rcrd1, 6).Value
If s10 <> MC10 Then 'Check Sources
t10 = "Record :" + CStr(Rcrd1) + Chr(13)
t10 = t10 + "Line :" + CStr(j100)
y = MsgBox(t10, vbCritical, "Conflict in Data")
End
End If
m1 = 1: m2 = nVar
For i1 = m1 To m2
a1(i1) = Sheets(Sht1).Cells(Rcrd1, i1 + 6).Value
Next i1
a20 = a1(m2) 'Maximum
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
Return
End Sub