Vorige Pagina About the Author

' Generates Inlaid Magic Squares of order 13, Prime Numbers (Border)

' Tested with Office 365 under Windows 10

Sub MgcSqr13k1()

   Dim a1(1690), a(169), b1(50169), b(50169), c(169), s(4)
   Dim a11(121)

y = MsgBox("Locked", vbCritical, "Routine MgcSqr13k1")
End
 
    n1 = 0: n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0
    ShtNm1 = "Pairs7"
    ShtNm2 = "Input56"

    Sheets("Klad1").Select
    
    t1 = Timer

For j100 = 43 To 44

    GoSub 2000                                                         'Define Variables

    s(3) = Sheets(ShtNm2).Cells(j100, 124).Value
    s(4) = Sheets(ShtNm2).Cells(j100, 125).Value
    
    s(1) = 12 * s1 / 13 - s(4)
    s(2) = 10 * s1 / 13 - s(3)

'   Create Border

t11 = Timer                                                            'Time Out

For j169 = m1 To m2                                                    'a(169)
If b(a1(j169)) = 0 Then b(a1(j169)) = a1(j169): c(169) = a1(j169) Else GoTo 1690
a(169) = a1(j169)

a(1) = 2 * s1 / 13 - a(169):
If b1(a(1)) = 0 Then GoTo 10
If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10

For j168 = m1 To m2                                                    'a(168)
If b(a1(j168)) = 0 Then b(a1(j168)) = a1(j168): c(168) = a1(j168) Else GoTo 1680
a(168) = a1(j168)

a(158) = -s1 / 13 + a(168) - s(3) + s(4)
If a(158) < a1(m1) Or a(158) > a1(m2) Then GoTo 1580
If b1(a(158)) = 0 Then GoTo 1580
If b(a(158)) = 0 Then b(a(158)) = a(158): c(158) = a(158) Else GoTo 1580

a(12) = 2 * s1 / 13 - a(158):
If b1(a(12)) = 0 Then GoTo 120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120

a(2) = 2 * s1 / 13 - a(168):
If b1(a(2)) = 0 Then GoTo 20
If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20

For j167 = j168 + 1 To m2                                                  'a(167)
If b(a1(j167)) = 0 Then b(a1(j167)) = a1(j167): c(167) = a1(j167) Else GoTo 1670
a(167) = a1(j167)

a(159) = -s1 / 13 + a(167) - s(3) + s(4)
If a(159) < a1(m1) Or a(159) > a1(m2) Then GoTo 1590
If b1(a(159)) = 0 Then GoTo 1590
If b(a(159)) = 0 Then b(a(159)) = a(159): c(159) = a(159) Else GoTo 1590

a(11) = 2 * s1 / 13 - a(159):
If b1(a(11)) = 0 Then GoTo 110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110

a(3) = 2 * s1 / 13 - a(167):
If b1(a(3)) = 0 Then GoTo 30
If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30

For j166 = j167 + 1 To m2                                                  'a(166)
If b(a1(j166)) = 0 Then b(a1(j166)) = a1(j166): c(166) = a1(j166) Else GoTo 1660
a(166) = a1(j166)

a(160) = -s1 / 13 + a(166) - s(3) + s(4)
If a(160) < a1(m1) Or a(160) > a1(m2) Then GoTo 1600
If b1(a(160)) = 0 Then GoTo 1600
If b(a(160)) = 0 Then b(a(160)) = a(160): c(160) = a(160) Else GoTo 1600

a(10) = 2 * s1 / 13 - a(160):
If b1(a(10)) = 0 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100

a(4) = 2 * s1 / 13 - a(166):
If b1(a(4)) = 0 Then GoTo 40
If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40

For j165 = j166 + 1 To m2                                                  'a(165)
If b(a1(j165)) = 0 Then b(a1(j165)) = a1(j165): c(165) = a1(j165) Else GoTo 1650
a(165) = a1(j165)

a(161) = -s1 / 13 + a(165) - s(3) + s(4)
If a(161) < a1(m1) Or a(161) > a1(m2) Then GoTo 1610
If b1(a(161)) = 0 Then GoTo 1610
If b(a(161)) = 0 Then b(a(161)) = a(161): c(161) = a(161) Else GoTo 1610

