A
ar0
Hi,
I'm crossposting this to comp.lang.c and comp.theory, because I'm coding
in c, but at the same time it's a general algorithm problem.
If it doesn't fit in either of those, please inform me.
Now my problem: I have an integer array of length N and I want to calculate
all possible sums of elements from that array. For example, if my array were
of length 3 and it would look like: [1, 4, 2], I would want to calculate:
1+4
1+2
1+4+2
4+2
Now, I imagine I could do this somehow if I could generate the "power set" of
an array containing the numbers from 0 to N-1 and using those "subsets" as indices.
So I'm asking: is there a "reasonable" (<300 lines of code) algorithm for getting
the "powerset" (ie every single "subset" as an extra array) of the array [0, 1, ..., N-1]?
Or perhaps there's a better way of approaching my problem. I would certainly be open to suggestions.
best regards.
I'm crossposting this to comp.lang.c and comp.theory, because I'm coding
in c, but at the same time it's a general algorithm problem.
If it doesn't fit in either of those, please inform me.
Now my problem: I have an integer array of length N and I want to calculate
all possible sums of elements from that array. For example, if my array were
of length 3 and it would look like: [1, 4, 2], I would want to calculate:
1+4
1+2
1+4+2
4+2
Now, I imagine I could do this somehow if I could generate the "power set" of
an array containing the numbers from 0 to N-1 and using those "subsets" as indices.
So I'm asking: is there a "reasonable" (<300 lines of code) algorithm for getting
the "powerset" (ie every single "subset" as an extra array) of the array [0, 1, ..., N-1]?
Or perhaps there's a better way of approaching my problem. I would certainly be open to suggestions.
best regards.