Vorige Pagina About the Author

' Generates Order 8 Pan Magic Squares of Subtraction, s8 = 260, Res8 = 16
' Composed of Order 4 Pan Magic Squares s4 = 130

' Tested with Office 365 under Windows 10

Sub MgcSqr8b()

Dim a(64), b(64), c(64)
Dim b8(8), Res8(18)

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

n2 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 64: s1 = 260: r8 = 16

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

'   Define Starting Condition

    m58 = 1: m59 = 1: m60 = 1: m62 = 1: m63 = 1: m64 = 1:
                      m52 = 1:                   m56 = 1:

For j64 = m64 To m2                                          'a(64)
If b(j64) = 0 Then b(j64) = j64: c(64) = j64 Else GoTo 640
a(64) = j64
For j63 = m63 To m2                                          'a(63)
If b(j63) = 0 Then b(j63) = j63: c(63) = j63 Else GoTo 630
a(63) = j63
For j62 = m62 To m2                                          'a(62)
If b(j62) = 0 Then b(j62) = j62: c(62) = j62 Else GoTo 620
j61 = 0.5 * s1 - j62 - j63 - j64
If j61  <= 0 Or j61  > 64 Then GoTo 610                       'a(61)
If b(j61) = 0 Then b(j61) = j61: c(61) = j61 Else GoTo 610
a(62) = j62: a(61) = j61

For j60 = m60 To m2                                          'a(60)
If b(j60) = 0 Then b(j60) = j60: c(60) = j60 Else GoTo 600
a(60) = j60
For j59 = m59 To m2                                          'a(59)
If b(j59) = 0 Then b(j59) = j59: c(59) = j59 Else GoTo 590
a(59) = j59
For j58 = m58 To m2                                          'a(58)
If b(j58) = 0 Then b(j58) = j58: c(58) = j58 Else GoTo 580
j57 = 0.5 * s1 - j58 - j59 - j60
If j57  <= 0 Or j57  > 64 Then GoTo 570                       'a(57)
If b(j57) = 0 Then b(j57) = j57: c(57) = j57 Else GoTo 570
a(58) = j58: a(57) = j57

i10 = 8: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 565

For j56 = m56 To m2                                           'a(56)
If b(j56) = 0 Then b(j56) = j56: c(56) = j56 Else GoTo 560
j55 = 0.5 * s1 - j56 - a(63) - a(64)
If j55  <= 0 Or j55  > 64 Then GoTo 550                       'a(55)
If b(j55) = 0 Then b(j55) = j55: c(55) = j55 Else GoTo 550
j54 = j56 - a(62) + a(64)
If j54  <= 0 Or j54  > 64 Then GoTo 540                       'a(54)
If b(j54) = 0 Then b(j54) = j54: c(54) = j54 Else GoTo 540
j53 = -j56 + a(62) + a(63)
If j53  <= 0 Or j53  > 64 Then GoTo 530                       'a(53)
If b(j53) = 0 Then b(j53) = j53: c(53) = j53 Else GoTo 530
a(56) = j56: a(55) = j55: a(54) = j54: a(53) = j53

For j52 = m52 To m2                                          'a(52)
If b(j52) = 0 Then b(j52) = j52: c(52) = j52 Else GoTo 520
j51 = 0.5 * s1 - j52 - a(59) - a(60)
If j51  <= 0 Or j51  > 64 Then GoTo 510                       'a(51)
If b(j51) = 0 Then b(j51) = j51: c(51) = j51 Else GoTo 510
j50 = j52 - a(58) + a(60)
If j50  <= 0 Or j50  > 64 Then GoTo 500                       'a(50)
If b(j50) = 0 Then b(j50) = j50: c(50) = j50 Else GoTo 500
j49 = -j52 + a(58) + a(59)
If j49  <= 0 Or j49  > 64 Then GoTo 490                       'a(49)
If b(j49) = 0 Then b(j49) = j49: c(49) = j49 Else GoTo 490

a(52) = j52: a(51) = j51: a(50) = j50: a(49) = j49:

i10 = 7: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 485

j48 = 0.25 * s1 - a(62)
If j48  <= 0 Or j48  > 64 Then GoTo 480                       'a(48)
If b(j48) = 0 Then b(j48) = j48: c(48) = j48 Else GoTo 480
j47 = -0.25 * s1 + a(62) + a(63) + a(64)
If j47  <= 0 Or j47  > 64 Then GoTo 470                       'a(47)
If b(j47) = 0 Then b(j47) = j47: c(47) = j47 Else GoTo 470
j46 = 0.25 * s1 - a(64)
If j46  <= 0 Or j46  > 64 Then GoTo 460                       'a(46)
If b(j46) = 0 Then b(j46) = j46: c(46) = j46 Else GoTo 460
j45 = 0.25 * s1 - a(63)
If j45  <= 0 Or j45  > 64 Then GoTo 450                       'a(45)
If b(j45) = 0 Then b(j45) = j45: c(45) = j45 Else GoTo 450
j44 = 0.25 * s1 - a(58)
If j44  <= 0 Or j44  > 64 Then GoTo 440                       'a(44)
If b(j44) = 0 Then b(j44) = j44: c(44) = j44 Else GoTo 440
j43 = -0.25 * s1 + a(58) + a(59) + a(60)
If j43  <= 0 Or j43  > 64 Then GoTo 430                       'a(43)
If b(j43) = 0 Then b(j43) = j43: c(43) = j43 Else GoTo 430
j42 = 0.25 * s1 - a(60)
If j42  <= 0 Or j42  > 64 Then GoTo 420                       'a(42)
If b(j42) = 0 Then b(j42) = j42: c(42) = j42 Else GoTo 420
j41 = 0.25 * s1 - a(59)
If j41  <= 0 Or j41  > 64 Then GoTo 410                       'a(41)
If b(j41) = 0 Then b(j41) = j41: c(41) = j41 Else GoTo 410

a(48) = j48: a(47) = j47: a(46) = j46: a(45) = j45:
a(44) = j44: a(43) = j43: a(42) = j42: a(41) = j41:

i10 = 6: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 405

j40 = 0.25 * s1 - a(56) + a(62) - a(64)
If j40  <= 0 Or j40  > 64 Then GoTo 400                       'a(40)
If b(j40) = 0 Then b(j40) = j40: c(40) = j40 Else GoTo 400
j39 = 0.25 * s1 + a(56) - a(62) - a(63)
If j39  <= 0 Or j39  > 64 Then GoTo 390                       'a(39)
If b(j39) = 0 Then b(j39) = j39: c(39) = j39 Else GoTo 390
j38 = 0.25 * s1 - a(56)
If j38  <= 0 Or j38  > 64 Then GoTo 380                       'a(38)
If b(j38) = 0 Then b(j38) = j38: c(38) = j38 Else GoTo 380
j37 = -0.25 * s1 + a(56) + a(63) + a(64)
If j37  <= 0 Or j37  > 64 Then GoTo 370                       'a(37)
If b(j37) = 0 Then b(j37) = j37: c(37) = j37 Else GoTo 370
j36 = 0.25 * s1 - j52 + a(58) - a(60)
If j36  <= 0 Or j36  > 64 Then GoTo 360                       'a(36)
If b(j36) = 0 Then b(j36) = j36: c(36) = j36 Else GoTo 360
j35 = 0.25 * s1 + j52 - a(58) - a(59)
If j35  <= 0 Or j35  > 64 Then GoTo 350                       'a(35)
If b(j35) = 0 Then b(j35) = j35: c(35) = j35 Else GoTo 350
j34 = 0.25 * s1 - j52
If j34  <= 0 Or j34  > 64 Then GoTo 340                       'a(34)
If b(j34) = 0 Then b(j34) = j34: c(34) = j34 Else GoTo 340
j33 = -0.25 * s1 + j52 + a(59) + a(60)
If j33  <= 0 Or j33  > 64 Then GoTo 330                       'a(33)
If b(j33) = 0 Then b(j33) = j33: c(33) = j33 Else GoTo 330

