Vorige Pagina About the Author

' Generates order 4 Magic Squares of Subtraction for Integers 1 ... 16

' Tested with Office 365 under Windows 10

Sub SubtrSqr4()

    Dim a1(16), a(16), b1(16), b(16), c(16), s10(10)
    Dim b4(4), Res4(10)

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

    n2 = 0: n3 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
    m1 = 1: m2 = 16

'   Define Integers
    
    For i1 = 1 To 16
        a1(i1) = i1
        b1(a1(i1)) = a1(i1)
    Next i1

'   Generate squares
    
    Sheets("Klad1").Select
    
    t1 = Timer

r4 =  8          'Residuum
s1 = 34          'Magic Sum

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 j11 = m1 To m2                                          'a(11)
If b(a1(j11)) = 0 Then b(a1(j11)) = a1(j11): c(11) = a1(j11) Else GoTo 110
a(11) = a1(j11)
For j6 = m1 To m2                                          'a(6)
If b(a1(j6)) = 0 Then b(a1(j6)) = a1(j6): c(6) = a1(j6) Else GoTo 60
a(6) = a1(j6)
For j1 = j16 + 1 To m2                                          'a(1)
If b(a1(j1)) = 0 Then b(a1(j1)) = a1(j1): c(1) = a1(j1) Else GoTo 10
a(1) = a1(j1)

i10 = 9: GoSub 1000: If Res4(i10) <> r4 Then GoTo 5
    
For j13 = j16 + 1 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 j10 = m1 To m2                                          'a(10)
If b(a1(j10)) = 0 Then b(a1(j10)) = a1(j10): c(10) = a1(j10) Else GoTo 100
a(10) = a1(j10)
For j7 = m1 To m2                                          'a(7)
If b(a1(j7)) = 0 Then b(a1(j7)) = a1(j7): c(7) = a1(j7) Else GoTo 70
a(7) = a1(j7)
For j4 = j13 + 1 To m2                                          'a(4)
If b(a1(j4)) = 0 Then b(a1(j4)) = a1(j4): c(4) = a1(j4) Else GoTo 40
a(4) = a1(j4)
    
i10 = 10: GoSub 1000: If Res4(i10) <> r4 Then GoTo 35
    
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)

i10 = 4: GoSub 1000: If Res4(i10) <> r4 Then GoTo 135

For j3 = m1 To m2                                          'a(3)
If b(a1(j3)) = 0 Then b(a1(j3)) = a1(j3): c(3) = a1(j3) Else GoTo 30
a(3) = a1(j3)

For j2 = m1 To m2                                          'a(2)
If b(a1(j2)) = 0 Then b(a1(j2)) = a1(j2): c(2) = a1(j2) Else GoTo 20
a(2) = a1(j2)

i10 = 1: GoSub 1000: If Res4(i10) <> r4 Then GoTo 15
i10 = 6: GoSub 1000: If Res4(i10) <> r4 Then GoTo 15
i10 = 7: GoSub 1000: If Res4(i10) <> r4 Then GoTo 15

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)
For j9 = m1 To m2                                          'a(9)
If b(a1(j9)) = 0 Then b(a1(j9)) = a1(j9): c(9) = a1(j9) Else GoTo 90
a(9) = a1(j9)

i10 = 3: GoSub 1000: If Res4(i10) <> r4 Then GoTo 85

For j8 = m1 To m2                                          'a(8)
If b(a1(j8)) = 0 Then b(a1(j8)) = a1(j8): c(8) = a1(j8) Else GoTo 80
a(8) = a1(j8)
For j5 = m1 To m2                                          'a(5)
If b(a1(j5)) = 0 Then b(a1(j5)) = a1(j5): c(5) = a1(j5) Else GoTo 50
a(5) = a1(j5)
   
i10 = 2: GoSub 1000: If Res4(i10) <> r4 Then GoTo 45
i10 = 5: GoSub 1000: If Res4(i10) <> r4 Then GoTo 45
i10 = 8: GoSub 1000: If Res4(i10) <> r4 Then GoTo 45

''GoSub 900: If fl2 = 0 Then GoTo 45  'Check Associated  (Option)
''GoSub 850: If fl2 = 0 Then GoTo 45  'Check Pan Magic   (Option)


n9 = n9 + 1: ''GoSub 650: End  'Print Squares 
Cells(1, 1).Value = n9         'Counting

   
45  b(c(5)) = 0: c(5) = 0
50  Next j5
    b(c(8)) = 0: c(8) = 0
80  Next j8

