Hi!
I am trying to compute an array of all n-tuples (as arrays) [a_1,...,a_n] of nonnegative integers satisfying \sum_i a_i = w and a_i <= b_i, for every i, for fixed b_1,...,b_n. The function is supposed to have w,b_1,...,b_n as arguments. The following is my attempt. I have considered the arguments w and x, where x = [b_1,...,b_n]. I am trying to compute recursively. However, the recursion does not seem to work. I get an empty array [] as output when x is not a singleton array. Any advice would be appreciated.
Code:
f = (w,x) -> (
if w < 0 then return []
else
if #x == 1 and w <= last x then return [[w]]
else
if #x == 1 then return []
else
g = [];
y = drop(x,-1);
t = min{w,last x};
for i from 0 when i <= t do (
h = f(w-i,y);
for j from 0 when j < #h do g = append(g,append(h_j,t));
);
return g
)
Thanks,
Venkitesh