' Generates Inlaid Magic Squares of order 12 (Part 2)
' Sub Squares with Different Magic Sums
' Tested with Office 2007 under Windows 7
Sub Priem12k()
Dim a1(1260), b1(187141), b(187141), c(144), a(25), a12(144), s(4)
y = MsgBox("Locked", vbCritical, "Routine Priem12k")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
' Generate Squares
For j101 = 43 To 46
' Read and Assign Center Squares
Pr12 = Sheets("Sqrs5").Cells(j101, 16).Value 'Pair Sum
s12 = 6 * Pr12 'MC12
For i10 = 1 To 4
i30 = Sheets("Sqrs5").Cells(j101, i10).Value
For i20 = 1 To 25
a(i20) = Sheets("Lines5").Cells(i30, i20).Value
Next i20
GoSub 750 'Assign Center Squares to a12()
Next i10
GoSub 1850 'Check Identical Integers
If fl1 = 0 Then GoTo 500
Rcrd12 = Sheets("Sqrs5").Cells(j101, 17).Value 'Record Border Pairs
If Rcrd12 = 0 Then GoTo 500
' Read MC5's
For i1 = 5 To 8
s(i1 - 4) = Sheets("Sqrs5").Cells(j101, i1).Value
Next i1
' Read Border Primes
' Pairs72 Pairs8 Pairs6
nVar1 = Sheets("Pairs6").Cells(Rcrd12, 5).Value ' 9 5 5
Erase b1
For i1 = 1 To nVar1
x = Sheets("Pairs6").Cells(Rcrd12, 10 + i1).Value ' 9 + i1 6 + i1 10 + i1
b1(x) = x
Next i1
pMax = Sheets("Pairs6").Cells(Rcrd12, 10 + nVar1).Value
' Remove used Primes
For i1 = 1 To 144
b1(a12(i1)) = 0
If Pr12 - a12(i1) > 0 Then
b1(Pr12 - a12(i1)) = 0
End If
Next i1
' 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
GoSub 2000 'Select Border
If fl1 = 0 Then GoTo 500
GoSub 1850 'Double Check Identical Integers
If fl1 = 1 Then
' n9 = n9 + 1: GoSub 645 'Print Selected Numbers
n9 = n9 + 1: GoSub 650 'Print Composed Squares
End If
500 Erase b, c, a12
Next j101
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem12k")
End
' Assign Sub Squares
750 Select Case i10
Case 1 'Top / Left
a12(14) = a(1): a12(15) = a(2): a12(16) = a(3): a12(17) = a(4): a12(18) = a(5):
a12(26) = a(6): a12(27) = a(7): a12(28) = a(8): a12(29) = a(9): a12(30) = a(10):
a12(38) = a(11): a12(39) = a(12): a12(40) = a(13): a12(41) = a(14): a12(42) = a(15):
a12(50) = a(16): a12(51) = a(17): a12(52) = a(18): a12(53) = a(19): a12(54) = a(20):
a12(62) = a(21): a12(63) = a(22): a12(64) = a(23): a12(65) = a(24): a12(66) = a(25):
Case 2 'Top / Right
a12(19) = a(1): a12(20) = a(2): a12(21) = a(3): a12(22) = a(4): a12(23) = a(5):
a12(31) = a(6): a12(32) = a(7): a12(33) = a(8): a12(34) = a(9): a12(35) = a(10):
a12(43) = a(11): a12(44) = a(12): a12(45) = a(13): a12(46) = a(14): a12(47) = a(15):
a12(55) = a(16): a12(56) = a(17): a12(57) = a(18): a12(58) = a(19): a12(59) = a(20):
a12(67) = a(21): a12(68) = a(22): a12(69) = a(23): a12(70) = a(24): a12(71) = a(25):
Case 3 'Bottom / Left
a12(74) = a(1): a12(75) = a(2): a12(76) = a(3): a12(77) = a(4): a12(78) = a(5):
a12(86) = a(6): a12(87) = a(7): a12(88) = a(8): a12(89) = a(9): a12(90) = a(10):
a12(98) = a(11): a12(99) = a(12): a12(100) = a(13): a12(101) = a(14): a12(102) = a(15):
a12(110) = a(16): a12(111) = a(17): a12(112) = a(18): a12(113) = a(19): a12(114) = a(20):
a12(122) = a(21): a12(123) = a(22): a12(124) = a(23): a12(125) = a(24): a12(126) = a(25):
Case 4 'Bottom / Right
a12(79) = a(1): a12(80) = a(2): a12(81) = a(3): a12(82) = a(4): a12(83) = a(5):
a12(91) = a(6): a12(92) = a(7): a12(93) = a(8): a12(94) = a(9): a12(95) = a(10):
a12(103) = a(11): a12(104) = a(12): a12(105) = a(13): a12(106) = a(14): a12(107) = a(15):
a12(115) = a(16): a12(116) = a(17): a12(117) = a(18): a12(118) = a(19): a12(119) = a(20):
a12(127) = a(21): a12(128) = a(22): a12(129) = a(23): a12(130) = a(24): a12(131) = a(25):
End Select
Return
' Exclude solutions with identical numbers a12()
1850 fl1 = 1
For j1 = 1 To 144
a20 = a12(j1): If a20 = 0 Then GoTo 1860
For j2 = (1 + j1) To 144
If a20 = a12(j2) Then fl1 = 0: Return
Next j2
1860 Next j1
Return
' Print results (selected numbers)
645 For i1 = 1 To 144
Cells(n9, i1).Value = a12(i1)
Next i1
Return
' Print results (squares)
650 n2 = n2 + 1
If n2 = 2 Then
n2 = 1: k1 = k1 + 13: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 13
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = "MC = " + CStr(s12)
i3 = 0
For i1 = 1 To 12
For i2 = 1 To 12
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a12(i3)
Next i2
Next i1
Return
' Select Border
2000 fl1 = 0
For j144 = m1 To m2 'a12(144)
If b1(a1(j144)) = 0 Then GoTo 1440
If b(a1(j144)) = 0 Then b(a1(j144)) = a1(j144): c(144) = a1(j144) Else GoTo 1440
a12(144) = a1(j144)
a12(1) = Pr12 - a12(144): If b(a12(1)) = 0 Then b(a12(1)) = a12(1): c(1) = a12(1) Else GoTo 10
For j143 = m1 To m2 'a12(143)
If b1(a1(j143)) = 0 Then GoTo 1430
If b(a1(j143)) = 0 Then b(a1(j143)) = a1(j143): c(143) = a1(j143) Else GoTo 1430
a12(143) = a1(j143)
a12(134) = a12(143) - s(3) + s(4)
If a12(134) < a1(m1) Or a12(134) > a1(m2) Then GoTo 1340
If b1(a12(134)) = 0 Then GoTo 1340
If b(a12(134)) = 0 Then b(a12(134)) = a12(134): c(134) = a12(134) Else GoTo 1340
a12(11) = Pr12 - a12(134): If b(a12(11)) = 0 Then b(a12(11)) = a12(11): c(11) = a12(11) Else GoTo 110
a12(2) = Pr12 - a12(143): If b(a12(2)) = 0 Then b(a12(2)) = a12(2): c(2) = a12(2) Else GoTo 20
For j142 = m1 To m2 'a12(142)
If b1(a1(j142)) = 0 Then GoTo 1420
If b(a1(j142)) = 0 Then b(a1(j142)) = a1(j142): c(142) = a1(j142) Else GoTo 1420
a12(142) = a1(j142)
a12(135) = a12(142) - s(3) + s(4)
If a12(135) < a1(m1) Or a12(135) > a1(m2) Then GoTo 1350
If b1(a12(135)) = 0 Then GoTo 1350
If b(a12(135)) = 0 Then b(a12(135)) = a12(135): c(135) = a12(135) Else GoTo 1350
a12(10) = Pr12 - a12(135): If b(a12(10)) = 0 Then b(a12(10)) = a12(10): c(10) = a12(10) Else GoTo 100
a12(3) = Pr12 - a12(142): If b(a12(3)) = 0 Then b(a12(3)) = a12(3): c(3) = a12(3) Else GoTo 30
For j141 = m1 To m2 'a12(141)
If b1(a1(j141)) = 0 Then GoTo 1410
If b(a1(j141)) = 0 Then b(a1(j141)) = a1(j141): c(141) = a1(j141) Else GoTo 1410
a12(141) = a1(j141)
a12(136) = a12(141) - s(3) + s(4)
If a12(136) < a1(m1) Or a12(136) > a1(m2) Then GoTo 1360
If b1(a12(136)) = 0 Then GoTo 1360
If b(a12(136)) = 0 Then b(a12(136)) = a12(136): c(136) = a12(136) Else GoTo 1360
a12(9) = Pr12 - a12(136): If b(a12(9)) = 0 Then b(a12(9)) = a12(9): c(9) = a12(9) Else GoTo 90
a12(4) = Pr12 - a12(141): If b(a12(4)) = 0 Then b(a12(4)) = a12(4): c(4) = a12(4) Else GoTo 40
For j140 = m1 To m2 'a12(140)
If b1(a1(j140)) = 0 Then GoTo 1400
If b(a1(j140)) = 0 Then b(a1(j140)) = a1(j140): c(140) = a1(j140) Else GoTo 1400
a12(140) = a1(j140)
a12(137) = a12(140) - s(3) + s(4)
If a12(137) < a1(m1) Or a12(137) > a1(m2) Then GoTo 1370
If b1(a12(137)) = 0 Then GoTo 1370
If b(a12(137)) = 0 Then b(a12(137)) = a12(137): c(137) = a12(137) Else GoTo 1370
a12(8) = Pr12 - a12(137): If b(a12(8)) = 0 Then b(a12(8)) = a12(8): c(8) = a12(8) Else GoTo 80
a12(5) = Pr12 - a12(140): If b(a12(5)) = 0 Then b(a12(5)) = a12(5): c(5) = a12(5) Else GoTo 50
For j139 = m1 To m2 'a12(139)
If b1(a1(j139)) = 0 Then GoTo 1390
If b(a1(j139)) = 0 Then b(a1(j139)) = a1(j139): c(139) = a1(j139) Else GoTo 1390
a12(139) = a1(j139)
a12(138) = a12(139) - s(3) + s(4)
If a12(138) < a1(m1) Or a12(138) > a1(m2) Then GoTo 1380
If b1(a12(138)) = 0 Then GoTo 1380
If b(a12(138)) = 0 Then b(a12(138)) = a12(138): c(138) = a12(138) Else GoTo 1380
a12(133) = s12-2*a12(139)-2*a12(140)-2*a12(141)-2*a12(142)-2*a12(143)-a12(144)+5*s(3)-5*s(4)
If a12(133) < a1(m1) Or a12(133) > a1(m2) Then GoTo 1330
If b1(a12(133)) = 0 Then GoTo 1330
If b(a12(133)) = 0 Then b(a12(133)) = a12(133): c(133) = a12(133) Else GoTo 1330
a12(12) = Pr12 - a12(133): If b(a12(12)) = 0 Then b(a12(12)) = a12(12): c(12) = a12(12) Else GoTo 120
a12(7) = Pr12 - a12(138): If b(a12(7)) = 0 Then b(a12(7)) = a12(7): c(7) = a12(7) Else GoTo 70
a12(6) = Pr12 - a12(139): If b(a12(6)) = 0 Then b(a12(6)) = a12(6): c(6) = a12(6) Else GoTo 60
For j132 = m1 To m2 'a12(132)
If b1(a1(j132)) = 0 Then GoTo 1320
If b(a1(j132)) = 0 Then b(a1(j132)) = a1(j132): c(132) = a1(j132) Else GoTo 1320
a12(132) = a1(j132)
a12(121) = s12 - a12(132) - s(3) - s(4)
If a12(121) < a1(m1) Or a12(121) > a1(m2) Then GoTo 1210
If b1(a12(121)) = 0 Then GoTo 1210
If b(a12(121)) = 0 Then b(a12(121)) = a12(121): c(121) = a12(121) Else GoTo 1210
a12(24) = Pr12 - a12(121): If b(a12(24)) = 0 Then b(a12(24)) = a12(24): c(24) = a12(24) Else GoTo 240
a12(13) = Pr12 - a12(132): If b(a12(13)) = 0 Then b(a12(13)) = a12(13): c(13) = a12(13) Else GoTo 130
For j120 = m1 To m2 'a12(120)
If b1(a1(j120)) = 0 Then GoTo 1200
If b(a1(j120)) = 0 Then b(a1(j120)) = a1(j120): c(120) = a1(j120) Else GoTo 1200
a12(120) = a1(j120)
a12(109) = s12 - a12(120) - s(3) - s(4)
If a12(109) < a1(m1) Or a12(109) > a1(m2) Then GoTo 1090
If b1(a12(109)) = 0 Then GoTo 1090
If b(a12(109)) = 0 Then b(a12(109)) = a12(109): c(109) = a12(109) Else GoTo 1090
a12(36) = Pr12 - a12(109): If b(a12(36)) = 0 Then b(a12(36)) = a12(36): c(36) = a12(36) Else GoTo 360
a12(25) = Pr12 - a12(120): If b(a12(25)) = 0 Then b(a12(25)) = a12(25): c(25) = a12(25) Else GoTo 250
For j108 = m1 To m2 'a12(108)
If b1(a1(j108)) = 0 Then GoTo 1080
If b(a1(j108)) = 0 Then b(a1(j108)) = a1(j108): c(108) = a1(j108) Else GoTo 1080
a12(108) = a1(j108)
a12(97) = s12 - a12(108) - s(3) - s(4)
If a12(97) < a1(m1) Or a12(97) > a1(m2) Then GoTo 970
If b1(a12(97)) = 0 Then GoTo 970
If b(a12(97)) = 0 Then b(a12(97)) = a12(97): c(97) = a12(97) Else GoTo 970
a12(48) = Pr12 - a12(97): If b(a12(48)) = 0 Then b(a12(48)) = a12(48): c(48) = a12(48) Else GoTo 480
a12(37) = Pr12 - a12(108): If b(a12(37)) = 0 Then b(a12(37)) = a12(37): c(37) = a12(37) Else GoTo 370
For j96 = m1 To m2 'a12(96)
If b1(a1(j96)) = 0 Then GoTo 960
If b(a1(j96)) = 0 Then b(a1(j96)) = a1(j96): c(96) = a1(j96) Else GoTo 960
a12(96) = a1(j96)
a12(85) = s12 - a12(96) - s(3) - s(4)
If a12(85) < a1(m1) Or a12(85) > a1(m2) Then GoTo 850
If b1(a12(85)) = 0 Then GoTo 850
If b(a12(85)) = 0 Then b(a12(85)) = a12(85): c(85) = a12(85) Else GoTo 850
a12(84) = 3*s12-a12(96)-a12(108)-a12(120)-a12(132)-a12(139)-a12(140)-a12(141)-a12(142)-a12(143)-a12(144)-5*s(4)
If a12(84) < a1(m1) Or a12(84) > a1(m2) Then GoTo 840
If b1(a12(84)) = 0 Then GoTo 840
If b(a12(84)) = 0 Then b(a12(84)) = a12(84): c(84) = a12(84) Else GoTo 840
a12(73) = s12 - a12(84) - s(3) - s(4)
If a12(73) < a1(m1) Or a12(73) > a1(m2) Then GoTo 730
If b1(a12(73)) = 0 Then GoTo 730
If b(a12(73)) = 0 Then b(a12(73)) = a12(73): c(73) = a12(73) Else GoTo 730
a12(72) = Pr12 - a12(73): If b(a12(72)) = 0 Then b(a12(72)) = a12(72): c(72) = a12(72) Else GoTo 720
a12(61) = Pr12 - a12(84): If b(a12(61)) = 0 Then b(a12(61)) = a12(61): c(61) = a12(61) Else GoTo 610
a12(60) = Pr12 - a12(85): If b(a12(60)) = 0 Then b(a12(60)) = a12(60): c(60) = a12(60) Else GoTo 600
a12(49) = Pr12 - a12(96): If b(a12(49)) = 0 Then b(a12(49)) = a12(49): c(49) = a12(49) Else GoTo 490
fl1 = 1: Return
b(c(49)) = 0: c(49) = 0
490 b(c(60)) = 0: c(60) = 0
600 b(c(61)) = 0: c(61) = 0
610 b(c(72)) = 0: c(72) = 0
720 b(c(73)) = 0: c(73) = 0
730 b(c(84)) = 0: c(84) = 0
840 b(c(85)) = 0: c(85) = 0
850 b(c(96)) = 0: c(96) = 0
960 Next j96
b(c(37)) = 0: c(37) = 0
370 b(c(48)) = 0: c(48) = 0
480 b(c(97)) = 0: c(97) = 0
970 b(c(108)) = 0: c(108) = 0
1080 Next j108
b(c(25)) = 0: c(25) = 0
250 b(c(36)) = 0: c(36) = 0
360 b(c(109)) = 0: c(109) = 0
1090 b(c(120)) = 0: c(120) = 0
1200 Next j120
b(c(13)) = 0: c(13) = 0
130 b(c(24)) = 0: c(24) = 0
240 b(c(121)) = 0: c(121) = 0
1210 b(c(132)) = 0: c(132) = 0
1320 Next j132
b(c(6)) = 0: c(6) = 0
60 b(c(7)) = 0: c(7) = 0
70 b(c(12)) = 0: c(12) = 0
120 b(c(133)) = 0: c(133) = 0
1330 b(c(138)) = 0: c(138) = 0
1380 b(c(139)) = 0: c(139) = 0
1390 Next j139
b(c(5)) = 0: c(5) = 0
50 b(c(8)) = 0: c(8) = 0
80 b(c(137)) = 0: c(137) = 0
1370 b(c(140)) = 0: c(140) = 0
1400 Next j140
b(c(4)) = 0: c(4) = 0
40 b(c(9)) = 0: c(9) = 0
90 b(c(136)) = 0: c(136) = 0
1360 b(c(141)) = 0: c(141) = 0
1410 Next j141
b(c(3)) = 0: c(3) = 0
30 b(c(10)) = 0: c(10) = 0
100 b(c(135)) = 0: c(135) = 0
1350 b(c(142)) = 0: c(142) = 0
1420 Next j142
b(c(2)) = 0: c(2) = 0
20 b(c(11)) = 0: c(11) = 0
110 b(c(134)) = 0: c(134) = 0
1340 b(c(143)) = 0: c(143) = 0
1430 Next j143
b(c(1)) = 0: c(1) = 0
10 b(c(144)) = 0: c(144) = 0
1440 Next j144
fl1 = 0
Return
End Sub