a(40) = j40: a(39) = j39: a(38) = j38: a(37) = j37:
a(36) = j36: a(35) = j35: a(34) = j34: a(33) = j33

i10 = 5: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 325

For j32 = m1 To m2                                          'a(32)
If b(j32) = 0 Then b(j32) = j32: c(32) = j32 Else GoTo 320
a(32) = j32
For j31 = m1 To m2                                          'a(31)
If b(j31) = 0 Then b(j31) = j31: c(31) = j31 Else GoTo 310
a(31) = j31
For j30 = m1 To m2                                          'a(30)
If b(j30) = 0 Then b(j30) = j30: c(30) = j30 Else GoTo 300
j29 = 0.5 * s1 - j30 - a(31) - a(32)
If j29  <= 0 Or j29  > 64 Then GoTo 290                       'a(29)
If b(j29) = 0 Then b(j29) = j29: c(29) = j29 Else GoTo 290
j28 = a(32) + a(60) - a(64)
If j28  <= 0 Or j28  > 64 Then GoTo 280                       'a(28)
If b(j28) = 0 Then b(j28) = j28: c(28) = j28 Else GoTo 280
a(30) = j30: a(29) = j29: a(28) = j28

For j27 = m1 To m2                                           'a(27)
If b(j27) = 0 Then b(j27) = j27: c(27) = j27 Else GoTo 270
j26 = -j27 + a(30) + a(31) + a(58) + a(59) - a(62) - a(63)
If j26  <= 0 Or j26  > 64 Then GoTo 260
If b(j26) = 0 Then b(j26) = j26: c(26) = j26 Else GoTo 260
j25 = 0.5 * s1 - a(30) - a(31) - a(32) - a(58) - a(59) - a(60) + a(62) + a(63) + a(64)
If j25  <= 0 Or j25  > 64 Then GoTo 250
If b(j25) = 0 Then b(j25) = j25: c(25) = j25 Else GoTo 250
a(27) = j27: a(26) = j26: a(25) = j25

i10 = 4: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 245

For j24 = m1 To m2                                           'a(24)
If b(j24) = 0 Then b(j24) = j24: c(24) = j24 Else GoTo 240
a(24) = j24

