Subj : efficient way of processing m combinations of n numbers in p sets To : comp.programming From : Mike Date : Tue Aug 16 2005 04:29 am Hi, I am trying to process all combinations of 6 sets of numbers (using VB). The minimum number in each set will be 2 and the maximum number is definable but will be the same for all 6 sets. The increment in each set is also definable and is also the same for each set. As an example, if I set the Maxnumber as 4 and the increment as 1, I would end up with the following possibilities: 2,2,2,2,2,2 3,2,2,2,2,2 2,3,2,2,2,2 3,3,2,2,2,2 2,2,3,2,2,2 3,2,3,2,2,2 2,3,3,2,2,2 3,3,3,2,2,2 .... .... 4,4,4,4,4,4 etc After each of the combinations is found I wish to perform a calculation. The way I am running it at the moment is 6 nested for loops as in the following: Stp = 1 'increment between numbers in each set Maxnumber = 4 'largest number in each set. For i = 2 To Maxnumber Step Stp For j = 2 To Maxnumber Step Stp For k = 2 To Maxnumber Step Stp For l = 2 To Maxnumber Step Stp For m = 2 To Maxnumber Step Stp For n = 2 To Maxnumber Step Stp ' perform calc on i,j,k,l,m and n next n next m next l next k next j next i The problem I have is that as soon the maximum number is set too high or the increment is too low, the number of calculations increases exponentially and the pc freezes. Ideally I am looking to perform these types of calculations in under 2 seconds. The ideal ranges would be from 2 to 50 in each set with an increment of 0.1, so 2, 2.1, 2.2....up to 50. I would be really grateful if someone could offer some advice on how to perform these calculations in a more efficient manner. For example, would placing all possibilities into an array first and then performing calculations at the end offer any improvements? My final question is would it be possible to vary the number of for loops involved e.g. in the above example, if I only had 3 sets of numbers I would only need 3 for loops to work through all of the possibilities. Many thanks in advance, Mike .