' Generates Inlaid Magic Squares of order 12
' Based on Order 3 Sub Squares with Different Magic Sums
' Tested with Office 2007 under Windows 7
Sub Priem12c()
Dim a1(550), a(16), a12(144), b1(25355), b(25355), c(16)
y = MsgBox("Locked", vbCritical, "Routine Priem12c")
End
n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
Sheets("Klad1").Select
t1 = Timer
For j200 = 2 To 69
s12 = Sheets("Input12").Cells(j200, 34).Value
Erase a12
'Define Center Elements
For i1 = 1 To 16
a(i1) = Sheets("Input12").Cells(j200, i1).Value
Next i1
a12(14) = a(1): a12(17) = a(2): a12(20) = a(3): a12(23) = a(4):
a12(50) = a(5): a12(53) = a(6): a12(56) = a(7): a12(59) = a(8):
a12(86) = a(9): a12(89) = a(10): a12(92) = a(11): a12(95) = a(12):
a12(122) = a(13): a12(125) = a(14): a12(128) = a(15): a12(131) = a(16):
Erase a
For j101 = 18 To 33
j100 = Sheets("Input12").Cells(j200, j101).Value
' Define variables
Cntr3 = Sheets("Pairs7").Cells(j100, 6).Value 'Mid
s1 = 3 * Cntr3
nVar1 = Sheets("Pairs7").Cells(j100, 9).Value
For i1 = 1 To nVar1
a1(i1) = Sheets("Pairs7").Cells(j100, 9 + i1).Value
Next i1
m1 = 1: m2 = nVar1
If a1(1) = 1 Then m1 = 2: m2 = m2 - 1
Erase b1
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
' Remove Used Primes
For i1 = 1 To 144
b1(a12(i1)) = 0
Next i1
' Generate Squares
For j9 = m1 To m2 'a(9)
If b1(a1(j9)) = 0 Then GoTo 160
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 160
a(9) = a1(j9)
For j8 = m1 To m2 'a(8)
If b1(a1(j8)) = 0 Then GoTo 120
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 120
a(8) = a1(j8)
a(7) = s1 - a(8) - a(9):
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
If b1(a(7)) = 0 Then GoTo 110
a(6) = 4 * s1 / 3 - a(8) - 2 * a(9):
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
If b1(a(6)) = 0 Then GoTo 110
a(5) = s1 / 3:
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 110:
''If b1(a(5)) = 0 Then GoTo 110
a(4) = -2 * s1 / 3 + a(8) + 2 * a(9):
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 110:
If b1(a(4)) = 0 Then GoTo 110
a(3) = -s1 / 3 + a(8) + a(9):
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
If b1(a(3)) = 0 Then GoTo 110
a(2) = 2 * s1 / 3 - a(8):
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 110:
If b1(a(2)) = 0 Then GoTo 110
a(1) = 2 * s1 / 3 - a(9):
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
If b1(a(1)) = 0 Then GoTo 110
' Exclude solutions with identical numbers
GoSub 800: If fl1 = 0 Then GoTo 110
n10 = n10 + 1: GoSub 750 'Assign to a12()
Erase b, c: GoTo 5 'Assign only first square
110 b(c(8)) = 0: c(8) = 0
120 Next j8
b(c(9)) = 0: c(9) = 0
160 Next j9
' Not found
Erase b, c: n10 = 0: GoTo 2000
5
If n10 = 16 Then
GoSub 850:
If fl1 = 1 Then
n9 = n9 + 1: GoSub 660 'Print results (squares)
n10 = 0: GoTo 2000
End If
End If
Next j101
2000 n10 = 0
Next j200
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine Priem12c")
End
' Assign to a12()
750 Select Case n10
Case 1
a12(1) = a(1): a12(2) = a(2): a12(3) = a(3):
a12(13) = a(4): a12(14) = a(5): a12(15) = a(6):
a12(25) = a(7): a12(26) = a(8): a12(27) = a(9):
Case 2
a12(4) = a(1): a12(5) = a(2): a12(6) = a(3):
a12(16) = a(4): a12(17) = a(5): a12(18) = a(6):
a12(28) = a(7): a12(29) = a(8): a12(30) = a(9):
Case 3
a12(7) = a(1): a12(8) = a(2): a12(9) = a(3):
a12(19) = a(4): a12(20) = a(5): a12(21) = a(6):
a12(31) = a(7): a12(32) = a(8): a12(33) = a(9):
Case 4
a12(10) = a(1): a12(11) = a(2): a12(12) = a(3):
a12(22) = a(4): a12(23) = a(5): a12(24) = a(6):
a12(34) = a(7): a12(35) = a(8): a12(36) = a(9):
Case 5
a12(37) = a(1): a12(38) = a(2): a12(39) = a(3):
a12(49) = a(4): a12(50) = a(5): a12(51) = a(6):
a12(61) = a(7): a12(62) = a(8): a12(63) = a(9):
Case 6
a12(40) = a(1): a12(41) = a(2): a12(42) = a(3):
a12(52) = a(4): a12(53) = a(5): a12(54) = a(6):
a12(64) = a(7): a12(65) = a(8): a12(66) = a(9):
Case 7
a12(43) = a(1): a12(44) = a(2): a12(45) = a(3):
a12(55) = a(4): a12(56) = a(5): a12(57) = a(6):
a12(67) = a(7): a12(68) = a(8): a12(69) = a(9):
Case 8
a12(46) = a(1): a12(47) = a(2): a12(48) = a(3):
a12(58) = a(4): a12(59) = a(5): a12(60) = a(6):
a12(70) = a(7): a12(71) = a(8): a12(72) = a(9):
Case 9
a12(73) = a(1): a12(74) = a(2): a12(75) = a(3):
a12(85) = a(4): a12(86) = a(5): a12(87) = a(6):
a12(97) = a(7): a12(98) = a(8): a12(99) = a(9):
Case 10
a12(76) = a(1): a12(77) = a(2): a12(78) = a(3):
a12(88) = a(4): a12(89) = a(5): a12(90) = a(6):
a12(100) = a(7): a12(101) = a(8): a12(102) = a(9):
Case 11
a12(79) = a(1): a12(80) = a(2): a12(81) = a(3):
a12(91) = a(4): a12(92) = a(5): a12(93) = a(6):
a12(103) = a(7): a12(104) = a(8): a12(105) = a(9):
Case 12
a12(82) = a(1): a12(83) = a(2): a12(84) = a(3):
a12(94) = a(4): a12(95) = a(5): a12(96) = a(6):
a12(106) = a(7): a12(107) = a(8): a12(108) = a(9):
Case 13
a12(109) = a(1): a12(110) = a(2): a12(111) = a(3):
a12(121) = a(4): a12(122) = a(5): a12(123) = a(6):
a12(133) = a(7): a12(134) = a(8): a12(135) = a(9):
Case 14
a12(112) = a(1): a12(113) = a(2): a12(114) = a(3):
a12(124) = a(4): a12(125) = a(5): a12(126) = a(6):
a12(136) = a(7): a12(137) = a(8): a12(138) = a(9):
Case 15
a12(115) = a(1): a12(116) = a(2): a12(117) = a(3):
a12(127) = a(4): a12(128) = a(5): a12(129) = a(6):
a12(139) = a(7): a12(140) = a(8): a12(141) = a(9):
Case 16
a12(118) = a(1): a12(119) = a(2): a12(120) = a(3):
a12(130) = a(4): a12(131) = a(5): a12(132) = a(6):
a12(142) = a(7): a12(143) = a(8): a12(144) = a(9):
End Select
Return
' Print results: squares a12()
660 n1 = n1 + 1
If n1 = 2 Then
n1 = 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 = 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
' Exclude solutions with identical numbers a()
800 fl1 = 1
For j1 = 1 To 9
a2 = a(j1)
For j2 = (1 + j1) To 9
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers a12()
850 fl1 = 1
For j1 = 1 To 144
a2 = a12(j1): If a2 = 0 Then GoTo 860
For j2 = (1 + j1) To 144
If a2 = a12(j2) Then fl1 = 0: Return
Next j2
860 Next j1
Return
End Sub