Vorige Pagina Volgende Pagina About the Author

' Generates Inlaid Magic Squares of order 11, Integers 1 ... 121, Part II

' Tested with Office 2007 under Windows 7

Sub Priem11k2()

   Dim a1(121), a(121), b1(121), b(121), c(121), s(4)

y = MsgBox("Locked", vbCritical, "Routine Priem11k2")
End
 
    n1 = 0: n2 = 0: n3 = 0: k1 = 1: k2 = 1: n9 = 0: n10 = 0

    s11 = 671: Pr11 = 122

'   Generate Squares

    Sheets("Klad1").Select
    
    t1 = Timer

For j100 = 2 To 191       'Test Case = 136

    s(3) = Sheets("CntrLns11").Cells(j100, 124).Value
    s(4) = Sheets("CntrLns11").Cells(j100, 125).Value
    
    s(1) = 10 * s11 / 11 - s(4)
    s(2) = 8 * s11 / 11 - s(3)

    For i1 = 1 To 121    'Redefine Integers
        b1(i1) = i1
    Next i1

    For i1 = 1 To 121    'Remove Integers Center Squares
        x = Sheets("CntrLns11").Cells(j100, i1).Value
        a(i1) = x: b1(x) = 0
    Next i1

    n10 = 0
    For i1 = 1 To 121
        x = b1(i1)
        If x <> 0 Then n10 = n10 + 1: a1(n10) = x
    Next i1
    m1 = 1: m2 = n10: n10 = 0

'   Create Border

t11 = Timer                                                            'Time Out

For j121 = m1 To m2                                                    'a(121)
If b(a1(j121)) = 0 Then b(a1(j121)) = a1(j121): c(121) = a1(j121) Else GoTo 1210
a(121) = a1(j121)

a(1) = 2 * s11 / 11 - a(121): If b(a(1)) = 0 Then b(a(1)) = a(1): c(1) = a(1) Else GoTo 10

For j120 = m1 To m2                                                    'a(120)
If b(a1(j120)) = 0 Then b(a1(j120)) = a1(j120): c(120) = a1(j120) Else GoTo 1200
a(120) = a1(j120)

t12 = Timer: t13 = t12 - t11                                           'Time Out
If t13 > 30 Then Erase b1, b, c: GoTo 500                              'Time Out, Try Next

a(112) = -s11 / 11 + a(120) - s(3) + s(4)
If a(112) < a1(m1) Or a(112) > a1(m2) Then GoTo 1120
If b1(a(112)) = 0 Then GoTo 1120
If b(a(112)) = 0 Then b(a(112)) = a(112): c(112) = a(112) Else GoTo 1120

a(10) = 2 * s11 / 11 - a(112): If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100
a(2) = 2 * s11 / 11 - a(120): If b(a(2)) = 0 Then b(a(2)) = a(2): c(2) = a(2) Else GoTo 20

For j119 = m1 To m2                                                    'a(119)
If b(a1(j119)) = 0 Then b(a1(j119)) = a1(j119): c(119) = a1(j119) Else GoTo 1190
a(119) = a1(j119)

a(113) = -s11 / 11 + a(119) - s(3) + s(4)
If a(113) < a1(m1) Or a(113) > a1(m2) Then GoTo 1130
If b1(a(113)) = 0 Then GoTo 1130
If b(a(113)) = 0 Then b(a(113)) = a(113): c(113) = a(113) Else GoTo 1130

a(9) = 2 * s11 / 11 - a(113): If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90
a(3) = 2 * s11 / 11 - a(119): If b(a(3)) = 0 Then b(a(3)) = a(3): c(3) = a(3) Else GoTo 30

For j118 = m1 To m2                                                    'a(118)
If b(a1(j118)) = 0 Then b(a1(j118)) = a1(j118): c(118) = a1(j118) Else GoTo 1180
a(118) = a1(j118)

a(114) = -s11 / 11 + a(118) - s(3) + s(4)
If a(114) < a1(m1) Or a(114) > a1(m2) Then GoTo 1140
If b1(a(114)) = 0 Then GoTo 1140
If b(a(114)) = 0 Then b(a(114)) = a(114): c(114) = a(114) Else GoTo 1140