85  b(c(9)) = 0: c(9) = 0
90  Next j9
    b(c(12)) = 0: c(12) = 0
120 Next j12

15  b(c(2)) = 0: c(2) = 0
20  Next j2
    b(c(3)) = 0: c(3) = 0
30  Next j3

135 b(c(14)) = 0: c(14) = 0
140 Next j14
    b(c(15)) = 0: c(15) = 0
150 Next j15
    
35  b(c(4)) = 0: c(4) = 0
40  Next j4
    b(c(7)) = 0: c(7) = 0
70  Next j7
    b(c(10)) = 0: c(10) = 0
100 Next j10
    b(c(13)) = 0: c(13) = 0
130 Next j13
    
5   b(c(1)) = 0: c(1) = 0
10  Next j1
    b(c(6)) = 0: c(6) = 0
60  Next j6
    b(c(11)) = 0: c(11) = 0
110 Next j11
    b(c(16)) = 0: c(16) = 0
160 Next j16

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

End

'   Print results (selected numbers)

640 For i1 = 1 To 16
        Cells(n9, i1).Value = a(i1)
    Next i1
    
    Return

'   Print results (squares)

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

    Return

'   Check Pan Diagonals (Option)
    
850 fl2 = 1

    s10(1) = a(1) + a(6) + a(11) + a(16)
    s10(2) = a(2) + a(7) + a(12) + a(13)
    s10(3) = a(3) + a(8) + a(9) + a(14)
    s10(4) = a(4) + a(5) + a(10) + a(15)
    
    s10(5) = a(4) + a(7) + a(10) + a(13)
    s10(6) = a(3) + a(6) + a(9) + a(16)
    s10(7) = a(2) + a(5) + a(12) + a(15)
    s10(8) = a(1) + a(8) + a(11) + a(14)

    For i1 = 1 To 8
        If s10(i1) <> s1 Then fl2 = 0: Return
    Next i1

    Return
    
'   Check Associated (Option)

900 fl2 = 1

    s10(1) = a(1) + a(16)   'Dia 1
    s10(2) = a(6) + a(11)
    s10(3) = a(4) + a(13)   'Dia 2
    s10(4) = a(7) + a(10)
    
    s10(5) = a(2) + a(15)
    s10(6) = a(3) + a(14)
    s10(7) = a(5) + a(12)
    s10(8) = a(8) + a(9)

    For i1 = 1 To 8
        If s10(i1) <> s1 / 2 Then fl2 = 0: Return
    Next i1

    Return
    
'   determine Residuum line i10

1000

    Select Case i10
    
'       Rows
    
        Case 1:
                b4(1) = a(1): b4(2) = a(2): b4(3) = a(3): b4(4) = a(4):
                GoSub 1200
        Case 2:
                b4(1) = a(5): b4(2) = a(6): b4(3) = a(7): b4(4) = a(8):
                GoSub 1200
        Case 3:
                b4(1) = a(9): b4(2) = a(10): b4(3) = a(11): b4(4) = a(12):
                GoSub 1200
        Case 4:
                b4(1) = a(13): b4(2) = a(14): b4(3) = a(15): b4(4) = a(16):
                GoSub 1200

'       Columns

        Case 5:
                b4(1) = a(1): b4(2) = a(5): b4(3) = a(9): b4(4) = a(13):
                GoSub 1200
        Case 6:
                b4(1) = a(2): b4(2) = a(6): b4(3) = a(10): b4(4) = a(14):
                GoSub 1200
        Case 7:
                b4(1) = a(3): b4(2) = a(7): b4(3) = a(11): b4(4) = a(15):
                GoSub 1200
        Case 8:
                b4(1) = a(4): b4(2) = a(8): b4(3) = a(12): b4(4) = a(16):
                GoSub 1200

'       Diagonals

        Case 9:
                b4(1) = a(1): b4(2) = a(6): b4(3) = a(11): b4(4) = a(16):
                GoSub 1200
        Case 10:
                b4(1) = a(4): b4(2) = a(7): b4(3) = a(10): b4(4) = a(13):
                GoSub 1200
    
    End Select

    Return

1200

    For i2 = 1 To 3
    For i1 = i2 To 4
        If b4(i2) < b4(i1) Then
            x = b4(i1)
            b4(i1) = b4(i2)
            b4(i2) = x
        End If
    Next i1
    Next i2

    Res4(i10) = b4(1) - b4(2) + b4(3) - b4(4)

    Return
    
End Sub

Vorige Pagina About the Author