' Generates Unique Borders for Concentric Magic Squares of order 8
' Tested with Office 2007 under Windows 7
Sub MgcSqr8i1()
Dim a1(28), a(64), b1(64), b(64), c(64)
y = MsgBox("Locked", vbCritical, "Routine Priem88i2")
End
k1 = 1: k2 = 1: n1 = 0: n2 = 0: n9 = 0
' Define variables
a1(1) = 1: a1(2) = 2: a1(3) = 3: a1(4) = 4: a1(5) = 5: a1(6) = 6:
a1(7) = 7: a1(8) = 8: a1(9) = 9: a1(10) = 10: a1(11) = 11: a1(12) = 12:
a1(13) = 13: a1(14) = 14: a1(15) = 51: a1(16) = 52: a1(17) = 53: a1(18) = 54:
a1(19) = 55: a1(20) = 56: a1(21) = 57: a1(22) = 58: a1(23) = 59: a1(24) = 60:
a1(25) = 61: a1(26) = 62: a1(27) = 63: a1(28) = 64:
For i1 = 1 To 28
b1(a1(i1)) = a1(i1)
Next i1
m1 = 1: m2 = 28: s1 = 260: p2 = 65
t1 = Timer
Sheets("Klad1").Select
' Generate Border
For j64 = m2 To m1 Step -1 'a(64)
If b(a1(j64)) = 0 Then b(a1(j64)) = a1(j64): c(64) = a1(j64) Else GoTo 640
a(64) = a1(j64)
a(1) = s1 / 4 - a(64): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10
For j57 = j64 - 1 To m1 Step -1 'a(57)
If b(a1(j57)) = 0 Then b(a1(j57)) = a1(j57): c(57) = a1(j57) Else GoTo 570
a(57) = a1(j57)
a(8) = s1 / 4 - a(57):
If a(8) > a(57) Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
For j63 = m2 To m1 Step -1 'a(63)
If b(a1(j63)) = 0 Then b(a1(j63)) = a1(j63): c(63) = a1(j63) Else GoTo 630
a(63) = a1(j63)
a(7) = s1 / 4 - a(63): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
For j62 = j63 - 1 To m1 Step -1 'a(62)
If b(a1(j62)) = 0 Then b(a1(j62)) = a1(j62): c(62) = a1(j62) Else GoTo 620
a(62) = a1(j62)
a(6) = s1 / 4 - a(62): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60
For j61 = j62 - 1 To m1 Step -1 'a(61)
If b(a1(j61)) = 0 Then b(a1(j61)) = a1(j61): c(61) = a1(j61) Else GoTo 610
a(61) = a1(j61)
a(5) = s1 / 4 - a(61): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50
For j60 = j61 - 1 To m1 Step -1 'a(60)
If b(a1(j60)) = 0 Then b(a1(j60)) = a1(j60): c(60) = a1(j60) Else GoTo 600
a(60) = a1(j60)
a(4) = s1 / 4 - a(60): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40
For j59 = j60 - 1 To m1 Step -1 'a(59)
If b(a1(j59)) = 0 Then b(a1(j59)) = a1(j59): c(59) = a1(j59) Else GoTo 590
a(59) = a1(j59)
a(3) = s1 / 4 - a(59): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30
a(58) = s1 - a(57) - a(59) - a(60) - a(61) - a(62) - a(63) - a(64)
If a(58) < a1(m1) Or a(58) > a1(m2) Then GoTo 580
If b1(a(58)) = 0 Then GoTo 580
If a(58) > a(59) Then GoTo 580
If b(a(58)) = 0 Then b(a(58)) = a(58): c(58) = a(58) Else GoTo 580
a(2) = s1 / 4 - a(58): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20
For j56 = m2 To m1 Step -1 'a(56)
If b(a1(j56)) = 0 Then b(a1(j56)) = a1(j56): c(56) = a1(j56) Else GoTo 560
a(56) = a1(j56)
a(49) = s1 / 4 - a(56): If b(a(49)) = 0 Then b(a(49)) = a(49): c(49) = a(49) Else GoTo 490
For j48 = j56 - 1 To m1 Step -1 'a(48)
If b(a1(j48)) = 0 Then b(a1(j48)) = a1(j48): c(48) = a1(j48) Else GoTo 480
a(48) = a1(j48)
a(41) = s1 / 4 - a(48): If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410
For j40 = j48 - 1 To m1 Step -1 'a(40)
If b(a1(j40)) = 0 Then b(a1(j40)) = a1(j40): c(40) = a1(j40) Else GoTo 400
a(40) = a1(j40)
a(33) = s1 / 4 - a(40): If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330
For j32 = j40 - 1 To m1 Step -1 'a(32)
If b(a1(j32)) = 0 Then b(a1(j32)) = a1(j32): c(32) = a1(j32) Else GoTo 320
a(32) = a1(j32)
a(25) = s1 / 4 - a(32): If b(a(25)) = 0 Then b(a(25)) = a(25): c(25) = a(25) Else GoTo 250
For j24 = j32 - 1 To m1 Step -1 'a(24)
If b(a1(j24)) = 0 Then b(a1(j24)) = a1(j24): c(24) = a1(j24) Else GoTo 240
a(24) = a1(j24)
a(17) = s1 / 4 - a(24): If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
a(16) = s1 - a(8) - a(24) - a(32) - a(40) - a(48) - a(56) - a(64)
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 160
If b1(a(16)) = 0 Then GoTo 160
If a(16) > a(24) Then GoTo 160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160
a(9) = s1 / 4 - a(16): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
' Exclude solutions with identical numbers (Back Check)
GoSub 800: If fl1 = 0 Then GoTo 5
n9 = n9 + 1: GoSub 1640 'Print results (selected numbers)
' n9 = n9 + 1: GoSub 1650 'Print results (squares)
' n9 = n9 + 1: Cells(1, 1).Value = n9 'counting
' Erase b, c: GoTo 640 'Print only first square for j64
5
b(c(9)) = 0: c(0) = 0
90 b(c(16)) = 0: c(16) = 0
160 b(c(17)) = 0: c(17) = 0
170 b(c(24)) = 0: c(24) = 0
240 Next j24
b(c(25)) = 0: c(25) = 0
250 b(c(32)) = 0: c(32) = 0
320 Next j32
b(c(33)) = 0: c(33) = 0
330 b(c(40)) = 0: c(40) = 0
400 Next j40
b(c(41)) = 0: c(41) = 0
410 b(c(48)) = 0: c(48) = 0
480 Next j48
b(c(49)) = 0: c(49) = 0
490 b(c(56)) = 0: c(56) = 0
560 Next j56
b(c(2)) = 0: c(2) = 0
20 b(c(58)) = 0: c(58) = 0
580 b(c(3)) = 0: c(3) = 0
30 b(c(59)) = 0: c(59) = 0
590 Next j59
b(c(4)) = 0: c(4) = 0
40 b(c(60)) = 0: c(60) = 0
600 Next j60
b(c(5)) = 0: c(5) = 0
50 b(c(61)) = 0: c(61) = 0
610 Next j61
b(c(6)) = 0: c(6) = 0
60 b(c(62)) = 0: c(62) = 0
620 Next j62
b(c(7)) = 0: c(7) = 0
70 b(c(63)) = 0: c(63) = 0
630 Next j63
b(c(8)) = 0: c(8) = 0
80 b(c(57)) = 0: c(57) = 0
570 Next j57
b(c(1)) = 0: c(1) = 0
10 b(c(64)) = 0: c(64) = 0
640 Next j64
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine Priem8i2")
End
' Print results (selected numbers)
1640 For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 65).Value = n9
Return
' Print results (squares)
1650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
End If
Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = n9 ''"MC = " + CStr(s1)
i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1
Return
' Exclude solutions with identical numbers (Back Check)
800 fl1 = 1
For j1 = 1 To 64
a2 = a(j1): If a2 = 0 Then GoTo 810
For j2 = (1 + j1) To 64
If a2 = a(j2) Then fl1 = 0: Return
Next j2
810 Next j1
Return
End Sub