a(9) = 2 * s1 / 13 - a(161):
If b1(a(9)) = 0 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90

a(5) = 2 * s1 / 13 - a(165):
If b1(a(5)) = 0 Then GoTo 50
If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50

For j164 = j165 + 1 To m2                                                  'a(164)
If b(a1(j164)) = 0 Then b(a1(j164)) = a1(j164): c(164) = a1(j164) Else GoTo 1640
a(164) = a1(j164)

a(162) = -s1 / 13 + a(164) - s(3) + s(4)
If a(162) < a1(m1) Or a(162) > a1(m2) Then GoTo 1620
If b1(a(162)) = 0 Then GoTo 1620
If b(a(162)) = 0 Then b(a(162)) = a(162): c(162) = a(162) Else GoTo 1620

a(8) = 2 * s1 / 13 - a(162):
If b1(a(8)) = 0 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80

a(6) = 2 * s1 / 13 - a(164):
If b1(a(6)) = 0 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60

For j163 = j164 + 1 To m2                                                  'a(163)
If b(a1(j163)) = 0 Then b(a1(j163)) = a1(j163): c(163) = a1(j163) Else GoTo 1630
a(163) = a1(j163)

a(157) = 18 * s1 / 13 - a(163) - 2*a(164) - 2*a(165) - 2*a(166) - 2*a(167) - 2*a(168) - a(169) + 5*s(3) - 5*s(4)
If a(157) < a1(m1) Or a(157) > a1(m2) Then GoTo 1570
If b1(a(157)) = 0 Then GoTo 1570
If b(a(157)) = 0 Then b(a(157)) = a(157): c(157) = a(157) Else GoTo 1570

a(13) = 2 * s1 / 13 - a(157):
If b1(a(13)) = 0 Then GoTo 130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130

a(7) = 2 * s1 / 13 - a(163):
If b1(a(7)) = 0 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70

For j156 = m1 To m2                                               'a(156)
If b(a1(j156)) = 0 Then b(a1(j156)) = a1(j156): c(156) = a1(j156) Else GoTo 1560
a(156) = a1(j156)

a(144) = 13 * s1 / 13 - a(156) - s(3) - s(4)
If a(144) < a1(m1) Or a(144) > a1(m2) Then GoTo 1440
If b1(a(144)) = 0 Then GoTo 1440
If b(a(144)) = 0 Then b(a(144)) = a(144): c(144) = a(144) Else GoTo 1440

a(26) = 2 * s1 / 13 - a(144):
If b1(a(26)) = 0 Then GoTo 260
If b(a(26)) = 0 Then b(a(26)) = a(26): c(14) = a(26) Else GoTo 260

a(14) = 2 * s1 / 13 - a(156):
If b1(a(14)) = 0 Then GoTo 140
If b(a(14)) = 0 Then b(a(14)) = a(14): c(14) = a(14) Else GoTo 140

For j143 = j156 + 1 To m2                                                  'a(143)
If b(a1(j143)) = 0 Then b(a1(j143)) = a1(j143): c(143) = a1(j143) Else GoTo 1430
a(143) = a1(j143)

a(131) = 13 * s1 / 13 - a(143) - s(3) - s(4)
If a(131) < a1(m1) Or a(131) > a1(m2) Then GoTo 1310
If b1(a(131)) = 0 Then GoTo 1310
If b(a(131)) = 0 Then b(a(131)) = a(131): c(131) = a(131) Else GoTo 1310

a(39) = 2 * s1 / 13 - a(131):
If b1(a(39)) = 0 Then GoTo 390
If b(a(39)) = 0 Then b(a(39)) = a(39): c(39) = a(39) Else GoTo 390

a(27) = 2 * s1 / 13 - a(143):
If b1(a(27)) = 0 Then GoTo 270
If b(a(27)) = 0 Then b(a(27)) = a(27): c(27) = a(27) Else GoTo 270