a(23) = 0.5 * s1 - a(24) - a(31) - a(32)
If a(23)  <= 0 Or a(23)  > 64 Then GoTo 230
a(22) = a(24) - a(30) + a(32)
If a(22)  <= 0 Or a(22)  > 64 Then GoTo 230
a(21) = -a(24) + a(30) + a(31)
If a(21)  <= 0 Or a(21)  > 64 Then GoTo 230
a(20) = a(24) - a(27) + a(31) + a(52) - a(56) + a(59) - a(63)
If a(20)  <= 0 Or a(20)  > 64 Then GoTo 230
a(19) = 0.5 * s1 - a(24) - a(31) - a(32) - a(52) + a(56) - a(59) - a(60) + a(63) + a(64)
If a(19)  <= 0 Or a(19)  > 64 Then GoTo 230
a(18) = a(24) - a(30) + a(32) + a(52) - a(56) - a(58) + a(60) + a(62) - a(64)
If a(18)  <= 0 Or a(18)  > 64 Then GoTo 230
a(17) = -a(24) + a(27) + a(30) - a(52) + a(56) + a(58) - a(62)
If a(17)  <= 0 Or a(17)  > 64 Then GoTo 230
a(16) = 0.25 * s1 - a(30)
If a(16)  <= 0 Or a(16)  > 64 Then GoTo 230
a(15) = -0.25 * s1 + a(30) + a(31) + a(32)
If a(15)  <= 0 Or a(15)  > 64 Then GoTo 230
a(14) = 0.25 * s1 - a(32)
If a(14)  <= 0 Or a(14)  > 64 Then GoTo 230
a(13) = 0.25 * s1 - a(31)
If a(13)  <= 0 Or a(13)  > 64 Then GoTo 230
a(12) = 0.25 * s1 + a(27) - a(30) - a(31) - a(58) - a(59) + a(62) + a(63)
If a(12)  <= 0 Or a(12)  > 64 Then GoTo 230
a(11) = -0.25 * s1 + a(30) + a(31) + a(32) + a(58) + a(59) + a(60) - a(62) - a(63) - a(64)
If a(11)  <= 0 Or a(11)  > 64 Then GoTo 230
a(10) = 0.25 * s1 - a(32) - a(60) + a(64)
If a(10)  <= 0 Or a(10)  > 64 Then GoTo 230
a(9) = 0.25 * s1 - a(27)
If a(9)  <= 0 Or a(9)  > 64 Then GoTo 230
a(8) = 0.25 * s1 - a(24) + a(30) - a(32)
If a(8)  <= 0 Or a(8)  > 64 Then GoTo 230
a(7) = 0.25 * s1 + a(24) - a(30) - a(31)
If a(7)  <= 0 Or a(7)  > 64 Then GoTo 230
a(6) = 0.25 * s1 - a(24)
If a(6)  <= 0 Or a(6)  > 64 Then GoTo 230
a(5) = -0.25 * s1 + a(24) + a(31) + a(32)
If a(5)  <= 0 Or a(5)  > 64 Then GoTo 230
a(4) = 0.25 * s1 - a(24) + a(30) - a(32) - a(52) + a(56) + a(58) - a(60) - a(62) + a(64)
If a(4)  <= 0 Or a(4)  > 64 Then GoTo 230
a(3) = 0.25 * s1 + a(24) - a(27) - a(30) + a(52) - a(56) - a(58) + a(62)
If a(3)  <= 0 Or a(3)  > 64 Then GoTo 230
a(2) = 0.25 * s1 - a(24) + a(27) - a(31) - a(52) + a(56) - a(59) + a(63)
If a(2)  <= 0 Or a(2)  > 64 Then GoTo 230
a(1) = -0.25 * s1 + a(24) + a(31) + a(32) + a(52) - a(56) + a(59) + a(60) - a(63) - a(64)
If a(1)  <= 0 Or a(1)  > 64 Then GoTo 230

i10 = 3: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225
i10 = 2: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225
i10 = 1: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225

For i10 = 16 To 13 Step -1
    GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225
Next i10

i10 = 18: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225

For i10 = 12 To 9 Step -1
    GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225
Next i10

i10 = 17: GoSub 1000: If Res8(i10)  < > r8 Then GoTo 225

'                          Exclude solutions with identical numbers

                           GoSub 800: If fl1 = 0 Then GoTo 230
                           
                           n9 = n9 + 1
                           GoSub 650 'Print results (squares)
'                          GoSub 645 'Print results (selected numbers)

GoTo 5

225
230 b(c(24)) = 0: c(24) = 0
240 Next j24

245 b(c(25)) = 0: c(25) = 0
250 b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 Next j27

    b(c(28)) = 0: c(28) = 0
280 b(c(29)) = 0: c(29) = 0
290 b(c(30)) = 0: c(30) = 0
300 Next j30
    b(c(31)) = 0: c(31) = 0
310 Next j31
    b(c(32)) = 0: c(32) = 0
320 Next j32

