Comments on: Summing Subsets
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/
I Never Metaop I Didn't LikeMon, 09 Feb 2015 19:36:59 +0000hourly1http://wordpress.com/By: roy_hu
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2452
Thu, 14 Oct 2010 05:46:20 +0000http://justrakudoit.wordpress.com/?p=210#comment-2452I uploaded my Haskell code to http://gist.github.com/625639 I know no perl.
]]>By: Tim Bollman
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2451
Tue, 12 Oct 2010 17:43:25 +0000http://justrakudoit.wordpress.com/?p=210#comment-2451Shorter version that doesn’t reuse the count array but would theoretically have multi-threading. There’s no requirement that @a be presorted:
my @a = …
my $biggest = @a.max
my @counts = (1, 0 xx $biggest)
@counts = @counts >>+<< (0 xx $_, @counts[0 .. $biggest – $_]) for @a
[+] (-@a.elems, @counts[@a])
]]>By: Tim Bollman
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2449
Tue, 12 Oct 2010 03:30:56 +0000http://justrakudoit.wordpress.com/?p=210#comment-2449For the explanation of how that works in more abstract terms than what I do:
1. We start with an array of how many ways we can get to a particular number given all the numbers smaller than the current one.
2. We add the current number to all numbers that could previously be reached to get the list of all numbers that can be reached using the current number and any of the previous numbers.
3. Pairwise add the list of previous counts to the ways we can get to numbers using the current number.
4. Step to the next number in our master set until exhausted.
5. sum up all the counts. Subtract out the number of elements in the master set because our count set contains the 1 element subsets which aren’t allowed in the problem.
My program merges steps 2 and 3 because as long as you go from the big numbers to the small numbers, you don’t need to keep track of the old counts. And messes up step 2 a little because I should have initialized @count as (1, 0 xx 99) so I could remove the @counts[$a]++. Also the .. 100 should be .. 99.
]]>By: Tim Bollman
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2448
Tue, 12 Oct 2010 03:14:51 +0000http://justrakudoit.wordpress.com/?p=210#comment-2448This should do the trick:
my @a = 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99;
my @counts = 0 xx 100;
for @a -> $a
{
for reverse $a .. 100 -> $b
{
if @counts[$b – $a] != 0
{
@counts[$b] += @counts[$b – $a];
}
}
@counts[$a]++;
}
say [+] (-@a.elems, @counts[@a]);
]]>By: colomon
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2447
Mon, 11 Oct 2010 18:19:47 +0000http://justrakudoit.wordpress.com/?p=210#comment-2447Would you care to provide an implementation? This sounds like an interesting and useful way of solving the problem.
]]>By: roy_hu
https://justrakudoit.wordpress.com/2010/10/09/summing-subsets/#comment-2446
Mon, 11 Oct 2010 18:16:52 +0000http://justrakudoit.wordpress.com/?p=210#comment-2446You could use dynamic programming to solve this problem instantly. To start, build a 2D-table in which the rows represent the source set, and the columns represent sum targets between 3 and 99.
]]>