''n9 = n9 + 1: GoSub 2650: End ''GoTo 500

For j130 = j143 + 1 To m2                                                  'a(130)
If b(a1(j130)) = 0 Then b(a1(j130)) = a1(j130): c(130) = a1(j130) Else GoTo 1300
a(130) = a1(j130)

a(118) = 13 * s1 / 13 - a(130) - s(3) - s(4)
If a(118) < a1(m1) Or a(118) > a1(m2) Then GoTo 1180
If b1(a(118)) = 0 Then GoTo 1180
If b(a(118)) = 0 Then b(a(118)) = a(118): c(118) = a(118) Else GoTo 1180

a(52) = 2 * s1 / 13 - a(118):
If b1(a(52)) = 0 Then GoTo 520
If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520

a(40) = 2 * s1 / 13 - a(130):
If b1(a(40)) = 0 Then GoTo 40
If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400

For j117 = j130 + 1 To m2                                                  'a(117)
If b(a1(j117)) = 0 Then b(a1(j117)) = a1(j117): c(117) = a1(j117) Else GoTo 1170
a(117) = a1(j117)

a(105) = 13 * s1 / 13 - a(117) - s(3) - s(4)
If a(105) < a1(m1) Or a(105) > a1(m2) Then GoTo 1050
If b1(a(105)) = 0 Then GoTo 1050
If b(a(105)) = 0 Then b(a(105)) = a(105): c(105) = a(105) Else GoTo 1050

a(65) = 2 * s1 / 13 - a(105):
If b1(a(65)) = 0 Then GoTo 650
If b(a(65)) = 0 Then b(a(65)) = a(65): c(65) = a(65) Else GoTo 650

a(53) = 2 * s1 / 13 - a(117):
If b1(a(53)) = 0 Then GoTo 530
If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530

For j104 = j117 + 1 To m2                                                  'a(104)
If b(a1(j104)) = 0 Then b(a1(j104)) = a1(j104): c(104) = a1(j104) Else GoTo 1040
a(104) = a1(j104)

a(92) = 13 * s1 / 13 - a(104) - s(3) - s(4)
If a(92) < a1(m1) Or a(92) > a1(m2) Then GoTo 920
If b1(a(92)) = 0 Then GoTo 920
If b(a(92)) = 0 Then b(a(92)) = a(92): c(92) = a(92) Else GoTo 920

a(91) = 66 * s1 / 13 - 2*a(104) - 2*a(117) - 2*a(130) - 2*a(143) - 2*a(156) + a(157) - a(169) - 5*s(3) - 5*s(4)
If a(91) < a1(m1) Or a(91) > a1(m2) Then GoTo 910
If b1(a(91)) = 0 Then GoTo 910
If b(a(91)) = 0 Then b(a(91)) = a(91): c(91) = a(91) Else GoTo 910

a(79) = 2 * s1 / 13 - a(91):
If b1(a(79)) = 0 Then GoTo 790
If b(a(79)) = 0 Then b(a(79)) = a(79): c(79) = a(79) Else GoTo 790

a(78) = 2 * s1 / 13 - a(92):
If b1(a(78)) = 0 Then GoTo 780
If b(a(78)) = 0 Then b(a(78)) = a(78): c(78) = a(78) Else GoTo 780

a(66) = 2 * s1 / 13 - a(104):
If b1(a(66)) = 0 Then GoTo 660
If b(a(66)) = 0 Then b(a(66)) = a(66): c(66) = a(66) Else GoTo 660


       GoSub 1500: If fl1 = 0 Then GoTo 5   'Back Check Identical Numbers

       n9 = n9 + 1: GoSub 2650              'Print Square
''     n9 = n9 + 1: Cells(1, 1).Value = n9  'Counting
       Erase b, c: GoTo 500                 'Print only one square for j100


5

     b(c(66)) = 0: c(66) = 0
660  b(c(78)) = 0: c(78) = 0
780  b(c(79)) = 0: c(79) = 0
790  b(c(91)) = 0: c(91) = 0
910  b(c(92)) = 0: c(92) = 0
920  b(c(104)) = 0: c(104) = 0
1040 Next j104

     b(c(53)) = 0: c(53) = 0