325 b(c(33)) = 0: c(33) = 0
330 b(c(34)) = 0: c(34) = 0
340 b(c(35)) = 0: c(35) = 0
350 b(c(36)) = 0: c(36) = 0
360 b(c(37)) = 0: c(37) = 0
370 b(c(38)) = 0: c(38) = 0
380 b(c(39)) = 0: c(39) = 0
390 b(c(40)) = 0: c(40) = 0
400
405 b(c(41)) = 0: c(41) = 0
410 b(c(42)) = 0: c(42) = 0
420 b(c(43)) = 0: c(43) = 0
430 b(c(44)) = 0: c(44) = 0
440 b(c(45)) = 0: c(45) = 0
450 b(c(46)) = 0: c(46) = 0
460 b(c(47)) = 0: c(47) = 0
470 b(c(48)) = 0: c(48) = 0
480
485 b(c(49)) = 0: c(49) = 0
490 b(c(50)) = 0: c(50) = 0
500 b(c(51)) = 0: c(51) = 0
510 b(c(52)) = 0: c(52) = 0
520 Next j52
    m52 = 1

    b(c(53)) = 0: c(53) = 0
530 b(c(54)) = 0: c(54) = 0
540 b(c(55)) = 0: c(55) = 0
550 b(c(56)) = 0: c(56) = 0
560 Next j56
    m56 = 1

565 b(c(57)) = 0: c(57) = 0
570 b(c(58)) = 0: c(58) = 0
580 Next j58
    m58 = 1
    b(c(59)) = 0: c(59) = 0
590 Next j59
    m59 = 1
    b(c(60)) = 0: c(60) = 0
600 Next j60
    m60 = 1

    b(c(61)) = 0: c(61) = 0
610 b(c(62)) = 0: c(62) = 0
620 Next j62
    m62 = 1
    b(c(63)) = 0: c(63) = 0
630 Next j63
    m63 = 1
    b(c(64)) = 0: c(64) = 0
640 Next j64
    m64 = 1
5
   t2 = Timer
    
   t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
   y = MsgBox(t10, 0, "Routine MgcSqr8b")

End

'   Print results (selected numbers)

645 For i1 = 1 To 64
        Cells(n9, i1).Value = a(i1)
    Next i1
    
    Return

'   Print results (squares)

650 n2 = n2 + 1
    If n2 = 5 Then
        n2 = 1: k1 = k1 + 9: k2 = 1
    Else
        If n9  > 1 Then k2 = k2 + 9
    End If

    Cells(k1, k2 + 1).Value = n9

    i3 = 0
    For i1 = 1 To 8
        For i2 = 1 To 8
            i3 = i3 + 1
            Cells(k1 + i1, k2 + i2).Value = a(i3)
        Next i2
    Next i1

    Return

'   Exclude solutions with identical numbers

800 fl1 = 1
    For j1 = 1 To 64
       a2 = a(j1)
       For j2 = (1 + j1) To 64
           If a2 = a(j2) Then fl1 = 0: GoTo 850
       Next j2
    Next j1
850 Return

'   Determine Residu line i10

1000

    Select Case i10
    
'       Rows
    
        Case 1:
                b8(1) = a(1): b8(2) = a(2): b8(3) = a(3): b8(4) = a(4): b8(5) = a(5): b8(6) = a(6): b8(7) = a(7): b8(8) = a(8):
                GoSub 1200
        Case 2:
                b8(1) = a(9): b8(2) = a(10): b8(3) = a(11): b8(4) = a(12): b8(5) = a(13): b8(6) = a(14): b8(7) = a(15): b8(8) = a(16):
                GoSub 1200
        Case 3:
                b8(1) = a(17): b8(2) = a(18): b8(3) = a(19): b8(4) = a(20): b8(5) = a(21): b8(6) = a(22): b8(7) = a(23): b8(8) = a(24):
                GoSub 1200
        Case 4:
                b8(1) = a(25): b8(2) = a(26): b8(3) = a(27): b8(4) = a(28): b8(5) = a(29): b8(6) = a(30): b8(7) = a(31): b8(8) = a(32):
                GoSub 1200
        Case 5:
                b8(1) = a(33): b8(2) = a(34): b8(3) = a(35): b8(4) = a(36): b8(5) = a(37): b8(6) = a(38): b8(7) = a(39): b8(8) = a(40):
                GoSub 1200
        Case 6:
                b8(1) = a(41): b8(2) = a(42): b8(3) = a(43): b8(4) = a(44): b8(5) = a(45): b8(6) = a(46): b8(7) = a(47): b8(8) = a(48):
                GoSub 1200
        Case 7:
                b8(1) = a(49): b8(2) = a(50): b8(3) = a(51): b8(4) = a(52): b8(5) = a(53): b8(6) = a(54): b8(7) = a(55): b8(8) = a(56):
                GoSub 1200
        Case 8:
                b8(1) = a(57): b8(2) = a(58): b8(3) = a(59): b8(4) = a(60): b8(5) = a(61): b8(6) = a(62): b8(7) = a(63): b8(8) = a(64):
                GoSub 1200

