Contribution on Puzzlet #017

From:  Denis Borris [borrisd@ca.inter.net]


In this one, only 2 loops required, since one variable can be
calculated from the other 2. Your 3-looper has 840 combinations;
this one has 19 only:

(a,b,c = quantities, x,y,z = weights; @ = mod in Ubasic)

100 For a = 1 to 5 : x = 17 * a
110 For b = 1 to int((100 - x - 3)/11) : y = 11 * b
120 z = 100 - x - y
130 If z @ 3 <> 0 then 150 else c = z / 3
140 Print a;x;b;y;c;z
150 next b
160 next c

Output:
 1 17  1 11 24 72
 1 17  4 44 13 39
 1 17  7 77  2  6
 2 34  3 33 11 33
 3 51  2 22  9 27
 4 68  1 11  7 21

I know it matters very little in such a small looper;
however, it's a good thing to keep in mind, when larger
loopers are required: as example, looping 10 variables
from 0 to 9 versus looping 9 and calculating the 10th
saves 10^10 - 10^9 = 9 billion loops.



MAIN MENU
CONTRIBUTIONS

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.