530  b(c(65)) = 0: c(65) = 0
650  b(c(105)) = 0: c(105) = 0
1050 b(c(117)) = 0: c(117) = 0
1170 Next j117

     b(c(40)) = 0: c(40) = 0
400  b(c(52)) = 0: c(52) = 0
520  b(c(118)) = 0: c(118) = 0
1180 b(c(130)) = 0: c(130) = 0
1300 Next j130

     b(c(27)) = 0: c(27) = 0
270  b(c(39)) = 0: c(39) = 0
390  b(c(131)) = 0: c(131) = 0
1310 b(c(143)) = 0: c(143) = 0
1430 Next j143

     b(c(14)) = 0: c(14) = 0
140  b(c(26)) = 0: c(26) = 0
260  b(c(144)) = 0: c(144) = 0
1440 b(c(156)) = 0: c(156) = 0
1560 Next j156
     
     b(c(7)) = 0: c(7) = 0
70   b(c(13)) = 0: c(13) = 0
130  b(c(157)) = 0: c(157) = 0
1570 b(c(163)) = 0: c(163) = 0
1630 Next j163

     b(c(6)) = 0: c(6) = 0
60   b(c(8)) = 0: c(8) = 0
80   b(c(162)) = 0: c(162) = 0
1620 b(c(164)) = 0: c(164) = 0
1640 Next j164

     b(c(5)) = 0: c(5) = 0
50   b(c(9)) = 0: c(9) = 0
90   b(c(161)) = 0: c(161) = 0
1610 b(c(165)) = 0: c(165) = 0
1650 Next j165

     b(c(4)) = 0: c(4) = 0
40   b(c(10)) = 0: c(10) = 0
100  b(c(160)) = 0: c(160) = 0
1600 b(c(166)) = 0: c(166) = 0
1660 Next j166

     b(c(3)) = 0: c(3) = 0
30   b(c(11)) = 0: c(11) = 0
110  b(c(159)) = 0: c(159) = 0
1590 b(c(167)) = 0: c(167) = 0
1670 Next j167

     b(c(2)) = 0: c(2) = 0
20   b(c(12)) = 0: c(12) = 0
120  b(c(158)) = 0: c(158) = 0
1580 b(c(168)) = 0: c(168) = 0
1680 Next j168

     b(c(1)) = 0: c(1) = 0
10   b(c(169)) = 0: c(169) = 0
1690 Next j169

500 Erase b1, b, c
    Next j100

    t2 = Timer
    
    t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s11)
    y = MsgBox(t10, 0, "Routine MgcSqr13k1")

End

'   Remove used pairs from b1()

950 For i1 = 1 To n32
        b1(a11(i1)) = 0
        If (Pr13 - a11(i1)) > 0 Then
            b1(Pr13 - a11(i1)) = 0   'Complement
        End If
    Next i1
    Return

'    Define Variables
     
2000 fl1 = 1

'    Start Reading Data ShtNm2
    
     Rcrd1a = Sheets(ShtNm2).Cells(j100, 126).Value

'    Read Prime Numbers From Sheet ShtNm1

     Pr13 = Sheets(ShtNm1).Cells(Rcrd1a, 1).Value      'PairSum
     s1 = 13 * Pr13 / 2
     nVar = Sheets(ShtNm1).Cells(Rcrd1a, 9).Value

     Erase b1
     For j1 = 1 To nVar
         x = Sheets(ShtNm1).Cells(Rcrd1a, 9 + j1).Value
         b1(x) = x
     Next j1
     pMax = Sheets(ShtNm1).Cells(Rcrd1a, 9 + nVar).Value

