All pastes #984353 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-14 03:49 UTC
rendered paste body
# Given a list of positive integers L, and two positive integers n and s,
# the following Maple function returns a subset of the first n
# members of L which adds up to s, or FAIL if no such subset exists.

subsetsum := proc(n,s,L) option remember; local sub;
if n=1 then 
    if s=L[1] then 
        return [s] 
        else return FAIL 
    end if;
end if;
sub := subsetsum(n-1,s,L);
if (sub <> FAIL) then 
    return sub 
end if;
if L[n] > s then 
    return FAIL 
end if;
if L[n] = s then 
    return [s] 
end if;
sub:=subsetsum(n-1,s-L[n],L);
if (sub=FAIL) then 
    return FAIL
    else return [op(sub),L[n]] 
end if;
end;