Vorige Pagina About the Author

' Generates the borders for Eccentric Magic Squares of order 9 for integers as defined in a1()

' Tested with Office 2007 under Windows 7

Sub MgcSqr9f()

Dim a1(32), a(81), b1(141), b(141), c(81)

y = MsgBox("Locked", vbCritical, "Routine MgcSqr9f")
End

'   Defining Magic Square I (7 x 7)

    a(19) = 57:  a(20) = 56:  a(21) = 30:  a(22) = 112: a(23) = 136: a(24) = 99:  a(25) = 105:
    a(28) = 113: a(29) = 114: a(30) = 140: a(31) = 58:  a(32) = 34:  a(33) = 65:  a(34) = 71:
    a(37) = 151: a(38) = 18:  a(39) = 21:  a(40) = 89:  a(41) = 146: a(42) = 135: a(43) = 35:
    a(46) = 27:  a(47) = 82:  a(48) = 150: a(49) = 155: a(50) = 11:  a(51) = 63:  a(52) = 107:
    a(55) = 143: a(56) = 159: a(57) = 15:  a(58) = 20:  a(59) = 88:  a(60) = 115: a(61) = 55:
    a(64) = 19:  a(65) = 24:  a(66) = 81:  a(67) = 149: a(68) = 152: a(69) = 54:  a(70) = 116:
    a(73) = 85:  a(74) = 142: a(75) = 158: a(76) = 12:  a(77) = 28:  a(78) = 64:  a(79) = 106:

'   Border Variable Values

    a1(1) = 29:   a1(2) = 31:   a1(3) = 32:   a1(4) = 33:   a1(5) = 36:   a1(6) = 37:   a1(7) = 59:   a1(8) = 60:
    a1(9) = 61:   a1(10) = 62:  a1(11) = 66:  a1(12) = 67:  a1(13) = 68:  a1(14) = 69:  a1(15) = 70:  a1(16) = 72:
    a1(17) = 98:  a1(18) = 100: a1(19) = 101: a1(20) = 102: a1(21) = 103: a1(22) = 104: a1(23) = 108: a1(24) = 109:
    a1(25) = 110: a1(26) = 111: a1(27) = 133: a1(28) = 134: a1(29) = 137: a1(30) = 138: a1(31) = 139: a1(32) = 141:

    For i1 = 1 To 32
        b1(a1(i1)) = a1(i1)
    Next i1

    n2 = 0: n9 = 0: k1 = 1: k2 = 1
    m1 = 1: m2 = 32: s1 = 765
    
    Sheets("Klad1").Select
    
    t1 = Timer

    For j81 = 10 To 10                                          'a(81)
    If b(a1(j81)) = 0 Then b(a1(j81)) = a1(j81): c(81) = a1(j81) Else GoTo 810
    a(81) = a1(j81)
    
    a(80) = 170 - a(81): If a(80) < a1(m1) Or a(80) > a1(m2) Then GoTo 800
    If b1(a(80)) = 0 Then GoTo 800
    If b(a(80)) = 0 Then b(a(80)) = a(80): c(80) = a(80) Else GoTo 800
    
    For j72 = 12 To 12                                          'a(72)
    If b(a1(j72)) = 0 Then b(a1(j72)) = a1(j72): c(72) = a1(j72) Else GoTo 720
    a(72) = a1(j72)
    
    a(71) = 170 - a(72): If a(71) < a1(m1) Or a(71) > a1(m2) Then GoTo 710
    If b1(a(71)) = 0 Then GoTo 710
    If b(a(71)) = 0 Then b(a(71)) = a(71): c(71) = a(71) Else GoTo 710
    
    For j63 = 14 To 14                                          '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(62) = 170 - a(63): If a(62) < a1(m1) Or a(62) > a1(m2) Then GoTo 620
    If b1(a(62)) = 0 Then GoTo 620
    If b(a(62)) = 0 Then b(a(62)) = a(62): c(62) = a(62) Else GoTo 620
    
    For j54 = 29 To 29                                          'a(54)
    If b(a1(j54)) = 0 Then b(a1(j54)) = a1(j54): c(54) = a1(j54) Else GoTo 540
    a(54) = a1(j54)
    
    a(53) = 170 - a(54): If a(53) < a1(m1) Or a(53) > a1(m2) Then GoTo 530
    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 j45 = 32 To 32                                          'a(45)
    If b(a1(j45)) = 0 Then b(a1(j45)) = a1(j45): c(45) = a1(j45) Else GoTo 450
    a(45) = a1(j45)
    
    a(44) = 170 - a(45): If a(44) < a1(m1) Or a(44) > a1(m2) Then GoTo 440
    If b1(a(44)) = 0 Then GoTo 440
    If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440
    
    For j36 = m1 To m2                                          'a(36)
    If b(a1(j36)) = 0 Then b(a1(j36)) = a1(j36): c(36) = a1(j36) Else GoTo 360
    a(36) = a1(j36)

    a(35) = 170 - a(36): If a(35) < a1(m1) Or a(35) > a1(m2) Then GoTo 350
    If b1(a(35)) = 0 Then GoTo 350
    If b(a(35)) = 0 Then b(a(35)) = a(35): c(35) = a(35) Else GoTo 350
    
    For j27 = m1 To m2                                          'a(27)
    If b(a1(j27)) = 0 Then b(a1(j27)) = a1(j27): c(27) = a1(j27) Else GoTo 270
    a(27) = a1(j27)

    a(26) = 170 - a(27): If a(26) < a1(m1) Or a(26) > a1(m2) Then GoTo 260
    If b1(a(26)) = 0 Then GoTo 260
    If b(a(26)) = 0 Then b(a(26)) = a(26): c(26) = a(26) Else GoTo 260
    
    For j18 = m1 To m2                                          'a(18)
    If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
    a(18) = a1(j18)
 
    a(17) = 595 + a(18) - a(26) - a(35) - a(44) - a(53) - a(62) - a(71) - a(80)
    If a(17) < a1(m1) Or a(17) > a1(m2) Then GoTo 170
    If b1(a(17)) = 0 Then GoTo 170
    If b(a(17)) = 0 Then b(a(17)) = a(17): c(17) = a(17) Else GoTo 170
    
    For j16 = m1 To m2                                          'a(16)
    If b(a1(j16)) = 0 Then b(a1(j16)) = a1(j16): c(16) = a1(j16) Else GoTo 160
    a(16) = a1(j16)
    For j15 = m1 To m2                                          'a(15)
    If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
    a(15) = a1(j15)
    For j14 = m1 To m2                                          'a(14)
    If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
    a(14) = a1(j14)
    For j13 = m1 To m2                                          'a(13)
    If b(a1(j13)) = 0 Then b(a1(j13)) = a1(j13): c(13) = a1(j13) Else GoTo 130
    a(13) = a1(j13)
    For j12 = m1 To m2                                          'a(12)
    If b(a1(j12)) = 0 Then b(a1(j12)) = a1(j12): c(12) = a1(j12) Else GoTo 120
    a(12) = a1(j12)

    a(11) = (1360 - a(12) - a(13) - a(14) - a(15) - a(16) - a(17) - a(18) - a(21) - a(31) - a(41) - a(51) - a(61) - a(71) - a(81)) / 2
    j11 = CInt(a(11)): If j11 <> a(11) Then GoTo 110
    If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 110:
    If b1(a(11)) = 0 Then GoTo 110

    a(10) = 765 - a(11) - a(12) - a(13) - a(14) - a(15) - a(16) - a(17) - a(18)
    If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 110:
    If b1(a(10)) = 0 Then GoTo 110
    
    a(9) = 170 - a(17): If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 110:
    If b1(a(9)) = 0 Then GoTo 110
    
    a(8) = 170 - a(18): If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 110:
    If b1(a(8)) = 0 Then GoTo 110

    a(7) = 170 - a(16): If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 110:
    If b1(a(7)) = 0 Then GoTo 110

    a(6) = 170 - a(15): If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 110:
    If b1(a(6)) = 0 Then GoTo 110

    a(5) = 170 - a(14): If a(5) < a1(m1) Or a(5) > a1(m2) Then GoTo 110:
    If b1(a(5)) = 0 Then GoTo 110

    a(4) = 170 - a(13): If a(4) < a1(m1) Or a(4) > a1(m2) Then GoTo 110:
    If b1(a(4)) = 0 Then GoTo 110

    a(3) = 170 - a(12): If a(3) < a1(m1) Or a(3) > a1(m2) Then GoTo 110:
    If b1(a(3)) = 0 Then GoTo 110

    a(2) = 170 - a(11): If a(2) < a1(m1) Or a(2) > a1(m2) Then GoTo 110:
    If b1(a(2)) = 0 Then GoTo 110

    a(1) = 170 - a(10): If a(1) < a1(m1) Or a(1) > a1(m2) Then GoTo 110:
    If b1(a(1)) = 0 Then GoTo 110
    
'                          Exclude solutions with identical numbers

                           GoSub 2800: If fl1 = 0 Then GoTo 110

                           n9 = n9 + 1
                           GoSub 2650 'Print results (squares)
'                          GoSub 2645 'Print results (selected numbers)

110 b(c(12)) = 0: c(12) = 0
120 Next j12
    b(c(13)) = 0: c(13) = 0
130 Next j13
    b(c(14)) = 0: c(14) = 0
140 Next j14
    b(c(15)) = 0: c(15) = 0
150 Next j15
    b(c(16)) = 0: c(16) = 0
160 Next j16

    b(c(17)) = 0: c(17) = 0
170 b(c(18)) = 0: c(18) = 0
180 Next j18
    b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 Next j27
    b(c(35)) = 0: c(35) = 0
350 b(c(36)) = 0: c(36) = 0
360 Next j36
    b(c(44)) = 0: c(44) = 0
440 b(c(45)) = 0: c(45) = 0
450 Next j45
    b(c(53)) = 0: c(53) = 0
530 b(c(54)) = 0: c(54) = 0
540 Next j54
    b(c(62)) = 0: c(62) = 0
620 b(c(63)) = 0: c(63) = 0
630 Next j63
    b(c(71)) = 0: c(71) = 0
710 b(c(72)) = 0: c(72) = 0
720 Next j72
    b(c(80)) = 0: c(80) = 0
800 b(c(81)) = 0: c(81) = 0
810 Next j81

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

End

'    Print results (selected numbers)

2645 For i1 = 1 To 81
         Cells(n9, i1).Value = a(i1)
     Next i1
    
     Return

'    Print results (squares)

2650 n2 = n2 + 1
     If n2 = 5 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
    
     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
     
'   Exclude solutions with identical numbers

2800 fl1 = 1
     For j1 = 1 To 81
        a2 = a(j1)
        For j2 = (1 + j1) To 81
            If a2 = a(j2) Then fl1 = 0: Return
        Next j2
     Next j1
     Return

End Sub

Vorige Pagina About the Author