Vorige Pagina About the Author

' Generates Concentric Magic Squares of order 7 for Odd Numbers
' Suitable for Concentric Lozenge Squares of order 15 (Predefined Borders)

' Tested with Office 2007 under Windows 7

Sub Priem7d2()

Dim a1(97), a(25), a7(49), b(225), b1(225), c7(49)

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

    n2 = 0: n3 = 0: n9 = 0: k1 = 1: k2 = 1

    Sheets("Klad1").Select
    
    t1 = Timer

For j100 = 1 To 48

'   Define Variables (Odd Numbers)

    a1(1) = 5:    a1(2) = 7:    a1(3) = 9:    a1(4) = 11:   a1(5) = 19:   a1(6) = 21:
    a1(7) = 23:   a1(8) = 25:   a1(9) = 27:   a1(10) = 33:  a1(11) = 35:
    a1(12) = 37:  a1(13) = 39:  a1(14) = 41:  a1(15) = 43:  a1(16) = 47:  a1(17) = 49:
    a1(18) = 51:  a1(19) = 53:  a1(20) = 55:  a1(21) = 57:  a1(22) = 59:
    a1(23) = 61:  a1(24) = 63:  a1(25) = 65:  a1(26) = 67:  a1(27) = 69:  a1(28) = 71:
    a1(29) = 73:  a1(30) = 75:  a1(31) = 77:  a1(32) = 79:  a1(33) = 81:
    a1(34) = 83:  a1(35) = 85:  a1(36) = 87:  a1(37) = 89:  a1(38) = 91:  a1(39) = 93:
    a1(40) = 95:  a1(41) = 97:  a1(42) = 99:  a1(43) = 101: a1(44) = 103:
    a1(45) = 105: a1(46) = 107: a1(47) = 109: a1(48) = 111: a1(49) = 113: a1(50) = 115:
    a1(51) = 117: a1(52) = 119: a1(53) = 121: a1(54) = 123: a1(55) = 125:
    a1(56) = 127: a1(57) = 129: a1(58) = 131: a1(59) = 133: a1(60) = 135: a1(61) = 137:
    a1(62) = 139: a1(63) = 141: a1(64) = 143: a1(65) = 145: a1(66) = 147:
    a1(67) = 149: a1(68) = 151: a1(69) = 153: a1(70) = 155: a1(71) = 157: a1(72) = 159:
    a1(73) = 161: a1(74) = 163: a1(75) = 165: a1(76) = 167: a1(77) = 169:
    a1(78) = 171: a1(79) = 173: a1(80) = 175: a1(81) = 177: a1(82) = 179: a1(83) = 183:
    a1(84) = 185: a1(85) = 187: a1(86) = 189: a1(87) = 191: a1(88) = 193:
    a1(89) = 199: a1(90) = 201: a1(91) = 203: a1(92) = 205: a1(93) = 207: a1(94) = 215:
    a1(95) = 217: a1(96) = 219: a1(97) = 221:

    s7 = 791: s2 = 226: m1 = 1: m2 = 97
    aMin = a1(1): aMax = a1(97)

    Erase b1
    For i1 = 1 To 97
        b1(a1(i1)) = a1(i1)
    Next i1

'   Read Center Square

    For i1 = 1 To 25
        a(i1) = Sheets("Center5").Cells(j100, i1):  b1(a(i1)) = 0
    Next i1

'   Reduce Variables

    i2 = 0
    For i1 = aMin To aMax
        If b1(i1) = i1 Then
           i2 = i2 + 1: a1(i2) = i1
        End If
    Next i1
    m2 = i2

    Erase b1
    For i1 = m1 To m2
        b1(a1(i1)) = a1(i1)
    Next i1

'   store Center Square in a7()

    Erase a7
    a7(9) = a(1):   a7(10) = a(2):  a7(11) = a(3):  a7(12) = a(4):  a7(13) = a(5):
    a7(16) = a(6):  a7(17) = a(7):  a7(18) = a(8):  a7(19) = a(9):  a7(20) = a(10):
    a7(23) = a(11): a7(24) = a(12): a7(25) = a(13): a7(26) = a(14): a7(27) = a(15):
    a7(30) = a(16): a7(31) = a(17): a7(32) = a(18): a7(33) = a(19): a7(34) = a(20):
    a7(37) = a(21): a7(38) = a(22): a7(39) = a(23): a7(40) = a(24): a7(41) = a(25):

'   Complete Border(s)

For j49 = m1 To m2                                                           'a7(49) odd
If b(a1(j49)) = 0 Then b(a1(j49)) = a1(j49): c7(49) = a1(j49) Else GoTo 2490
a7(49) = a1(j49)
     
a7(1) = s2 - a7(49): If b(a7(1)) = 0 Then b(a7(1)) = a7(1): c7(1) = a7(1) Else GoTo 2010
       
For j48 = m1 To m2                                                           'a7(48) odd
If b(a1(j48)) = 0 Then b(a1(j48)) = a1(j48): c7(48) = a1(j48) Else GoTo 2480
a7(48) = a1(j48)

a7(6) = s2 - a7(48): If b(a7(6)) = 0 Then b(a7(6)) = a7(6): c7(6) = a7(6) Else GoTo 2060

For j47 = m1 To m2                                                           'a7(47) odd
If b(a1(j47)) = 0 Then b(a1(j47)) = a1(j47): c7(47) = a1(j47) Else GoTo 2470
a7(47) = a1(j47)

a7(5) = s2 - a7(47): If b(a7(5)) = 0 Then b(a7(5)) = a7(5): c7(5) = a7(5) Else GoTo 2050
     
For j46 = m1 To m2                                                           'a7(46) odd
If b(a1(j46)) = 0 Then b(a1(j46)) = a1(j46): c7(46) = a1(j46) Else GoTo 2460
a7(46) = a1(j46)

a7(4) = s2 - a7(46): If b(a7(4)) = 0 Then b(a7(4)) = a7(4): c7(4) = a7(4) Else GoTo 2040
     
For j45 = m1 To m2                                                           'a7(45) odd
If b(a1(j45)) = 0 Then b(a1(j45)) = a1(j45): c7(45) = a1(j45) Else GoTo 2450
a7(45) = a1(j45)

a7(3) = s2 - a7(45): If b(a7(3)) = 0 Then b(a7(3)) = a7(3): c7(3) = a7(3) Else GoTo 2030
     
For j44 = m1 To m2                                                           'a7(44) odd
If b(a1(j44)) = 0 Then b(a1(j44)) = a1(j44): c7(44) = a1(j44) Else GoTo 2440
a7(44) = a1(j44)
          
a7(43) = s7 - a7(44) - a7(45) - a7(46) - a7(47) - a7(48) - a7(49)
If a7(43) < a1(m1) Or a7(43) > a1(m2) Then GoTo 2430
If b1(a7(43)) = 0 Then GoTo 2430
If b(a7(43)) = 0 Then b(a7(43)) = a7(43): c7(43) = a7(43) Else GoTo 2430

a7(7) = s2 - a7(43): If b(a7(7)) = 0 Then b(a7(7)) = a7(7): c7(7) = a7(7) Else GoTo 2070
a7(2) = s2 - a7(44): If b(a7(2)) = 0 Then b(a7(2)) = a7(2): c7(2) = a7(2) Else GoTo 2020
     
For j42 = m1 To m2                                                           'a7(42) odd
If b(a1(j42)) = 0 Then b(a1(j42)) = a1(j42): c7(42) = a1(j42) Else GoTo 2420
a7(42) = a1(j42)

a7(36) = s2 - a7(42): If b(a7(36)) = 0 Then b(a7(36)) = a7(36): c7(36) = a7(36) Else GoTo 2360

For j35 = m1 To m2                                                           'a7(35) odd
If b(a1(j35)) = 0 Then b(a1(j35)) = a1(j35): c7(35) = a1(j35) Else GoTo 2350
a7(35) = a1(j35)

a7(29) = s2 - a7(35): If b(a7(29)) = 0 Then b(a7(29)) = a7(29): c7(29) = a7(29) Else GoTo 2290
     
