Re: Total Combinations Matching a Set Sum



Paul Black wrote:
I would However, Still Appreciate an Explanation of what the
Calculation at the End of the For Loops does where it is Divided by One
Number Taken Away from Another Number.

We're counting integer 6-sets {I,J,K,L,M,N} with I<J<K<L<M<N and I+J+K+L+M+N=Target. Since this requires J>=I+1, K>=I+2, etc., we need
I+(I+1)+(I+2)+...+(I+5)<=Target.
It seems to me the line
For I = minval To (Target + 5) / 6 - 3
does this in integer arithmetic and that getting the correct solution depends critically on how the language interprets this when the upper bound does not give an integer value. The upper bounds actually seem a bit liberal for I,J,K and L, but OK for M; for the program to give the right answer, it is critical that
For M = L+1 To Mmax
does not do any iterations if Mmax<L+1.


The other lines for J,K,... are the same except J+K+L+M+N=Target-I is used to find the highest possible J, etc.: the variable sum is the partial sums I, I+J, etc. depending one the location in the program, so it says Target-sum instead of Target-I, Target-I-J, etc.

If you replace this with
For I = minval To Target
you'll get the same answer provided you change the counting step to
If (M < N <= maxval) Then Cnt = Cnt + 1
but the program will take much longer to run as it will try out all combinations of I<J<K<L<M<N<=maxval, even those with e.g. I+J>Target which couldn't possibly give a solution. The way the program is written ensures that only combinations of I<J<K<L<M are picked so that N=Target-(I+J+K+N+M)>M, so the only thing that remains to be checked is that M<=maxval.


But, as Henry has already pointed out, this will not help you hit the jackpot.

Einar
.