mardi 26 mai 2015

Need to generate 9 unique integers less than 100 such that no two has sum equal to another one/two numbers

I need to generate 9 unique integers less than 100, such that there exists no numbers x,y and z such that x + y = z and no numbers a,b,c and d such that a + b = c + d in VB.

Here's my VB code, but it generates number sets that doesn't satisfy the latter condition.

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Numbers(9) As Integer
    Dim w As Integer
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    S:
    Randomize()
    Numbers(1) = CInt(Int((99 * Rnd()) + 1))
    w = 2
    Do While w < 10
        Numbers(w) = CInt(Int((99 * Rnd()) + 1))
        x = 1
        Do While x < w
            If Numbers(w) = Numbers(x) Then
                GoTo S
            End If
            x = x + 1
        Loop
        w = w + 1
    Loop
    w = 1
    x = 1
    y = 1
    z = 1
    Do While w < 10
        Do While x < 10
            Do While y < 10
                If Numbers(y) = Numbers(x) + Numbers(z) Then
                    GoTo S
                End If
                y = y + 1
            Loop
            x = x + 1
        Loop
        w = w + 1
    Loop
    w = 1
    x = 1
    y = 1
    z = 1
    Do While w < 10
        Do While x < 10
            Do While y < 10
                Do While z < 10
                    If w <> x And w <> y And w <> z And x <> y And x <> z And y <> z And Numbers(x) + Numbers(y) = Numbers(z) + Numbers(w) Then
                        GoTo S
                    End If
                    z = z + 1
                Loop
                y = y + 1
            Loop
            x = x + 1
        Loop
        w = w + 1
    Loop
    Label2.Text = Numbers(1) & " " & Numbers(2) & " " & Numbers(3) & " " & Numbers(4) & " " & Numbers(5) & " " & Numbers(6) & " " & Numbers(7) & " " & Numbers(8) & " " & Numbers(9)
End Sub
End Class

What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire