Hilmar,
>Another note: Neither of these algorithms can be solved with simple SQL statements. You will have to program loops.
See my all SQL solution posted in the other thread. The one scan loop is just an performance optimization to help limit the exponential growth this problem faces.