Optimization in Visual Basic

-1

For my work, I created the following code (and it works! - although it takes a while). Unfortunately, it is anything but practical. Can you tell me a more pro-elbow way?

This is my code: For 21 different scenarios that can take values from 1 to 5, it shows all the possible combinations. Exit examples:

1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1

1/1/1/1/5/1/1/1/1/1/4/1/1/1/5/5/2/3/1/2/1

5/5/5/1/5/5/5/2/2/2/4/2/3/3/5/5/2/3/1/2/1

    Dim a, b, c, d, f, g, h, j, k, l, m, n, o, p, q, r, s, t, u, w, y, x
    Dim V(21) As Integer
    Dim texto As String
    texto = ""

    For a = 1 To 5
        V(1) = a
        For b = 1 To 5
            V(2) = b
            For c = 1 To 5
                V(3) = c
                For d = 1 To 5
                    V(4) = d
                    For f = 1 To 5
                        V(5) = f
                        For g = 1 To 5
                            V(6) = g
                            For h = 1 To 5
                                V(7) = h
                                For j = 1 To 5
                                    V(8) = j
                                    For k = 1 To 5
                                        V(9) = k
                                        For l = 1 To 5
                                            V(10) = l
                                            For m = 1 To 5
                                                V(11) = m
                                                For n = 1 To 5
                                                    V(12) = n
                                                    For o = 1 To 5
                                                        V(13) = o
                                                        For p = 1 To 5
                                                            V(14) = p
                                                            For q = 1 To 5
                                                                V(15) = q
                                                                For r = 1 To 5
                                                                    V(16) = r
                                                                    For s = 1 To 5
                                                                        V(17) = s
                                                                        For t = 1 To 5
                                                                            V(18) = t
                                                                            For u = 1 To 5
                                                                                V(19) = u
                                                                                For w = 1 To 5
                                                                                    V(20) = w
                                                                                    For y = 1 To 5
                                                                                        V(21) = y
                                                                                        For x = 1 To 21
                                                                                            texto = texto & V(x) & " / "
                                                                                        Next
                                                                                        ListBox1.Items.Add(texto)
                                                                                        texto = ""
                                                                                    Next
                                                                                Next
                                                                            Next
                                                                        Next
                                                                    Next
                                                                Next
                                                            Next
                                                        Next

                                                    Next
                                                Next
                                            Next
                                        Next
                                    Next
                                Next

                            Next
                        Next

                    Next
                Next
            Next
        Next
    Next

End Sub
    
asked by Lu Costa 18.12.2018 в 17:53
source

1 answer

2

(Sorry to put this here, since it is not properly an answer, but it is that what I wanted to say is a bit long for a comment.) If you consider it necessary then I delete it.)

It seems that your code generates permutations that are strings with this look:

"1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1"

and add them to a list, like this:

ListBox1.Items.Add(texto)

And you say it works for you but it takes "a little" ... Frankly I can not understand how it can work. The amount of elements you are adding to that list is astronomical.

Let's put some accounts. Each of these chains is 41 characters long, so even without taking into account possible overhead data structures handled by VB, it will at least occupy 41 bytes.

The number of chains to generate, which are the permutations of 5 elements taken from 21 in 21 with repetition, are 5 21 , no less than 476837158203125 different permutations.

Since each case occupies at least 41 bytes and you save them in ListBox , the amount of memory needed to store everything would be 19550323486328125 bytes !!!! Using more understandable units, it would be more than 17,000 Terabytes of RAM.

How can this work? On the other hand, what's the point?

I think you should clarify what you intend to generate all those permutations. Store them we just saw that it is impossible.

Still printing them on the screen without storing them would be impossible in a human life, assuming you can print a billion (10 9 ) of them per second (which seems completely impossible to me, since I / O operations can not go that fast), it would take about 226 years to finish (I've done the numbers).

So all this for what it is? I have read your comment:

  

For a questionnaire of 21 questions (in which each one has a score of 1 to 5) we want to study the possible answers. The code works, although it takes a lot of time.

but still does not clarify what this study consists of. If all you need to know is how many they are, I've told you before. If there are aspects such as "how many contain the answer 4" and similar, all of them could be calculated with formulas without the need to generate them all (and then process them). I can not think what else you may want to determine. Can you be more specific?

    
answered by 19.12.2018 в 10:07