For j28 = m1 To m2                                                           'a7(28) odd
If b(a1(j28)) = 0 Then b(a1(j28)) = a1(j28): c7(28) = a1(j28) Else GoTo 2280
a7(28) = a1(j28)

a7(22) = s2 - a7(28): If b(a7(22)) = 0 Then b(a7(22)) = a7(22): c7(22) = a7(22) Else GoTo 2220

For j21 = m1 To m2                                                           'a7(21) odd
If b(a1(j21)) = 0 Then b(a1(j21)) = a1(j21): c7(21) = a1(j21) Else GoTo 2210
a7(21) = a1(j21)
     
a7(15) = s2 - a7(21): If b(a7(15)) = 0 Then b(a7(15)) = a7(15): c7(15) = a7(15) Else GoTo 2150
     
a7(14) = -3 * s2 / 2 + a7(15) + a7(22) + a7(29) + a7(36) + a7(43) - a7(49)
If a7(14) < a1(m1) Or a7(14) > a1(m2) Then GoTo 2140
If b1(a7(14)) = 0 Then GoTo 2140
If b(a7(14)) = 0 Then b(a7(14)) = a7(14): c7(14) = a7(14) Else GoTo 2140

a7(8) = s2 - a7(14): If b(a7(8)) = 0 Then b(a7(8)) = a7(8): c7(8) = a7(8) Else GoTo 2080

                            GoSub 800: If fl1 = 0 Then GoTo 5      '(Back Check)

                            n9 = n9 + 1: GoSub 640   'Print results (selected numbers)
'                           n9 = n9 + 1: GoSub 660   'Print results (squares)
                            
                            Erase b, c7: GoTo 500    'Print only first square

5
     b(c7(8)) = 0: c7(8) = 0
2080 b(c7(14)) = 0: c7(14) = 0
2140 b(c7(15)) = 0: c7(15) = 0
2150 b(c7(21)) = 0: c7(21) = 0
2210 Next j21

     b(c7(22)) = 0: c7(22) = 0
2220 b(c7(28)) = 0: c7(28) = 0
2280 Next j28

     b(c7(29)) = 0: c7(29) = 0
2290 b(c7(35)) = 0: c7(35) = 0
2350 Next j35

     b(c7(36)) = 0: c7(36) = 0
2360 b(c7(42)) = 0: c7(42) = 0
2420 Next j42

     b(c7(2)) = 0: c7(2) = 0
2020 b(c7(7)) = 0: c7(7) = 0
2070 b(c7(43)) = 0: c7(43) = 0
2430 b(c7(44)) = 0: c7(44) = 0
2440 Next j44

     b(c7(3)) = 0: c7(3) = 0
2030 b(c7(45)) = 0: c7(45) = 0
2450 Next j45

     b(c7(4)) = 0: c7(4) = 0
2040 b(c7(46)) = 0: c7(46) = 0
2460 Next j46

     b(c7(5)) = 0: c7(5) = 0
2050 b(c7(47)) = 0: c7(47) = 0
2470 Next j47

     b(c7(6)) = 0: c7(6) = 0
2060 b(c7(48)) = 0: c7(48) = 0
2480 Next j48

     b(c7(1)) = 0: c7(1) = 0
2010 b(c7(49)) = 0: c7(49) = 0
2490 Next j49

    Erase b, c7
500 Next j100

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

End

'   Print results (selected numbers)

640 For i1 = 1 To 49
        Cells(n9, i1).Value = a7(i1)
    Next i1
    Cells(n9, 50).Value = n9
    Cells(n9, 50).Select
    Return

'   Print Square a7()

660 n2 = n2 + 1
    If n2 = 5 Then
        n2 = 1: k1 = k1 + 8: k2 = 1
    Else
        If n9 > 1 Then k2 = k2 + 8
    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 7
        For i2 = 1 To 7
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a7(i3)
        Next i2
    Next i1

    Return
    
'   Exclude solutions with identical numbers

800 fl1 = 1
    For j1 = 1 To 49
       a2 = a7(j1): If a2 = 0 Then GoTo 810
       For j2 = (1 + j1) To 49
           If a2 = a7(j2) Then fl1 = 0: Return
       Next j2
810 Next j1
    Return

End Sub

Vorige Pagina About the Author