Results 1 to 18 of 18

Thread: Magic Squares

Threaded View

  1. #18
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Well, I edited it seeing as it was using numbers more than once in the same box:

    VB Code:
    1. 'Needs: a command button (Command1), a listbox (List1)
    2.  
    3. Option Explicit
    4.  
    5.  
    6. Private Sub Command1_Click()
    7. Dim i As Long
    8. Dim i2 As Long
    9. Dim i3 As Long
    10. Dim i4 As Long
    11. Dim i5 As Long
    12.  
    13. Dim RndVal1 As Long
    14. Dim RndVal2 As Long
    15. Dim RndVal3 As Long
    16.  
    17. Dim Number(1 To 9) As Long
    18. Dim Permutations(1 To 3, 1 To 504) As Long
    19. Dim PermutationsCulled() As Long
    20. Dim CrossCheck(1 To 9) As Long
    21. Dim TotalNumber As Single
    22.  
    23. 'Here is where you store the numbers. Get them however you feel like,
    24.     'just store them in this array
    25.     'Ex. 0 1 2 4 6 8 5 10 9 should be entered like this:
    26. Number(1) = 0
    27. Number(2) = 1
    28. Number(3) = 2
    29. Number(4) = 4
    30. Number(5) = 6
    31. Number(6) = 8
    32. Number(7) = 5
    33. Number(8) = 10
    34. Number(9) = 9
    35.  
    36. 'Find all 504 permutations
    37. Randomize
    38. For i = 1 To 504
    39. ReInit:
    40.     RndVal1 = Int(Rnd * 9) + 1
    41.     RndVal2 = Int(Rnd * 9) + 1
    42.     RndVal3 = Int(Rnd * 9) + 1
    43.     Do Until RndVal2 <> RndVal1
    44.         RndVal2 = Int(Rnd * 9) + 1
    45.     Loop
    46.     Do Until RndVal3 <> RndVal2 And RndVal3 <> RndVal1
    47.         RndVal3 = Int(Rnd * 9) + 1
    48.     Loop
    49.     For i2 = 1 To 504
    50.         If Permutations(1, i2) = RndVal1 And Permutations(2, i2) = RndVal2 And Permutations(3, i2) = RndVal3 Then GoTo ReInit
    51.     Next i2
    52.     Permutations(1, i) = RndVal1
    53.     Permutations(2, i) = RndVal2
    54.     Permutations(3, i) = RndVal3
    55. Next i
    56.  
    57. 'Transfer the array while cutting out all permutations that do not
    58.     'add up to Total Number / 3 plus checking to make sure all numbers
    59.     'in Number array are used exactly once and displaying the % done
    60. ReDim PermutationsCulled(1 To 3, 1 To 1) As Long
    61. For i = 1 To 9
    62.     TotalNumber = TotalNumber + Number(i)
    63. Next i
    64. For i = 1 To 504
    65.     If Number(Permutations(1, i)) + Number(Permutations(2, i)) + Number(Permutations(3, i)) = TotalNumber / 3 Then
    66.         PermutationsCulled(1, UBound(PermutationsCulled, 2)) = Number(Permutations(1, i))
    67.         PermutationsCulled(2, UBound(PermutationsCulled, 2)) = Number(Permutations(2, i))
    68.         PermutationsCulled(3, UBound(PermutationsCulled, 2)) = Number(Permutations(3, i))
    69.        
    70.         If i <> 504 Then ReDim Preserve PermutationsCulled(1 To 3, 1 To UBound(PermutationsCulled, 2) + 1) As Long
    71.     End If
    72. Next i
    73.  
    74. 'Basically, loop through all possible squares and check whether or
    75.     'not it's a magic square. Also, loop through the list and check whether
    76.     'or not it's already been found
    77. For i = 1 To UBound(PermutationsCulled, 2)
    78. For i2 = 1 To UBound(PermutationsCulled, 2)
    79. For i3 = 1 To UBound(PermutationsCulled, 2)
    80.     If PermutationsCulled(1, i) + PermutationsCulled(1, i2) + PermutationsCulled(1, i3) = TotalNumber / 3 Then
    81.     If PermutationsCulled(2, i) + PermutationsCulled(2, i2) + PermutationsCulled(2, i3) = TotalNumber / 3 Then
    82.     If PermutationsCulled(3, i) + PermutationsCulled(3, i2) + PermutationsCulled(3, i3) = TotalNumber / 3 Then
    83.         For i4 = 0 To List1.ListCount - 1 Step 4
    84.             If List1.List(i4) = PermutationsCulled(1, i) & " " & PermutationsCulled(2, i) & " " & PermutationsCulled(3, i) Then
    85.             If List1.List(i4 + 1) = PermutationsCulled(1, i2) & " " & PermutationsCulled(2, i2) & " " & PermutationsCulled(3, i2) Then
    86.             If List1.List(i4 + 2) = PermutationsCulled(1, i3) & " " & PermutationsCulled(2, i3) & " " & PermutationsCulled(3, i3) Then
    87.                 GoTo SkipIf
    88.             End If
    89.             End If
    90.             End If
    91.         Next i4
    92.        
    93.         For i4 = 1 To 9
    94.             CrossCheck(i4) = Number(i4)
    95.         Next i4
    96.         For i4 = 1 To 3
    97.             For i5 = 1 To 9
    98.                 If CrossCheck(i5) = PermutationsCulled(i4, i) And CrossCheck(i5) <> 0 Then
    99.                     CrossCheck(i5) = 0
    100.                     Exit For
    101.                 End If
    102.             Next i5
    103.         Next i4
    104.         For i4 = 1 To 3
    105.             For i5 = 1 To 9
    106.                 If CrossCheck(i5) = PermutationsCulled(i4, i2) And CrossCheck(i5) <> 0 Then
    107.                     CrossCheck(i5) = 0
    108.                     Exit For
    109.                 End If
    110.             Next i5
    111.         Next i4
    112.         For i4 = 1 To 3
    113.             For i5 = 1 To 9
    114.                 If CrossCheck(i5) = PermutationsCulled(i4, i3) And CrossCheck(i5) <> 0 Then
    115.                     CrossCheck(i5) = 0
    116.                     Exit For
    117.                 End If
    118.             Next i5
    119.         Next i4
    120.         i5 = 0
    121.         For i4 = 1 To 9
    122.             i5 = i5 + CrossCheck(i4)
    123.         Next i4
    124.        
    125.         If i5 <> 0 Then GoTo SkipIf
    126.        
    127.         List1.AddItem PermutationsCulled(1, i) & " " & PermutationsCulled(2, i) & " " & PermutationsCulled(3, i)
    128.         List1.AddItem PermutationsCulled(1, i2) & " " & PermutationsCulled(2, i2) & " " & PermutationsCulled(3, i2)
    129.         List1.AddItem PermutationsCulled(1, i3) & " " & PermutationsCulled(2, i3) & " " & PermutationsCulled(3, i3)
    130.         List1.AddItem ""
    131.        
    132.         Me.Refresh
    133.         DoEvents
    134.     End If
    135.     End If
    136.     End If
    137. SkipIf:
    138. Next i3
    139. Next i2
    140. Command1.Caption = Int(i / UBound(PermutationsCulled, 2) * 100) & "%"
    141. Next i
    142.  
    143. Command1.Caption = "Command1"
    144. End Sub

    That should give you every single magic square a block of numbers can make. Again, it's amazingly fast for all the loops it uses. Please do note that the more possible magic square there are, the longer it will take (ie, [2, 2, 2, 2, 2, 2, 2, 2, 2] will take forever compared to [0, 1, 2, 4, 6, 8, 5, 10, 9] which will take maybe half a second]

    Edit: Somehow my mind wandered back to this and thought the diagonals aren't being checked (though they aren't mentioned in the problem). To check them too, you'd use this:

    VB Code:
    1. If PermutationsCulled(1, i) + PermutationsCulled(1, i2) + PermutationsCulled(1, i3) = TotalNumber / 3 Then
    2.     If PermutationsCulled(2, i) + PermutationsCulled(2, i2) + PermutationsCulled(2, i3) = TotalNumber / 3 Then
    3.     If PermutationsCulled(3, i) + PermutationsCulled(3, i2) + PermutationsCulled(3, i3) = TotalNumber / 3 Then
    4.     [b]If PermutationsCulled(1, i) + PermutationsCulled(2, i2) + PermutationsCulled(3, i3) = TotalNumber / 3 Then[/b]
    5.     [b]If PermutationsCulled(3, i) + PermutationsCulled(2, i2) + PermutationsCulled(1, i3) = TotalNumber / 3 Then[/b]

    with the proper End If's
    Last edited by jemidiah; Oct 13th, 2003 at 06:06 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width