// Solving the knapsack problem: given a number like 151 (the knapsack), fill it up with items here primes // until it is full. Which items do you have to take knapsack := function(Q, t) R := [IntegerRing() | x : x in Q | x le t]; if &+R lt t then return [ ]; elif t in R then return [t]; else for x in R do RR := Exclude(R, x); s := $$(RR, t - x); if not IsEmpty(s) then return Append(s, x); end if; end for; return [ ]; end if; end function; primes := [ p : p in [1..100] | IsPrime(p) ]; k := knapsack(primes, 151); k, &+k;