Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Can u help me how to do this operation.
VB
Private Sub Max2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Max2ToolStripMenuItem.Click
Dim count As long=0
           For i1 As Integer = 0 To 90
             For i2 As Integer = i1 + 1 To 91
                For i3 As Integer = i2 + 1 To 92
                   For i4 As Integer = i3 + 1 To 93
                        For i5 As Integer = i4 + 1 To 94
                            For i6 As Integer = i5 + 1 To 95
                                For i7 As Integer = i6 + 1 To 96
                                    For i8 As Integer = i7 + 1 To 97
                                        For i9 As Integer = i8 + 1 To 98
                                            For i10 As Integer = i9 + 1 To 99
                                                'input n.
If (i1+i2+i3+i4+i5+i6+i7+i8+i9+i10<n) Then
count+=1
End if
                                            Next i10
                                        Next i9
                                    Next i8
                                Next i7
                            Next i6
                        Next i5
                    Next i4
                Next i3
            Next i2
        Next i1
        MsgBox("completed" &count)
End Sub


Thanks all!
another way to do this work?
Posted
Updated 27-Oct-14 20:02pm
v2
Comments
George Jonsson 27-Oct-14 6:36am    
Shouldn't you tag the question VB instead of C++?
Besides that, what is your question?
I can't know what "Do something with i1,i2,i3,....i10" means in your world.
Member 11182962 28-Oct-14 2:04am    
Edited. Thank u!
Sergey Alexandrovich Kryukov 27-Oct-14 6:50am    
As the inner fragment of the code is just a comment, this code samples does not give any idea of your goal.
Not having this information, it's hard to suggest something useful. At least, you should get rid of those hard-coded immediate constants: 91, 92, etc. You should generated them during runtime (in another loop: :-)
Chances are, you may just need a different algorithm. But for what?

Besides, what is your problem? Does your code work? Or do you want just to find more elegant solution, as right no it is really ugly?

—SA

Replace the entire code with a message box. "Please get comfortable, this will take a few thousand years, depending on your hardware."

I'm serious: whatever operation is performed here, it will be repeated on the order of 10^20 times. If your hardware can process about 3*billion operations per second, it will still take a thousand years to complete! (31.7 million seconds per year at 3*billion operations processed per second equates to 95*10^15 or ~10^17 ops per year - so thousand years should be enough to process these loops)

The only way to solve this is programming it on a powerful GPU with a few thousand cores, or maybe a set of 4-8 GPUs such as the rigs used by bitcoin farmers.

If anything, this is hardware question more than a software question - unless the purpose of this question was for you to realize that it may be a good idea to do a sanity check first before starting to program something...
 
Share this answer
 
v2
First of all, please read all comments!

I'm not sure i understand you well, but you - probably - are looking for a way to reduce the count of for loops.

Do you know what is recursion[^]? In your case, i'd suggest to write recursive function[^] similar to:
C++
void RecursionFunction(int currValue, int endValue)
{
  cout << "The number is: " << currValue << " when limit is: " << endValue << endl;
  if(currValue<endvalue)>
  {
    //do what you want to do
    RecursionFunction(currValue+1, endValue);
  }
}
//call
int j = 0;
int k = 90 
for (int l=0; l<=9; l++)
{
    RecursionFunction(j + l, k +l);
}

Note: above code is in C++, because of used tag.
 
Share this answer
 
Comments
Member 11182962 28-Oct-14 2:35am    
I edited my question and I understand your code but i don't know how to use RecursionFunction because want to test i1+i2+...+i10
I assume that n is not to be input in the inner loop, but to be given as a parameter (otherwise the user had to answer many prompts over the next 1000 years).

In that case your code answers the questions: How many combinations of numbers i1 to i10 exists such that their sum is smaller than n, where i1 to i10 are in the range of 0 ... 99 and mutually distinct and in increasing order. Now, that question can be answered analytically, without enumerating all possible combinations of i1 to i10. Here is a hint how it's done:

Consider the simple case of only two variables i1 and i2, each in the range of 1 ... 5 and n = 8. Now you can visualize the combinations of i1 and i2 in the following diagram:
C++
    i2  1  2  3  4  5
i1
1       *  *  *  *  *
2       *  *  *  *  *
3       *  *  *  *
4       *  *  *  
5       *  * 

The * means that the sum of i1 and i2 is smaller than 7. 

Can you see how many combinations there exist? (5 * 5) - 1 - 2 - 3 = 19. Actually, your code does only count the combinations for which the in are mutually distinct and ordered. But you can account for that. Now, the only thing you have to do is construct the general formula for your case and to generalize the problem from 2 to 10 dimensions, which should not be all that hard to do. The resulting code will run a lot faster, probably some micro seconds instead of a thousand years. Some improvement, I guess.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900