a(8) = 2 * s11 / 11 - a(114): If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80
a(4) = 2 * s11 / 11 - a(118): If b(a(4)) = 0 Then b(a(4)) = a(4): c(4) = a(4) Else GoTo 40

For j117 = m1 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(115) = -s11 / 11 + a(117) - s(3) + s(4)
If a(115) < a1(m1) Or a(115) > a1(m2) Then GoTo 1150
If b1(a(115)) = 0 Then GoTo 1150
If b(a(115)) = 0 Then b(a(115)) = a(115): c(115) = a(115) Else GoTo 1150

a(7) = 2 * s11 / 11 - a(115): If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70
a(5) = 2 * s11 / 11 - a(117): If b(a(5)) = 0 Then b(a(5)) = a(5): c(5) = a(5) Else GoTo 50

For j116 = m1 To m2                                                    'a(116)
If b(a1(j116)) = 0 Then b(a1(j116)) = a1(j116): c(116) = a1(j116) Else GoTo 1160
a(116) = a1(j116)

a(111) = 15 * s11 / 11 - a(116) - 2*a(117) - 2*a(118) - 2*a(119) - 2*a(120) - a(121) + 4*s(3) - 4*s(4)
If a(111) < a1(m1) Or a(111) > a1(m2) Then GoTo 1110
If b1(a(111)) = 0 Then GoTo 1110
If b(a(111)) = 0 Then b(a(111)) = a(111): c(111) = a(111) Else GoTo 1110

a(11) = 2 * s11 / 11 - a(111): If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110
a(6) = 2 * s11 / 11 - a(116): If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60

For j110 = m1 To m2                                                    'a(110)
If b(a1(j110)) = 0 Then b(a1(j110)) = a1(j110): c(110) = a1(j110) Else GoTo 1100
a(110) = a1(j110)

a(100) = s11 - a(110) - s(3) - s(4)
If a(100) < a1(m1) Or a(100) > a1(m2) Then GoTo 1000
If b1(a(100)) = 0 Then GoTo 1000
If b(a(100)) = 0 Then b(a(100)) = a(100): c(100) = a(100) Else GoTo 1000

a(22) = 2 * s11 / 11 - a(100): If b(a(22)) = 0 Then b(a(22)) = a(22): c(22) = a(22) Else GoTo 220
a(12) = 2 * s11 / 11 - a(110): If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120

For j99 = m1 To m2                                                    'a(99)
If b(a1(j99)) = 0 Then b(a1(j99)) = a1(j99): c(99) = a1(j99) Else GoTo 990
a(99) = a1(j99)

a(89) = s11 - a(99) - s(3) - s(4)
If a(89) < a1(m1) Or a(89) > a1(m2) Then GoTo 890
If b1(a(89)) = 0 Then GoTo 890
If b(a(89)) = 0 Then b(a(89)) = a(89): c(89) = a(89) Else GoTo 890

a(33) = 2 * s11 / 11 - a(89): If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330
a(23) = 2 * s11 / 11 - a(99): If b(a(23)) = 0 Then b(a(23)) = a(23): c(23) = a(23) Else GoTo 230

For j88 = m1 To m2                                                    'a(88)
If b(a1(j88)) = 0 Then b(a1(j88)) = a1(j88): c(88) = a1(j88) Else GoTo 880
a(88) = a1(j88)

a(78) = s11 - a(88) - s(3) - s(4)
If a(78) < a1(m1) Or a(78) > a1(m2) Then GoTo 780
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(44) = 2 * s11 / 11 - a(78): If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440
a(34) = 2 * s11 / 11 - a(88): If b(a(34)) = 0 Then b(a(34)) = a(34): c(34) = a(34) Else GoTo 340

For j77 = m1 To m2                                                    'a(77)
If b(a1(j77)) = 0 Then b(a1(j77)) = a1(j77): c(77) = a1(j77) Else GoTo 770
a(77) = a1(j77)

a(67) = s11 - a(77) - s(3) - s(4)
If a(67) < a1(m1) Or a(67) > a1(m2) Then GoTo 670
If b1(a(67)) = 0 Then GoTo 670
If b(a(67)) = 0 Then b(a(67)) = a(67): c(67) = a(67) Else GoTo 670

a(66) = 60 * s11 / 11 - 2*a(77) - 2*a(88) - 2*a(99) - 2*a(110) - a(116) - 2*a(117) - 2*a(118) +
                                                               - 2*a(119) - 2*a(120) - 2*a(121) - 8*s(4)