'    Read Inlaid Squares 5 x 5 / 6 x 6
        
     For i1 = 1 To 121
         a11(i1) = Sheets(ShtNm2).Cells(j100, i1).Value
     Next i1
     n32 = 121: GoSub 950   'Remove used primes from available primes
        
     Erase a

     a(15) = a11(1):  a(16) = a11(2):  a(17) = a11(3):  a(18) = a11(4):  a(19) = a11(5):  a(20) = a11(6):
     a(21) = a11(7):  a(22) = a11(8):  a(23) = a11(9):  a(24) = a11(10): a(25) = a11(11):
     a(28) = a11(12): a(29) = a11(13): a(30) = a11(14): a(31) = a11(15): a(32) = a11(16): a(33) = a11(17):
     a(34) = a11(18): a(35) = a11(19): a(36) = a11(20): a(37) = a11(21): a(38) = a11(22):
     a(41) = a11(23): a(42) = a11(24): a(43) = a11(25): a(44) = a11(26): a(45) = a11(27): a(46) = a11(28):
     a(47) = a11(29): a(48) = a11(30): a(49) = a11(31): a(50) = a11(32): a(51) = a11(33):
     a(54) = a11(34): a(55) = a11(35): a(56) = a11(36): a(57) = a11(37): a(58) = a11(38): a(59) = a11(39):
     a(60) = a11(40): a(61) = a11(41): a(62) = a11(42): a(63) = a11(43): a(64) = a11(44):
     a(67) = a11(45): a(68) = a11(46): a(69) = a11(47): a(70) = a11(48): a(71) = a11(49): a(72) = a11(50):
     a(73) = a11(51): a(74) = a11(52): a(75) = a11(53): a(76) = a11(54): a(77) = a11(55):
     a(80) = a11(56): a(81) = a11(57): a(82) = a11(58): a(83) = a11(59): a(84) = a11(60): a(85) = a11(61):
     a(86) = a11(62): a(87) = a11(63): a(88) = a11(64): a(89) = a11(65): a(90) = a11(66):
     a(93) = a11(67): a(94) = a11(68): a(95) = a11(69): a(96) = a11(70): a(97) = a11(71): a(98) = a11(72):
     a(99) = a11(73):   a(100) = a11(74): a(101) = a11(75): a(102) = a11(76): a(103) = a11(77):
     a(106) = a11(78):  a(107) = a11(79): a(108) = a11(80): a(109) = a11(81): a(110) = a11(82): a(111) = a11(83):
     a(112) = a11(84):  a(113) = a11(85): a(114) = a11(86): a(115) = a11(87): a(116) = a11(88):
     a(119) = a11(89):  a(120) = a11(90): a(121) = a11(91): a(122) = a11(92): a(123) = a11(93): a(124) = a11(94):
     a(125) = a11(95):  a(126) = a11(96): a(127) = a11(97): a(128) = a11(98): a(129) = a11(99):
     a(132) = a11(100): a(133) = a11(101): a(134) = a11(102): a(135) = a11(103): a(136) = a11(104): a(137) = a11(105):
     a(138) = a11(106): a(139) = a11(107): a(140) = a11(108): a(141) = a11(109): a(142) = a11(110):
     a(145) = a11(111): a(146) = a11(112): a(147) = a11(113): a(148) = a11(114): a(149) = a11(115): a(150) = a11(116):
     a(151) = a11(117): a(152) = a11(118): a(153) = a11(119): a(154) = a11(120): a(155) = a11(121):
        
     Erase a11

'    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

     Return

'    Exclude solutions with identical numbers

1500 fl1 = 1
     For i10 = 1 To 169
       c2 = a(i10): If c2 = 0 Then GoTo 1510
       For i20 = (1 + i10) To 169
           If c2 = a(i20) Then fl1 = 0: Return
       Next i20
1510 Next i10

     Return

'    Print results (lines)

2640
     For i1 = 1 To 169
         Cells(n9, i1).Value = a(i1)
     Next i1
     Cells(n9, 170).Value = s1
     
     Return

'    Print results (squares)

2650 n2 = n2 + 1
     If n2 = 2 Then
         n2 = 1: k1 = k1 + 14: k2 = 1
     Else
         If n9 > 1 Then k1 = k1 + 14
     End If
     
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = j100
     
     i3 = 0
     For i1 = 1 To 13
         For i2 = 1 To 13
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a(i3)
         Next i2
     Next i1
     Return

End Sub

Vorige Pagina About the Author