'       Columns
                                                        
        Case 9:
                b8(1) = a(1): b8(2) = a(9): b8(3) = a(17): b8(4) = a(25): b8(5) = a(33): b8(6) = a(41): b8(7) = a(49): b8(8) = a(57):
                GoSub 1200
        Case 10:
                b8(1) = a(2): b8(2) = a(10): b8(3) = a(18): b8(4) = a(26): b8(5) = a(34): b8(6) = a(42): b8(7) = a(50): b8(8) = a(58):
                GoSub 1200
        Case 11:
                b8(1) = a(3): b8(2) = a(11): b8(3) = a(19): b8(4) = a(27): b8(5) = a(35): b8(6) = a(43): b8(7) = a(51): b8(8) = a(59):
                GoSub 1200
        Case 12:
                b8(1) = a(4): b8(2) = a(12): b8(3) = a(20): b8(4) = a(28): b8(5) = a(36): b8(6) = a(44): b8(7) = a(52): b8(8) = a(60):
                GoSub 1200
        Case 13:
                b8(1) = a(5): b8(2) = a(13): b8(3) = a(21): b8(4) = a(29): b8(5) = a(37): b8(6) = a(45): b8(7) = a(53): b8(8) = a(61):
                GoSub 1200
        Case 14:
                b8(1) = a(6): b8(2) = a(14): b8(3) = a(22): b8(4) = a(30): b8(5) = a(38): b8(6) = a(46): b8(7) = a(54): b8(8) = a(62):
                GoSub 1200
        Case 15:
                b8(1) = a(7): b8(2) = a(15): b8(3) = a(23): b8(4) = a(31): b8(5) = a(39): b8(6) = a(47): b8(7) = a(55): b8(8) = a(63):
                GoSub 1200
        Case 16:
                b8(1) = a(8): b8(2) = a(16): b8(3) = a(24): b8(4) = a(32): b8(5) = a(40): b8(6) = a(48): b8(7) = a(56): b8(8) = a(64):
                GoSub 1200
                                                        
'       Diagonals
                                                        
        Case 17:
                b8(1) = a(1): b8(2) = a(10): b8(3) = a(19): b8(4) = a(28): b8(5) = a(37): b8(6) = a(46): b8(7) = a(55): b8(8) = a(64):
                GoSub 1200
        Case 18:
                b8(1) = a(8): b8(2) = a(15): b8(3) = a(22): b8(4) = a(29): b8(5) = a(36): b8(6) = a(43): b8(7) = a(50): b8(8) = a(57):
                GoSub 1200
    
    End Select

    Return

1200

    For i2 = 1 To 7
    For i1 = i2 To 8
        If b8(i2)  < b8(i1) Then
            x = b8(i1)
            b8(i1) = b8(i2)
            b8(i2) = x
        End If
    Next i1
    Next i2

    Res8(i10) = b8(1) - b8(2) + b8(3) - b8(4) + b8(5) - b8(6) + b8(7) - b8(8)

    Return

End Sub

Vorige Pagina About the Author