If a(66) < a1(m1) Or a(66) > a1(m2) Then GoTo 660
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

a(56) = 2 * s11 / 11 - a(66): If b(a(56)) = 0 Then b(a(56)) = a(56): c(56) = a(56) Else GoTo 560
a(55) = 2 * s11 / 11 - a(67): If b(a(55)) = 0 Then b(a(55)) = a(55): c(55) = a(55) Else GoTo 550
a(45) = 2 * s11 / 11 - a(77): If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450

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

     n9 = n9 + 1: GoSub 1650              'Print Square
     Erase b, c: GoTo 500                 'Print only one square

5
    b(c(45)) = 0: c(45) = 0
450 b(c(55)) = 0: c(55) = 0
550 b(c(56)) = 0: c(56) = 0
560 b(c(66)) = 0: c(66) = 0
660 b(c(67)) = 0: c(67) = 0
670 b(c(77)) = 0: c(77) = 0
770 Next j77

    b(c(34)) = 0: c(34) = 0
340 b(c(44)) = 0: c(44) = 0
440 b(c(78)) = 0: c(78) = 0
780 b(c(88)) = 0: c(88) = 0
880 Next j88

    b(c(23)) = 0: c(23) = 0
230 b(c(33)) = 0: c(33) = 0
330 b(c(89)) = 0: c(89) = 0
890 b(c(99)) = 0: c(99) = 0
990 Next j99

     b(c(12)) = 0: c(12) = 0
120  b(c(22)) = 0: c(22) = 0
220  b(c(100)) = 0: c(100) = 0
1000 b(c(110)) = 0: c(110) = 0
1100 Next j110

     b(c(6)) = 0: c(6) = 0
60   b(c(11)) = 0: c(11) = 0
110  b(c(111)) = 0: c(111) = 0
1110 b(c(116)) = 0: c(116) = 0
1160 Next j116

     b(c(5)) = 0: c(5) = 0
50   b(c(7)) = 0: c(7) = 0
70   b(c(115)) = 0: c(115) = 0
1150 b(c(117)) = 0: c(117) = 0
1170 Next j117

     b(c(4)) = 0: c(4) = 0
40   b(c(8)) = 0: c(8) = 0
80   b(c(114)) = 0: c(114) = 0
1140 b(c(118)) = 0: c(118) = 0
1180 Next j118

     b(c(3)) = 0: c(3) = 0
30   b(c(9)) = 0: c(9) = 0
90   b(c(113)) = 0: c(113) = 0
1130 b(c(119)) = 0: c(119) = 0
1190 Next j119

     b(c(2)) = 0: c(2) = 0
20   b(c(10)) = 0: c(10) = 0
100  b(c(112)) = 0: c(112) = 0
1120 b(c(120)) = 0: c(120) = 0
1200 Next j120

     b(c(1)) = 0: c(1) = 0
10   b(c(121)) = 0: c(121) = 0
1210 Next j121

500  Next j100

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

End

'    Exclude solutions with identical numbers

1300 fl1 = 1
     For j10 = 1 To 121
       c2 = a(j10): If c2 = 0 Then GoTo 1310
       For j20 = (1 + j10) To 121
           If c2 = a(j20) Then fl1 = 0: Return
       Next j20
1310 Next j10

     Return

'    Print results (lines)

1640 Cells(n9, 122).Select
     For i1 = 1 To 121
         Cells(n9, i1).Value = a(i1)
     Next i1
     Cells(n9, 122).Value = s1
     Return

'    Print results (squares)

1650 n2 = n2 + 1
     If n2 = 3 Then
         n2 = 1: k1 = k1 + 12: k2 = 1
     Else
         If n9 > 1 Then k2 = k2 + 12
     End If
     
     Cells(k1, k2 + 1).Select
     Cells(k1, k2 + 1).Font.Color = -4165632
     Cells(k1, k2 + 1).Value = n9
    
     i3 = 0
     For i1 = 1 To 11
         For i2 = 1 To 11
             i3 = i3 + 1
             Cells(k1 + i1, k2 + i2).Value = a(i3)
         Next i2
     Next i1
     Return

End Sub

Vorige Pagina Volgende Pagina About the Author