' Constructs 10 x 10 Composed Magic Squares (Prime Numbers)
' Four Pan Magic Corner Squares
' Tested with Office 2007 under Windows 7
Sub Priem10g()
Dim a1(1944), a(16), b1(18720), b(18720), c(100), a10(100)
y = MsgBox("Locked", vbCritical, "Routine Priem10g")
End
n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
Sht1 = "Pairs8"
' Generate squares
Sheets("Klad1").Select
t1 = Timer
For j100 = 167 To 215
' Read Prime Numbers From sheet Sht1
s1 = 2 * Sheets(Sht1).Cells(j100, 1).Value
s6 = 3 * Sheets(Sht1).Cells(j100, 1).Value
s10 = 5 * Sheets(Sht1).Cells(j100, 1).Value
nVar = Sheets(Sht1).Cells(j100, 5).Value
nPm4 = Sheets(Sht1).Cells(j100, 6).Value
If nPm4 < 4 Or nVar < 100 Then GoTo 1000
m1 = 1: m2 = nVar
For i1 = m1 To m2
a1(i1) = Sheets(Sht1).Cells(j100, i1 + 6).Value
Next i1
Erase b1: n10 = 0
For i1 = m1 To m2
b1(a1(i1)) = a1(i1)
Next i1
For jj16 = m1 To m2 'a(16)
If b1(a1(jj16)) = 0 Then GoTo 1160
If b(a1(jj16)) = 0 Then b(a1(jj16)) = a1(jj16): c(16) = a1(jj16) Else GoTo 1160
a(16) = a1(jj16)
For jj15 = m1 To m2 'a(15)
If b1(a1(jj15)) = 0 Then GoTo 1150
If b(a1(jj15)) = 0 Then b(a1(jj15)) = a1(jj15): c(15) = a1(jj15) Else GoTo 1150
a(15) = a1(jj15)
For jj14 = m1 To m2 'a(14)
If b1(a1(jj14)) = 0 Then GoTo 1140
If b(a1(jj14)) = 0 Then b(a1(jj14)) = a1(jj14): c(14) = a1(jj14) Else GoTo 1140
a(14) = a1(jj14)
a(13) = s1 - a(14) - a(15) - a(16)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 1130
If b1(a(13)) = 0 Then GoTo 1130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 1130
For jj12 = m1 To m2 'a(12)
If b1(a1(jj12)) = 0 Then GoTo 1120
If b(a1(jj12)) = 0 Then b(a1(jj12)) = a1(jj12): c(12) = a1(jj12) Else GoTo 1120
a(12) = a1(jj12)
a(11) = s1 - a(12) - a(15) - a(16)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 1070
If b1(a(11)) = 0 Then GoTo 1070
a(10) = a(12) - a(14) + a(16)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 1070
If b1(a(10)) = 0 Then GoTo 1070
a(9) = -a(12) + a(14) + a(15)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 1070
If b1(a(9)) = 0 Then GoTo 1070
a(8) = 0.5 * s1 - a(14)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 1070
If b1(a(8)) = 0 Then GoTo 1070
a(7) = -0.5 * s1 + a(14) + a(15) + a(16)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 1070:
If b1(a(7)) = 0 Then GoTo 1070
a(6) = 0.5 * s1 - a(16)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 1070:
If b1(a(6)) = 0 Then GoTo 1070
a(5) = 0.5 * s1 - a(15)
If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 1070:
If b1(a(5)) = 0 Then GoTo 1070
a(4) = 0.5 * s1 - a(12) + a(14) - a(16)
If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 1070:
If b1(a(4)) = 0 Then GoTo 1070
a(3) = 0.5 * s1 + a(12) - a(14) - a(15)
If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 1070:
If b1(a(3)) = 0 Then GoTo 1070
a(2) = 0.5 * s1 - a(12)
If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 1070:
If b1(a(2)) = 0 Then GoTo 1070
a(1) = -0.5 * s1 + a(12) + a(15) + a(16)
If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 1070:
If b1(a(1)) = 0 Then GoTo 1070
' Exclude solutions with identical numbers a()
GoSub 1800: If fl1 = 0 Then GoTo 1070
n10 = n10 + 1: GoSub 2000 'Store Square a() in Square a10()
GoSub 1900 'Remove used primes from available primes
If n10 = 4 Then
GoSub 2200 'Restore available primes
Erase b, c: GoSub 3000 'Center Cross
If fl1 = 0 Then Erase b, c: GoTo 1000 'not possible
GoSub 1850 'Check Idendical Numbers (Back Check)
If fl1 = 1 Then
n9 = n9 + 1: GoSub 2650 'Print results (squares)
End If
Erase b, c: GoTo 1000
Else
Erase b, c: GoTo 1160
End If
1070 b(c(12)) = 0: c(12) = 0
1120 Next jj12
b(c(13)) = 0: c(13) = 0
1130 b(c(14)) = 0: c(14) = 0
1140 Next jj14
b(c(15)) = 0: c(15) = 0
1150 Next jj15
b(c(16)) = 0: c(16) = 0
1160 Next jj16
1000 Next j100
t2 = Timer
t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s10)
y = MsgBox(t10, 0, "Routine Priem10g")
End
' Exclude solutions with identical numbers a()
1800 fl1 = 1
For j1 = 1 To 16
a2 = a(j1)
For j2 = (1 + j1) To 16
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Exclude solutions with identical numbers a10()
1850 fl1 = 1
For j1 = 1 To 100
a2 = a10(j1)
For j2 = (1 + j1) To 100
If a2 = a10(j2) Then fl1 = 0: Return
Next j2
Next j1
Return
' Remove used primes from available primes
1900 For i1 = 1 To 16
b1(a(i1)) = 0
Next i1
Return
' Restore available primes
2200 i2 = 0: pMax = a1(m2)
Erase a1
For i1 = 1 To pMax
If b1(i1) <> 0 Then
i2 = i2 + 1: a1(i2) = b1(i1)
End If
Next i1
m1 = 1: m2 = i2
Return
' Store Square a() in Square a10()
2000
Select Case n10
Case 1
a10(1) = a(1): a10(2) = a(2): a10(3) = a(3): a10(4) = a(4):
a10(11) = a(5): a10(12) = a(6): a10(13) = a(7): a10(14) = a(8):
a10(21) = a(9): a10(22) = a(10): a10(23) = a(11): a10(24) = a(12):
a10(31) = a(13): a10(32) = a(14): a10(33) = a(15): a10(34) = a(16):
Case 2
a10(7) = a(1): a10(8) = a(2): a10(9) = a(3): a10(10) = a(4):
a10(17) = a(5): a10(18) = a(6): a10(19) = a(7): a10(20) = a(8):
a10(27) = a(9): a10(28) = a(10): a10(29) = a(11): a10(30) = a(12):
a10(37) = a(13): a10(38) = a(14): a10(39) = a(15): a10(40) = a(16):
Case 3
a10(61) = a(1): a10(62) = a(2): a10(63) = a(3): a10(64) = a(4):
a10(71) = a(5): a10(72) = a(6): a10(73) = a(7): a10(74) = a(8):
a10(81) = a(9): a10(82) = a(10): a10(83) = a(11): a10(84) = a(12):
a10(91) = a(13): a10(92) = a(14): a10(93) = a(15): a10(94) = a(16):
Case 4
a10(67) = a(1): a10(68) = a(2): a10(69) = a(3): a10(70) = a(4):
a10(77) = a(5): a10(78) = a(6): a10(79) = a(7): a10(80) = a(8):
a10(87) = a(9): a10(88) = a(10): a10(89) = a(11): a10(90) = a(12):
a10(97) = a(13): a10(98) = a(14): a10(99) = a(15): a10(100) = a(16):
End Select
Return
' Print results (squares)
2650 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(s10)
i3 = 0
For i1 = 1 To 10
For i2 = 1 To 10
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a10(i3)
Next i2
Next i1
Return
' Calculate Center Cross
3000 fl1 = 1
' Center Pairs
For j55 = m1 To m2
If b(a1(j55)) = 0 Then b(a1(j55)) = a1(j55): c(55) = a1(j55) Else GoTo 550
a10(55) = a1(j55)
a10(46) = s6 / 3 - a10(55)
If a10(46) < a1(m1) Or a10(46) > a1(m2) Then GoTo 460
If b1(a10(46)) = 0 Then GoTo 460
If b(a10(46)) = 0 Then b(a10(46)) = a10(46): c(46) = a10(46) Else GoTo 460
For j56 = m1 To m2
If b(a1(j56)) = 0 Then b(a1(j56)) = a1(j56): c(56) = a1(j56) Else GoTo 560
a10(56) = a1(j56)
a10(45) = s6 / 3 - a10(56)
If a10(45) < a1(m1) Or a10(45) > a1(m2) Then GoTo 450
If b1(a10(45)) = 0 Then GoTo 450
If b(a10(45)) = 0 Then b(a10(45)) = a10(45): c(45) = a10(45) Else GoTo 450
' Center Cross 6 x 6, Vertcal Pairs
For j25 = m1 To m2
If b(a1(j25)) = 0 Then b(a1(j25)) = a1(j25): c(25) = a1(j25) Else GoTo 250
a10(25) = a1(j25)
a10(26) = s6 / 3 - a10(25)
If a10(26) < a1(m1) Or a10(26) > a1(m2) Then GoTo 260
If b1(a10(26)) = 0 Then GoTo 260
If b(a10(26)) = 0 Then b(a10(26)) = a10(26): c(26) = a10(26) Else GoTo 260
For j35 = m1 To m2
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c(35) = a1(j35) Else GoTo 350
a10(35) = a1(j35)
a10(36) = s6 / 3 - a10(35)
If a10(36) < a1(m1) Or a10(36) > a1(m2) Then GoTo 360
If b1(a10(36)) = 0 Then GoTo 360
If b(a10(36)) = 0 Then b(a10(36)) = a10(36): c(36) = a10(36) Else GoTo 360
For j65 = m1 To m2
If b(a1(j65)) = 0 Then b(a1(j65)) = a1(j65): c(65) = a1(j65) Else GoTo 650
a10(65) = a1(j65)
a10(66) = s6 / 3 - a10(65)
If a10(66) < a1(m1) Or a10(66) > a1(m2) Then GoTo 660
If b1(a10(66)) = 0 Then GoTo 660
If b(a10(66)) = 0 Then b(a10(66)) = a10(66): c(66) = a10(66) Else GoTo 660
a10(75) = s6 - a10(25) - a10(35) - a10(45) - a10(55) - a10(65)
If a10(75) < a1(m1) Or a10(75) > a1(m2) Then GoTo 750
If b1(a10(75)) = 0 Then GoTo 750
If b(a10(75)) = 0 Then b(a10(75)) = a10(75): c(75) = a10(75) Else GoTo 750
a10(76) = s6 / 3 - a10(75)
If a10(76) < a1(m1) Or a10(76) > a1(m2) Then GoTo 760
If b1(a10(76)) = 0 Then GoTo 760
If b(a10(76)) = 0 Then b(a10(76)) = a10(76): c(76) = a10(76) Else GoTo 760
' Center Cross 6 x 6, Horizontal Pairs
For j53 = m1 To m2
If b(a1(j53)) = 0 Then b(a1(j53)) = a1(j53): c(53) = a1(j53) Else GoTo 530
a10(53) = a1(j53)
a10(43) = s6 / 3 - a10(53)
If a10(43) < a1(m1) Or a10(43) > a1(m2) Then GoTo 430
If b1(a10(43)) = 0 Then GoTo 430
If b(a10(43)) = 0 Then b(a10(43)) = a10(43): c(43) = a10(43) Else GoTo 430
For j54 = m1 To m2
If b(a1(j54)) = 0 Then b(a1(j54)) = a1(j54): c(54) = a1(j54) Else GoTo 540
a10(54) = a1(j54)
a10(44) = s6 / 3 - a10(54)
If a10(44) < a1(m1) Or a10(44) > a1(m2) Then GoTo 440
If b1(a10(44)) = 0 Then GoTo 440
If b(a10(44)) = 0 Then b(a10(44)) = a10(44): c(44) = a10(44) Else GoTo 440
For j57 = m1 To m2
If b(a1(j57)) = 0 Then b(a1(j57)) = a1(j57): c(57) = a1(j57) Else GoTo 570
a10(57) = a1(j57)
a10(47) = s6 / 3 - a10(57)
If a10(47) < a1(m1) Or a10(47) > a1(m2) Then GoTo 470
If b1(a10(47)) = 0 Then GoTo 470
If b(a10(47)) = 0 Then b(a10(47)) = a10(47): c(47) = a10(47) Else GoTo 470
a10(58) = s6 - a10(53) - a10(54) - a10(55) - a10(56) - a10(57)
If a10(58) < a1(m1) Or a10(58) > a1(m2) Then GoTo 580
If b1(a10(58)) = 0 Then GoTo 580
If b(a10(58)) = 0 Then b(a10(58)) = a10(58): c(58) = a10(58) Else GoTo 580
a10(48) = s6 / 3 - a10(58)
If a10(48) < a1(m1) Or a10(48) > a1(m2) Then GoTo 480
If b1(a10(48)) = 0 Then GoTo 480
If b(a10(48)) = 0 Then b(a10(48)) = a10(48): c(48) = a10(48) Else GoTo 480
' Complete 10 x 10 Cross, Horizontal
For j5 = m1 To m2
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 50
a10(5) = a1(j5)
a10(6) = s6 / 3 - a10(5)
If a10(6) < a1(m1) Or a10(6) > a1(m2) Then GoTo 60
If b1(a10(6)) = 0 Then GoTo 60
If b(a10(6)) = 0 Then b(a10(6)) = a10(6): c(6) = a10(6) Else GoTo 60
For j15 = m1 To m2
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a10(15) = a1(j15)
a10(16) = s6 / 3 - a10(15)
If a10(16) < a1(m1) Or a10(16) > a1(m2) Then GoTo 160
If b1(a10(16)) = 0 Then GoTo 160
If b(a10(16)) = 0 Then b(a10(16)) = a10(16): c(16) = a10(16) Else GoTo 160
For j85 = m1 To m2
If b(a1(j85)) = 0 Then b(a1(j85)) = a1(j85): c(85) = a1(j85) Else GoTo 850
a10(85) = a1(j85)
a10(86) = s6 / 3 - a10(85)
If a10(86) < a1(m1) Or a10(86) > a1(m2) Then GoTo 860
If b1(a10(86)) = 0 Then GoTo 860
If b(a10(86)) = 0 Then b(a10(86)) = a10(86): c(86) = a10(86) Else GoTo 860
a10(95) = 2 * s6 / 3 - a10(5) - a10(15) - a10(85)
If a10(95) < a1(m1) Or a10(95) > a1(m2) Then GoTo 950
If b1(a10(95)) = 0 Then GoTo 950
If b(a10(95)) = 0 Then b(a10(95)) = a10(95): c(95) = a10(95) Else GoTo 950
a10(96) = s6 / 3 - a10(95)
If a10(96) < a1(m1) Or a10(96) > a1(m2) Then GoTo 960
If b1(a10(96)) = 0 Then GoTo 960
If b(a10(96)) = 0 Then b(a10(96)) = a10(96): c(96) = a10(96) Else GoTo 960
' Complete 10 x 10 Cross, Vertical
For j51 = m1 To m2
If b(a1(j51)) = 0 Then b(a1(j51)) = a1(j51): c(51) = a1(j51) Else GoTo 510
a10(51) = a1(j51)
a10(41) = s6 / 3 - a10(51)
If a10(41) < a1(m1) Or a10(41) > a1(m2) Then GoTo 410
If b1(a10(41)) = 0 Then GoTo 410
If b(a10(41)) = 0 Then b(a10(41)) = a10(41): c(41) = a10(41) Else GoTo 410
For j52 = m1 To m2
If b(a1(j52)) = 0 Then b(a1(j52)) = a1(j52): c(52) = a1(j52) Else GoTo 520
a10(52) = a1(j52)
a10(42) = s6 / 3 - a10(52)
If a10(42) < a1(m1) Or a10(42) > a1(m2) Then GoTo 420
If b1(a10(42)) = 0 Then GoTo 420
If b(a10(42)) = 0 Then b(a10(42)) = a10(42): c(42) = a10(42) Else GoTo 420
For j59 = m1 To m2
If b(a1(j59)) = 0 Then b(a1(j59)) = a1(j59): c(59) = a1(j59) Else GoTo 590
a10(59) = a1(j59)
a10(49) = s6 / 3 - a10(59)
If a10(49) < a1(m1) Or a10(49) > a1(m2) Then GoTo 490
If b1(a10(49)) = 0 Then GoTo 490
If b(a10(49)) = 0 Then b(a10(49)) = a10(49): c(49) = a10(49) Else GoTo 490
a10(60) = 2 * s6 / 3 - a10(51) - a10(52) - a10(59)
If a10(60) < a1(m1) Or a10(60) > a1(m2) Then GoTo 600
If b1(a10(60)) = 0 Then GoTo 600
If b(a10(60)) = 0 Then b(a10(60)) = a10(60): c(60) = a10(60) Else GoTo 600
a10(50) = s6 / 3 - a10(60)
If a10(50) < a1(m1) Or a10(50) > a1(m2) Then GoTo 500
If b1(a10(50)) = 0 Then GoTo 500
If b(a10(50)) = 0 Then b(a10(50)) = a10(50): c(50) = a10(50) Else GoTo 500
Return
b(c(50)) = 0: c(50) = 0
500 b(c(60)) = 0: c(60) = 0
600 b(c(49)) = 0: c(49) = 0
490 b(c(59)) = 0: c(59) = 0
590 Next j59
b(c(42)) = 0: c(42) = 0
420 b(c(52)) = 0: c(52) = 0
520 Next j52
b(c(41)) = 0: c(41) = 0
410 b(c(51)) = 0: c(51) = 0
510 Next j51
b(c(96)) = 0: c(96) = 0
960 b(c(95)) = 0: c(95) = 0
950 b(c(86)) = 0: c(86) = 0
860 b(c(85)) = 0: c(85) = 0
850 Next j85
b(c(16)) = 0: c(16) = 0
160 b(c(15)) = 0: c(15) = 0
150 Next j15
b(c(6)) = 0: c(6) = 0
60 b(c(5)) = 0: c(5) = 0
50 Next j5
b(c(48)) = 0: c(48) = 0
480 b(c(58)) = 0: c(58) = 0
580 b(c(47)) = 0: c(47) = 0
470 b(c(57)) = 0: c(57) = 0
570 Next j57
b(c(44)) = 0: c(44) = 0
440 b(c(54)) = 0: c(54) = 0
540 Next j54
b(c(43)) = 0: c(43) = 0
430 b(c(53)) = 0: c(53) = 0
530 Next j53
b(c(76)) = 0: c(76) = 0
760 b(c(75)) = 0: c(75) = 0
750 b(c(66)) = 0: c(66) = 0
660 b(c(65)) = 0: c(65) = 0
650 Next j65
b(c(36)) = 0: c(36) = 0
360 b(c(35)) = 0: c(35) = 0
350 Next j35
b(c(26)) = 0: c(26) = 0
260 b(c(25)) = 0: c(25) = 0
250 Next j25
b(c(45)) = 0: c(45) = 0
450 b(c(56)) = 0: c(56) = 0
560 Next j56
b(c(46)) = 0: c(46) = 0
460 b(c(55)) = 0: c(55) = 0
550 Next j55
fl1 = 0: 'not found
Return
End Sub