I uploaded my Haskell code to http://gist.github.com/625639 I know no perl.

]]>Shorter 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])

For 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.

This 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]);

Would you care to provide an implementation? This sounds like an interesting and useful way of solving the problem.

]]>