in reply to Recursion problem

Let's see... if I use the 5, then I have to form a sum of 16 with the remaining numbers. But if I don't use the 5, then... (is anything starting to click?)

Not that this is going to help you, but here is naive approach using modules available from CPAN. The idea is simply to iterate through all the subsets of the values finding those which satisfy the summation constraint.

use Algorithm::Combinatorics qw(subsets); use List::Util qw(sum); my @data = (5, -6, 8, 10, 12, 3, 10); my $iter = subsets(\@data); while (my $subset = $item->next) { if (sum(@$subset) == 21) { print "found a solution: @$subset\n" } }

Replies are listed 'Best First'.
Re^2: Recursion problem
by someone202 (Initiate) on May 25, 2008 at 08:17 UTC
    Wow man with one sentence "Let's see... if I use the 5, then I have to form a sum of 16 with the remaining numbers. But if I don't use the 5, then... (is anything starting to click?)" you just solved the problem for me. I will try to write it in perl and post it here. Can you please explaine how you thought about this solution ? Thanks.

      I'm glad that's all it took to get you going on a solution. Please post what you come up with - we'd be happy to critique it.