In comp.lang.c Ben Bacarisse <> wrote:
> Why would you exclude 0, 1, 2 and 4?
Oh well, they're not so hard to calculate, so I just didn't mention them explicitly
> long unsigned npower = 1UL << N;
> for (unsigned long ps = 0; ps < npower; ps++)
> /* use ps here */
>
> In the body of the loop ps represents, in turn, each element of the
> power set of {0... N-1}. Can you see why?
Wow, that's *exactly* why I asked this question. I didn't want to start malloc()ing 2^N
seperate arrays (and I would also have had to store their respective lengths somewhere).
So every number in the range from 0 to 2^N - 1 represents in its bit representation
the "state" of the subset. Meaning if (2^k & ps) is true, then the k-th element of my
set is in the subset represented by ps.
I can't even describe how awesome I find this approach. Thank you very much for this
line of code. It also provides a quick proof of why the powerset has exactly 2^N elements.
> This solution is fine if you really want to list all the possible subset
> sums because this task will be impractical for large N. If your problem
> is really something else that you are not letting on, then there may
> well be superior methods. If so, re-post with the full story in
> comp.programming.
My problem really is sheer bruteforce, and I don't think I'm gonna repost, since I think your
code already solves my problem.
But if I have a similar question in the future I'll know where to post it.
best regards.
--
Sick nature.