' Generates (Pan) Magic Squares of order 9 based on Sudoku Comparable Squares
' Tested with Office 365 under Windows 10
Sub CnstrSqrs9a()
Dim b(2, 81), a(81), c(81), s9(29)
Sheets("Klad1").Select
y = MsgBox("Locked", vbCritical, "Routine CnstrSqrs9a")
End
n4 = 1152
n2 = 0: n9 = 0: k1 = 1: k2 = 1
s1 = 369
t1 = Timer
n10 = 0: n11 = 0
For j1 = 1 To n4
For j2 = 1 To n4
If j2 = j1 Then GoTo 20
j10 = j1: j20 = 1: GoSub 100 'Read Sudoku Comparable Square 1
j10 = j2: j20 = 2: GoSub 100 'Read Sudoku Comparable Square 2
For j4 = 1 To 81
'' a(j4) = b(1, j4) + 9 * b(2, j4) + 1 'Batch 1
a(j4) = 9 * b(1, j4) + b(2, j4) + 1 'Batch 2
Next j4
GoSub 300: If fl1 = 0 Then GoTo 20 'Check identical numbers
GoSub 400 'Construct Square c() (Squared Elements)
GoSub 500: If fl1 = 0 Then GoTo 20 'Check Magic Sum c()
' n9 = n9 + 1: GoSub 740 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 750 'Print results (squares)
n9 = n9 + 1: Cells(1, 1).Value = n9 'Counting
n10 = n10 + 1
20 Next j2
If n10 > 0 Then
n11 = n11 + 1
Cells(n11, 2) = j1
Cells(n11, 3).Value = n10
n10 = 0
End If
Next j1
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine CnstrSqrs9a")
End
' Read Sudoku Comparable Squares (Line Format)
100 For i1 = 1 To 81
b(j20, i1) = Sheets("SudLns9b").Cells(j10, i1).Value
Next i1
Return
' Check identical numbers
300 fl1 = 1
For i1 = 1 To 81
a2 = a(i1)
For i2 = (1 + i1) To 81
If a2 = a(i2) Then fl1 = 0: Return
Next i2
Next i1
Return
' Construct Square c() (Squared Elements)
400 For i1 = 1 To 81
c(i1) = a(i1) ^ 2
Next i1
Return
' Check Magic Sum c()
500 fl1 = 1
s9(1) = c(1) + c(2) + c(3) + c(4) + c(5) + c(6) + c(7) + c(8) + c(9)
s9(2) = c(10) + c(11) + c(12) + c(13) + c(14) + c(15) + c(16) + c(17) + c(18)
s9(3) = c(19) + c(20) + c(21) + c(22) + c(23) + c(24) + c(25) + c(26) + c(27)
s9(4) = c(28) + c(29) + c(30) + c(31) + c(32) + c(33) + c(34) + c(35) + c(36)
s9(5) = c(37) + c(38) + c(39) + c(40) + c(41) + c(42) + c(43) + c(44) + c(45)
s9(6) = c(46) + c(47) + c(48) + c(49) + c(50) + c(51) + c(52) + c(53) + c(54)
s9(7) = c(55) + c(56) + c(57) + c(58) + c(59) + c(60) + c(61) + c(62) + c(63)
s9(8) = c(64) + c(65) + c(66) + c(67) + c(68) + c(69) + c(70) + c(71) + c(72)
s9(9) = c(73) + c(74) + c(75) + c(76) + c(77) + c(78) + c(79) + c(80) + c(81)
s9(10) = c(1) + c(10) + c(19) + c(28) + c(37) + c(46) + c(55) + c(64) + c(73)
s9(11) = c(2) + c(11) + c(20) + c(29) + c(38) + c(47) + c(56) + c(65) + c(74)
s9(12) = c(3) + c(12) + c(21) + c(30) + c(39) + c(48) + c(57) + c(66) + c(75)
s9(13) = c(4) + c(13) + c(22) + c(31) + c(40) + c(49) + c(58) + c(67) + c(76)
s9(14) = c(5) + c(14) + c(23) + c(32) + c(41) + c(50) + c(59) + c(68) + c(77)
s9(15) = c(6) + c(15) + c(24) + c(33) + c(42) + c(51) + c(60) + c(69) + c(78)
s9(16) = c(7) + c(16) + c(25) + c(34) + c(43) + c(52) + c(61) + c(70) + c(79)
s9(17) = c(8) + c(17) + c(26) + c(35) + c(44) + c(53) + c(62) + c(71) + c(80)
s9(18) = c(9) + c(18) + c(27) + c(36) + c(45) + c(54) + c(63) + c(72) + c(81)
s9(19) = c(1) + c(11) + c(21) + c(31) + c(41) + c(51) + c(61) + c(71) + c(81)
s9(20) = c(9) + c(17) + c(25) + c(33) + c(41) + c(49) + c(57) + c(65) + c(73)
' Regular Sub Squares (Optional)
s9(21) = c(1) + c(2) + c(3) + c(10) + c(11) + c(12) + c(19) + c(20) + c(21)
s9(22) = c(4) + c(5) + c(6) + c(13) + c(14) + c(15) + c(22) + c(23) + c(24)
s9(23) = c(7) + c(8) + c(9) + c(16) + c(17) + c(18) + c(25) + c(26) + c(27)
s9(24) = c(28) + c(29) + c(30) + c(37) + c(38) + c(39) + c(46) + c(47) + c(48)
s9(25) = c(31) + c(32) + c(33) + c(40) + c(41) + c(42) + c(49) + c(50) + c(51)
s9(26) = c(34) + c(35) + c(36) + c(43) + c(44) + c(45) + c(52) + c(53) + c(54)
s9(27) = c(55) + c(56) + c(57) + c(64) + c(65) + c(66) + c(73) + c(74) + c(75)
s9(28) = c(58) + c(59) + c(60) + c(67) + c(68) + c(69) + c(76) + c(77) + c(78)
s9(29) = c(61) + c(62) + c(63) + c(70) + c(71) + c(72) + c(79) + c(80) + c(81)
For i1 = 1 To 29 ''20
If s9(i1) <> 20049 Then fl1 = 0: Return
Next i1
Return
' Print results (selected numbers)
740 ''Cells(n9, 81).Select
For i1 = 1 To 81
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 82).Value = j1
Cells(n9, 83).Value = j2
Cells(1, 85).Value = n9
Return
' Print results (squares)
750 n2 = n2 + 1
If n2 = 9 Then
n2 = 1: k1 = k1 + 10: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 10
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = n9
Cells(k1, k2 + 2).Value = j1
Cells(k1, k2 + 3).Value = j2
i3 = 0
For i1 = 1 To 9
For i2 = 1 To 9
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
End Sub