Vorige Pagina About the Author

' Generates Magic Squares of order 8, Magic Sum 260, (Magic Sub squares)

' Tested with Office 2007 under Windows 7

Sub MgcSqr8d()

Dim a(64), b(64), c(64)

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

n2 = 0: n9 = 0
m1 = 1: m2 = 64: s1 = 260

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

For j64 = 24 To 24                                          'a(64)
If b(j64) = 0 Then b(j64) = j64: c(64) = j64 Else GoTo 640
a(64) = j64
For j63 = 42 To 42                                          'a(63)
If b(j63) = 0 Then b(j63) = j63: c(63) = j63 Else GoTo 630
a(63) = j63
For j62 = 39 To 39                                          '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 = 8 To 8                                          'a(60)
If b(j60) = 0 Then b(j60) = j60: c(60) = j60 Else GoTo 600
a(60) = j60
For j59 = 58 To 58                                          'a(59)
If b(j59) = 0 Then b(j59) = j59: c(59) = j59 Else GoTo 590
a(59) = j59
For j58 = 55 To 55                                          '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

For j56 = 47 To 47                                          'a(56)
If b(j56) = 0 Then b(j56) = j56: c(56) = j56 Else GoTo 560
a(56) = j56
For j55 = 17 To 17                                          'a(55)
If b(j55) = 0 Then b(j55) = j55: c(55) = j55 Else GoTo 550
a(55) = j55
For j54 = 32 To 32                                          'a(54)
If b(j54) = 0 Then b(j54) = j54: c(54) = j54 Else GoTo 540
j53 = 0.5 * s1 - j54 - j55 - j56
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(54) = j54: a(53) = j53

For j52 = 63 To 63                                          'a(52)
If b(j52) = 0 Then b(j52) = j52: c(52) = j52 Else GoTo 520
a(52) = j52
For j51 = 1 To 1                                          'a(51)
If b(j51) = 0 Then b(j51) = j51: c(51) = j51 Else GoTo 510
a(51) = j51
For j50 = 16 To 16                                          'a(50)
If b(j50) = 0 Then b(j50) = j50: c(50) = j50 Else GoTo 500
j49 = 0.5 * s1 - j50 - j51 - j52
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(50) = j50: a(49) = j49

For j48 = 26 To 26                                          'a(48)
If b(j48) = 0 Then b(j48) = j48: c(48) = j48 Else GoTo 480
j47 = j48 - a(54) + a(56) - a(61) + 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.5 * s1 - j48 - a(55) - a(56) + a(61) - 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 = -j48 + a(54) + a(55)
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
a(48) = j48: a(47) = j47: a(46) = j46: a(45) = j45

For j44 = 10 To 10                                          'a(44)
If b(j44) = 0 Then b(j44) = j44: c(44) = j44 Else GoTo 440
j43 = j44 - a(50) + a(52) - a(57) + 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.5 * s1 - j44 - a(51) - a(52) + a(57) - 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 = -j44 + a(50) + a(51)
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
j40 = 0.5 * s1 - a(47) - a(54) - a(61)
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.5 * s1 - a(48) + a(53) + 2 * a(54) + 2 * a(61) + a(62)
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 = a(48) - a(53) - 2 * a(54) + a(63) + 2 * a(64)
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 = a(48) + a(56) - a(61)
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.5 * s1 - j43 - a(50) - a(57)
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.5 * s1 - j44 + a(49) + 2 * a(50) + 2 * a(57) + a(58)
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 = j44 - a(49) - 2 * a(50) + a(59) + 2 * a(60)
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 = j44 + a(52) - a(57)
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(44) = j44: a(43) = j43: a(42) = j42: a(41) = j41: a(40) = j40
a(39) = j39: a(38) = j38: a(37) = j37: a(36) = j36: a(35) = j35
a(34) = j34: a(33) = j33

For j32 = 22 To 22                                          'a(32)
If b(j32) = 0 Then b(j32) = j32: c(32) = j32 Else GoTo 320
a(32) = j32
For j31 = 44 To 44                                          'a(31)
If b(j31) = 0 Then b(j31) = j31: c(31) = j31 Else GoTo 310
a(31) = j31
For j30 = 37 To 37                                          'a(30)
If b(j30) = 0 Then b(j30) = j30: c(30) = j30 Else GoTo 300
j29 = 0.5 * s1 - j30 - j31 - j32
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
a(30) = j30: a(29) = j29

For j28 = m1 To m2                                           'a(28)
If b(j28) = 0 Then b(j28) = j28: c(28) = j28 Else GoTo 280
a(28) = j28
For j27 = m1 To m2                                           'a(27)
If b(j27) = 0 Then b(j27) = j27: c(27) = j27 Else GoTo 270
a(27) = j27
For j26 = m1 To m2                                           'a(26)
If b(j26) = 0 Then b(j26) = j26: c(26) = j26 Else GoTo 260
j25 = 0.5 * s1 - j26 - j27 - a(28)
If j25 <= 0 Or j25 > 64 Then GoTo 250                        'a(25)
If b(j25) = 0 Then b(j25) = j25: c(25) = j25 Else GoTo 250
a(26) = j26: a(25) = j25

For j24 = 45 To 45                                           'a(24)
If b(j24) = 0 Then b(j24) = j24: c(24) = j24 Else GoTo 240
a(24) = j24
For j23 = 19 To 19                                           'a(23)
If b(j23) = 0 Then b(j23) = j23: c(23) = j23 Else GoTo 230
a(23) = j23
For j22 = 30 To 30                                           'a(22)
If b(j22) = 0 Then b(j22) = j22: c(22) = j22 Else GoTo 220
j21 = 0.5 * s1 - j22 - j23 - j24
If j21 <= 0 Or j21 > 64 Then GoTo 210
If b(j21) = 0 Then b(j21) = j21: c(21) = j21 Else GoTo 210
a(22) = j22: a(21) = j21

For j20 = m1 To m2                                           'a(20)
If b(j20) = 0 Then b(j20) = j20: c(20) = j20 Else GoTo 200
a(20) = j20
For j19 = m1 To m2                                           'a(19)
If b(j19) = 0 Then b(j19) = j19: c(19) = j19 Else GoTo 190
a(19) = j19
For j18 = m1 To m2                                           'a(18)
If b(j18) = 0 Then b(j18) = j18: c(18) = j18 Else GoTo 180
j17 = 0.5 * s1 - j18 - j19 - j20
If j17 <= 0 Or j17 > 64 Then GoTo 170
If b(j17) = 0 Then b(j17) = j17: c(17) = j17 Else GoTo 170
a(18) = j18: a(17) = j17

For j16 = 28 To 28                                           'a(16)
If b(j16) = 0 Then b(j16) = j16: c(16) = j16 Else GoTo 160

j15 = j16 - a(22) + a(24) - a(29) + a(32)
If j15 <= 0 Or j15 > 64 Then GoTo 150
If b(j15) = 0 Then b(j15) = j15: c(15) = j15 Else GoTo 150
j14 = 0.5 * s1 - j16 - a(23) - a(24) + a(29) - a(32)
If j14 <= 0 Or j14 > 64 Then GoTo 140
If b(j14) = 0 Then b(j14) = j14: c(14) = j14 Else GoTo 140
j13 = -j16 + a(22) + a(23)
If j13 <= 0 Or j13 > 64 Then GoTo 130
If b(j13) = 0 Then b(j13) = j13: c(13) = j13 Else GoTo 130
a(16) = j16: a(15) = j15: a(14) = j14: a(13) = j13

For j12 = m1 To m2                                           'a(12)
If b(j12) = 0 Then b(j12) = j12: c(12) = j12 Else GoTo 120
a(12) = j12

a(11) = a(12) - a(18) + a(20) - a(25) + a(28)
If a(11) <= 0 Or a(11) > 64 Then GoTo 110
a(10) = 0.5 * s1 - a(12) - a(19) - a(20) + a(25) - a(28)
If a(10) <= 0 Or a(10) > 64 Then GoTo 110
a(9) = -a(12) + a(18) + a(19)
If a(9) <= 0 Or a(9) > 64 Then GoTo 110
a(8) = 0.5 * s1 - a(16) - a(24) - a(32)
If a(8) <= 0 Or a(8) > 64 Then GoTo 110
a(7) = -0.5 * s1 - a(16) + a(21) + 2 * a(22) + 2 * a(29) + a(30)
If a(7) <= 0 Or a(7) > 64 Then GoTo 110
a(6) = a(16) - a(21) - 2 * a(22) + a(31) + 2 * a(32)
If a(6) <= 0 Or a(6) > 64 Then GoTo 110
a(5) = a(16) + a(24) - a(29)
If a(5) <= 0 Or a(5) > 64 Then GoTo 110
a(4) = 0.5 * s1 - a(12) - a(20) - a(28)
If a(4) <= 0 Or a(4) > 64 Then GoTo 110
a(3) = -0.5 * s1 - a(12) + a(17) + 2 * a(18) + 2 * a(25) + a(26)
If a(3) <= 0 Or a(3) > 64 Then GoTo 110
a(2) = a(12) - a(17) - 2 * a(18) + a(27) + 2 * a(28)
If a(2) <= 0 Or a(2) > 64 Then GoTo 110
a(1) = a(12) + a(20) - a(25)
If a(1) <= 0 Or a(1) > 64 Then GoTo 110

'                          Exclude solutions with identical numbers

                           GoSub 800: If fl1 = 0 Then GoTo 110
                           
                           n9 = n9 + 1
                           GoSub 650 'Print results (squares)
'                          GoSub 645 'Print results (selected numbers)
    
    
110 b(c(12)) = 0: c(12) = 0
120 Next j12

    b(c(13)) = 0: c(13) = 0
130 b(c(14)) = 0: c(14) = 0
140 b(c(15)) = 0: c(15) = 0
150 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(19)) = 0: c(19) = 0
190 Next j19
    b(c(20)) = 0: c(20) = 0
200 Next j20

    b(c(21)) = 0: c(21) = 0
210 b(c(22)) = 0: c(22) = 0
220 Next j22
    b(c(23)) = 0: c(23) = 0
230 Next j23
    b(c(24)) = 0: c(24) = 0
240 Next j24

    b(c(25)) = 0: c(25) = 0
250 b(c(26)) = 0: c(26) = 0
260 Next j26
    b(c(27)) = 0: c(27) = 0
270 Next j27
    b(c(28)) = 0: c(28) = 0
280 Next j28

    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

    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 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 Next j44

    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 Next j48

    b(c(49)) = 0: c(49) = 0
490 b(c(50)) = 0: c(50) = 0
500 Next j50
    b(c(51)) = 0: c(51) = 0
510 Next j51
    b(c(52)) = 0: c(52) = 0
520 Next j52

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

    b(c(57)) = 0: c(57) = 0
570 b(c(58)) = 0: c(58) = 0
580 Next j58
    b(c(59)) = 0: c(59) = 0
590 Next j59
    b(c(60)) = 0: c(60) = 0
600 Next j60

    b(c(61)) = 0: c(61) = 0
610 b(c(62)) = 0: c(62) = 0
620 Next j62
    b(c(63)) = 0: c(63) = 0
630 Next j63
    b(c(64)) = 0: c(64) = 0
640 Next j64


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

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 = 0
    Else
        If n9 > 1 Then k2 = k2 + 9
    End If

    Cells(k1 + 1, k2 + 1).Select

    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

End Sub

Vorige Pagina About the Author