Subset sum problem: Difference between revisions

m
m (→‎{{header|REXX}}: added/changed comments and whitespace, re-arranged the list of words for a smaller footprint within the program.)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 10 users not shown)
Line 91:
Note that an implementation that depends on enumerating all possible subsets is likely to be infeasible.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V words = [
‘alliance’ = -624, ‘archbishop’ = -925, ‘balm’ = 397,
‘bonnet’ = 452, ‘brute’ = 870, ‘centipede’ = -658,
‘cobol’ = 362, ‘covariate’ = 590, ‘departure’ = 952,
‘deploy’ = 44, ‘diophantine’ = 645, ‘efferent’ = 54,
‘elysee’ = -326, ‘eradicate’ = 376, ‘escritoire’ = 856,
‘exorcism’ = -983, ‘fiat’ = 170, ‘filmy’ = -874,
‘flatworm’ = 503, ‘gestapo’ = 915, ‘infra’ = -847,
‘isis’ = -982, ‘lindholm’ = 999, ‘markham’ = 475,
‘mincemeat’ = -880, ‘moresby’ = 756, ‘mycenae’ = 183,
‘plugging’ = -266, ‘smokescreen’ = 423, ‘speakeasy’ = -745,
‘vein’ = 813
]
 
V neg = 0
V pos = 0
L(w, v) words
I v > 0
pos += v
E
neg += v
 
V sums = [[String]()] * (pos - neg + 1)
 
L(w, v) words
V s = copy(sums)
I s[v - neg].empty
s[v - neg] = [w]
 
L(w2) sums
V i = L.index
I !w2.empty & s[i + v].empty
s[i + v] = w2 [+] [w]
 
sums = s
I !s[-neg].empty
L(x) s[-neg]
print(x‘ ’words[x])
L.break</syntaxhighlight>
 
{{out}}
<pre>
alliance -624
archbishop -925
balm 397
bonnet 452
centipede -658
cobol 362
departure 952
deploy 44
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
DEFINE PAIR_SIZE="4"
 
TYPE Pair=[PTR word INT weight]
 
BYTE ARRAY pairs(200)
BYTE count=[0]
 
PTR FUNC GetPairAddr(INT index)
RETURN (pairs+index*PAIR_SIZE)
 
PROC PrintWords(BYTE ARRAY indices BYTE len)
Pair POINTER p
BYTE i
 
FOR i=0 TO len-1
DO
IF i>0 THEN Put(' ) FI
p=GetPairAddr(indices(i))
Print(p.word)
OD
PutE()
RETURN
 
PROC Append(CHAR ARRAY wrd INT wght)
Pair POINTER dst
 
dst=GetPairAddr(count)
dst.word=wrd
dst.weight=wght
count==+1
RETURN
 
PROC Init()
Append("alliance",-624) Append("archbishop",-915)
Append("balm",397) Append("bonnet",452)
Append("brute",870) Append("centipede",-658)
Append("cobol",362) Append("covariate",590)
Append("departure",952) Append("deploy",44)
Append("diophantine",645) Append("efferent",54)
Append("elysee",-326) Append("eradicate",376)
Append("escritoire",856) Append("exorcism",-983)
Append("fiat",170) Append("filmy",-874)
Append("flatworm",503) Append("gestapo",915)
Append("infra",-847) Append("isis",-982)
Append("lindholm",999) Append("markham",475)
Append("mincemeat",-880) Append("moresby",756)
Append("mycenae",183) Append("plugging",-266)
Append("smokescreen",423) Append("speakeasy",-745)
Append("vein",813)
RETURN
 
INT FUNC Sum(BYTE ARRAY indices BYTE len)
Pair POINTER p
INT sum
BYTE i
 
sum=0
FOR i=0 TO len-1
DO
p=GetPairAddr(indices(i))
sum==+p.weight
OD
RETURN (sum)
 
BYTE FUNC NextSubset(BYTE ARRAY indices BYTE len)
INT i,j
 
i=len-1
WHILE i>=0
DO
IF indices(i)#i+count-len THEN
indices(i)==+1
FOR j=i+1 TO len-1
DO
indices(j)=indices(j-1)+1
OD
RETURN (1)
FI
i==-1
OD
RETURN (0)
 
PROC Test(INT len)
BYTE ARRAY indices(100)
BYTE i
 
PrintF("%I: ",len)
FOR i=0 TO len-1
DO
indices(i)=i
OD
DO
IF Sum(indices,len)=0 THEN
PrintWords(indices,len) PutE()
RETURN
FI
IF NextSubset(indices,len)=0 THEN
PrintE("no subset") PutE()
RETURN
FI
OD
RETURN
 
PROC Main()
Init()
Test(2)
Test(3)
Test(4)
Test(5)
Test(10)
Test(27)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Subset_sum_problem.png Screenshot from Atari 8-bit computer]
<pre>
2: archbishop gestapo
 
3: centipede markham mycenae
 
4: alliance balm deploy mycenae
 
5: alliance brute covariate deploy mincemeat
 
10: alliance archbishop balm bonnet brute centipede cobol departure deploy mincemeat
 
27: alliance archbishop balm bonnet brute centipede covariate departure deploy efferent elysee eradicate escritoire exorcism fiat filmy flatworm infra isis lindholm markham mincemeat moresby mycenae plugging smokescreen speakeasy
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure SubsetSum is
Line 148 ⟶ 333:
end loop;
end loop;
end SubsetSum;</langsyntaxhighlight>
{{out}}
<pre>2: archbishop gestapo
Line 178 ⟶ 363:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 242 ⟶ 427:
subsum(0, 0);
return 0;
}</langsyntaxhighlight>
 
{{output}}<pre>alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate escritoire exorcism fiat filmy flatworm mincemeat plugging speakeasy
Line 249 ⟶ 434:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 330 ⟶ 515:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The weights of the following 5 subsets add up to zero:
Line 346 ⟶ 531:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 413 ⟶ 598:
subsum(0, 0);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate escritoire exorcism fiat filmy flatworm mincemeat plugging speakeasy
Line 424 ⟶ 609:
A simple brute-force solution. This used the module of the third D solution of the Combinations Task.
{{trans|Ruby}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.typecons, combinations3;
 
Line 447 ⟶ 632:
comb.map!q{ a[0] });
"No solution found.".writeln;
}</langsyntaxhighlight>
{{out}}
<pre>A subset of length 2: archbishop, gestapo</pre>
Line 454 ⟶ 639:
This version prints all the 349_167 solutions in about 1.8 seconds and counts them in about 0.05 seconds.
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
enum showAllSolutions = true;
Line 550 ⟶ 735:
 
writeln("Zero sums: ", sols);
}</langsyntaxhighlight>
{{out}}
<pre>Zero sums: 349167</pre>
Line 558 ⟶ 743:
We use the Pseudo-polynomial time dynamic programming solution, found in the [[wp:Subset sum problem|Subset sum problem]] Wikipedia article. If A and B are the min and max possible sums, the time and memory needed are '''O((B-A)*N)'''. '''Q''' is an array such as Q(i,s) = true if there is a nonempty subset of x0, ..., xi which sums to s.
<langsyntaxhighlight lang="scheme">
;; 0 <= i < N , A <= s < B , -A = abs(A)
;; mapping two dims Q(i,s) to one-dim Q(qidx(i,s)) :
Line 605 ⟶ 790:
(map (lambda(i) (first (list-ref input i))) (fillQ (map rest input))))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 660 ⟶ 845:
===Brute force===
We use the '''powerset''' procrastinator which gives in sequence all subsets of the input list.
<langsyntaxhighlight lang="scheme">
(lib 'sequences) ;; for powerset
 
Line 687 ⟶ 872:
("deploy" . 44) ("diophantine" . 645) ("efferent" . 54)
("elysee" . -326) ("eradicate" . 376))
</syntaxhighlight>
</lang>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">def subsetSum( s, w, v ) =
def sumset( a ) = foldl1( (+), map(w, a) )
Line 734 ⟶ 919:
 
for i <- 0..5
println( i, subsetSum(s, snd, i).get() )</langsyntaxhighlight>
 
{{out}}
Line 748 ⟶ 933:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 814 ⟶ 999:
}
fmt.Println("no subset sums to 0")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 830 ⟶ 1,015:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">combinations :: Int -> [a] -> [[a]]
combinations 0 _ = [[]]
combinations _ [] = []
Line 861 ⟶ 1,046:
W "vein" 813]
 
main = print $ map word $ head $ solver items</langsyntaxhighlight>
{{out}}
<pre>["archbishop","gestapo"]</pre>
 
Non brute-force: the list of numbers used here are different, and difficult for a bruteforce method.
<langsyntaxhighlight lang="haskell">subsum :: Int -> [Int] -> [Int]
subsum w =
snd . head . filter ((== w) . fst) . (++ [(w, [])]) . foldl s [(0, [])]
Line 888 ⟶ 1,073:
 
main :: IO ()
main = print $ subsum 0 items</langsyntaxhighlight>
{{out}}
<pre>[-61,32,373,311,249,311,32,-92,-185,-433,-402,-247,156,125,249,32,-464,-278,218,32,-123,-216,373,-185,-402,156,-402,-61,902]</pre>
Line 894 ⟶ 1,079:
=={{header|Icon}} and {{header|Unicon}}==
{{trans|Ruby}}
<langsyntaxhighlight Iconlang="icon">link printf,lists
 
procedure main()
Line 939 ⟶ 1,124:
 
return T
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 981 ⟶ 1,166:
Task data:
 
<langsyntaxhighlight Jlang="j">text=:0 :0
alliance -624
archbishop -915
Line 1,016 ⟶ 1,201:
 
words=:{.@;:;._2 text
numbs=:+/|:0&".;._2 text</langsyntaxhighlight>
 
Implementation:
 
<langsyntaxhighlight Jlang="j">wsum0=:4 :0
p=:(#~ 0&<)y
n=:(#~ 0&>)y
Line 1,029 ⟶ 1,214:
keep=: [ #~ #&2@#@[ #: choose i.~ ]
;:inv words #~y e. (p keep P),n keep N
)</langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> words wsum0 numbs
centipede markham mycenae</langsyntaxhighlight>
 
Note also that there are over 300,000 valid solutions here. More than can be comfortably displayed:
 
<langsyntaxhighlight Jlang="j"> Ps=: </.~ /:~ (I.P e. N){P
Ns=: </.~ /:~ (I.N e. P){N
+/#@,@{"1 Ps,.Ns
349168</langsyntaxhighlight>
 
(One of those is the empty solution, but the rest of them are valid.)
Line 1,047 ⟶ 1,232:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class SubsetSum {
private static class Item {
private String word;
Line 1,124 ⟶ 1,309:
zeroSum(0, 0);
}
}</langsyntaxhighlight>
{{out}}
<pre>The weights of the following 5 subsets add up to zero:
Line 1,137 ⟶ 1,322:
 
(alliance, -624) (archbishop, -915) (balm, 397) (bonnet, 452) (brute, 870) (centipede, -658) (cobol, 362) (covariate, 590) (departure, 952) (deploy, 44) (diophantine, 645) (efferent, 54) (elysee, -326) (eradicate, 376) (exorcism, -983) (fiat, 170) (infra, -847) (isis, -982) (smokescreen, 423)</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq''' (provided `keys_unsorted` is replaced by `keys`)
<syntaxhighlight lang="jq">
# Input: an array of n elements, each of which is either in or out
# Output: a stream of the 2^n possible selections
def selections:
# map( [null, .] ) | combinations | map(select(.));
if length == 0
then .
else .[0] as $x
| .[1:] | selections | ., ([$x] + .)
end ;
 
# input: a JSON object giving the weights
def zero_sums:
def sum($dict; $sum):
map( $dict[.] ) | add == $sum;
. as $dict
| keys_unsorted | selections | select( sum($dict; 0));
</syntaxhighlight>
'''An Example'''
<syntaxhighlight lang="jq">def weights:
{
"alliance": -624,
"archbishop": -915,
"balm": 397,
"bonnet": 452,
"brute": 870,
"centipede": -658,
"cobol": 362,
"covariate": 590,
"departure": 952,
"deploy": 44,
"diophantine": 645,
"efferent": 54,
"elysee": -326,
"eradicate": 376,
"escritoire": 856,
"exorcism": -983,
"fiat": 170,
"filmy": -874,
"flatworm": 503,
"gestapo": 915,
"infra": -847,
"isis": -982,
"lindholm": 999,
"markham": 475,
"mincemeat": -880,
"moresby": 756,
"mycenae": 183,
"plugging": -266,
"smokescreen": 423,
"speakeasy": -745,
"vein": 813
};
 
weights | first(zero_sums)</syntaxhighlight>
{{out}}
<pre>
[
"archbishop",
"balm",
"bonnet",
"centipede",
"cobol",
"covariate",
"deploy",
"efferent",
"elysee"
]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const pairs = [
Line 1,169 ⟶ 1,428:
 
zerosums()
</langsyntaxhighlight>{{out}}
<pre>
For length 1: None
Line 1,208 ⟶ 1,467:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Item(val word: String, val weight: Int) {
Line 1,271 ⟶ 1,530:
println("The weights of the following $LIMIT subsets add up to zero:\n")
zeroSum(0, 0)
}</langsyntaxhighlight>
 
{{out}}
Line 1,288 ⟶ 1,547:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a = {{"alliance", -624}, {"archbishop", -915}, {"balm", 397}, {"bonnet", 452},
{"brute", 870}, {"centipede", -658}, {"cobol", 362}, {"covariate", 590},{"departure", 952},
{"deploy", 44}, {"diophantine", 645}, {"efferent", 54}, {"elysee", -326}, {"eradicate", 376},
Line 1,298 ⟶ 1,557:
 
result = Rest@Select[ Subsets[a, 7], (Total[#[[;; , 2]]] == 0) &];
Map[ (Print["A zero-sum subset of length ", Length[#], " : ", #[[;; , 1]]])& , result ]</langsyntaxhighlight>
 
<pre>A zero-sum subset of length 2 : {archbishop,gestapo}
Line 1,311 ⟶ 1,570:
The above code uses a brute-force approach, but Mathematica includes several solution schemes that can be used to solve this problem. We can cast it as an integer linear programming problem, and thus find the largest or smallest subset sum, or even sums with specific constraints, such as a sum using three negative values and nine positive values.
 
<langsyntaxhighlight Mathematicalang="mathematica">a = {{"alliance", -624}, {"archbishop", -915}, {"balm", 397}, {"bonnet", 452},
{"brute", 870}, {"centipede", -658}, {"cobol", 362}, {"covariate", 590},{"departure", 952},
{"deploy", 44}, {"diophantine", 645}, {"efferent", 54}, {"elysee", -326}, {"eradicate", 376},
Line 1,345 ⟶ 1,604:
 
Print["3 -ves, 9 +ves: ", Select[Transpose[{threeNineSoln*aValues, aNames}], #[[1]] != 0 &]];
</syntaxhighlight>
</lang>
 
<pre>Maximal solution: {{-624, alliance}, {-915, archbishop}, {397, balm},
Line 1,362 ⟶ 1,621:
{423, smokescreen}}.</pre>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
%Subset sum. Nigel Galloway: January 6th., 2021.
enum Items={alliance,archbishop,balm,bonnet,brute,centipede,cobol,covariate,departure,deploy,diophantine,efferent,elysee,eradicate,escritoire,exorcism,fiat,filmy,flatworm,gestapo,infra,isis,lindholm,markham,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein};
array[Items] of int: weight=[-624,-915,397,452,870,-658,362,590,952,44,645,54,-326,376,856,-983,170,-874,503,915,-847,-982,999,475,-880,756,183,-266,423,-745,813];
var set of Items: selected;
var int: wSelected=sum(n in selected)(weight[n]);
constraint wSelected=0;
</syntaxhighlight>
{{out}}
<pre>
selected = {alliance, archbishop, balm, bonnet, brute, centipede, cobol, covariate, departure, deploy, diophantine, efferent, elysee, eradicate, escritoire, exorcism, fiat, filmy, flatworm, mincemeat, plugging, speakeasy};
----------
Finished in 185msec
</pre>
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE SubsetSum;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,464 ⟶ 1,738:
 
ReadChar;
END SubsetSum.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|itertools}}
We need the third party module "itertools" to compute the combinations.
<syntaxhighlight lang="nim">import sequtils, strformat, strutils
import itertools
 
const Words = {"alliance": -624,
"archbishop": -915,
"balm": 397,
"bonnet": 452,
"brute": 870,
"centipede": -658,
"cobol": 362,
"covariate": 590,
"departure": 952,
"deploy": 44,
"diophantine": 645,
"efferent": 54,
"elysee": -326,
"eradicate": 376,
"escritoire": 856,
"exorcism": -983,
"fiat": 170,
"filmy": -874,
"flatworm": 503,
"gestapo": 915,
"infra": -847,
"isis": -982,
"lindholm": 999,
"markham": 475,
"mincemeat": -880,
"moresby": 756,
"mycenae": 183,
"plugging": -266,
"smokescreen": 423,
"speakeasy": -745,
"vein": 813}
 
var words: seq[string]
var values: seq[int]
for (word, value) in Words:
words.add word
values.add value
 
for lg in 1..words.len:
block checkCombs:
for comb in combinations(words.len, lg):
var sum = 0
for idx in comb: sum += values[idx]
if sum == 0:
echo &"For length {lg}, found for example: ", comb.mapIt(words[it]).join(" ")
break checkCombs
echo &"For length {lg}, no set found."</syntaxhighlight>
 
{{out}}
<pre>For length 1, no set found.
For length 2, found for example: archbishop gestapo
For length 3, found for example: centipede markham mycenae
For length 4, found for example: alliance balm deploy mycenae
For length 5, found for example: alliance brute covariate deploy mincemeat
For length 6, found for example: alliance archbishop balm deploy gestapo mycenae
For length 7, found for example: alliance archbishop bonnet cobol departure exorcism moresby
For length 8, found for example: alliance archbishop balm bonnet fiat flatworm isis lindholm
For length 9, found for example: alliance archbishop balm bonnet brute covariate eradicate mincemeat plugging
For length 10, found for example: alliance archbishop balm bonnet brute centipede cobol departure deploy mincemeat
For length 11, found for example: alliance archbishop balm bonnet brute centipede cobol departure infra moresby speakeasy
For length 12, found for example: alliance archbishop balm bonnet brute centipede cobol covariate diophantine efferent elysee infra
For length 13, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure efferent eradicate filmy isis
For length 14, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy elysee filmy markham speakeasy
For length 15, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy elysee exorcism flatworm infra mycenae
For length 16, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine elysee exorcism filmy gestapo infra
For length 17, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine exorcism isis mincemeat mycenae plugging vein
For length 18, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee exorcism filmy isis mycenae vein
For length 19, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate exorcism fiat infra isis smokescreen
For length 20, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate exorcism gestapo infra isis smokescreen speakeasy
For length 21, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate exorcism flatworm infra lindholm mincemeat plugging speakeasy
For length 22, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate escritoire exorcism fiat filmy flatworm mincemeat plugging speakeasy
For length 23, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate escritoire exorcism infra isis mincemeat moresby mycenae smokescreen speakeasy
For length 24, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee exorcism filmy gestapo infra markham mincemeat moresby mycenae plugging smokescreen speakeasy
For length 25, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine eradicate exorcism fiat filmy flatworm infra isis lindholm markham mincemeat moresby mycenae plugging speakeasy
For length 26, found for example: alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine elysee eradicate escritoire exorcism fiat filmy gestapo infra isis markham mincemeat mycenae plugging speakeasy vein
For length 27, found for example: alliance archbishop balm bonnet brute centipede covariate departure deploy efferent elysee eradicate escritoire exorcism fiat filmy flatworm infra isis lindholm markham mincemeat moresby mycenae plugging smokescreen speakeasy
For length 28, no set found.
For length 29, no set found.
For length 30, no set found.
For length 31, no set found.</pre>
 
=={{header|OCaml}}==
Line 1,470 ⟶ 1,831:
Just search randomly until a result is found:
 
<langsyntaxhighlight lang="ocaml">let d =
[ "alliance", -624; "archbishop", -915; "balm", 397; "bonnet", 452;
"brute", 870; "centipede", -658; "cobol", 362; "covariate", 590;
Line 1,504 ⟶ 1,865:
in
let res = aux [] d in
List.iter (fun (n,w) -> Printf.printf " %4d\t%s\n" w n) res</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/:all/;
 
my %pairs = (
Line 1,528 ⟶ 1,889:
lastfor, print "Length $n: @names[@_]\n" if vecsum(@weights[@_]) == 0;
} @names, $n;
}</langsyntaxhighlight>
Printing just the first one found for each number of elements:
{{out}}
Line 1,544 ⟶ 1,905:
 
We can also use different modules for this brute force method. Assuming the same pairs/names/weights variables:
<langsyntaxhighlight lang="perl">use List::Util qw/sum/;
use Algorithm::Combinatorics qw/combinations/;
foreach my $n (1 .. @names) {
Line 1,553 ⟶ 1,914:
last;
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Simple Brute force
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence {words,weights} = columnize({{"alliance", -624},
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
{"archbishop", -915},
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"alliance"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">624</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"archbishop"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">915</span><span style="color: #0000FF;">},</span>
{"balm", 397},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"balm"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">397</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"bonnet"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">452</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"brute"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">870</span><span style="color: #0000FF;">},</span>
{"bonnet", 452},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"centipede"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">658</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"cobol"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">362</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"covariate"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">590</span><span style="color: #0000FF;">},</span>
{"brute", 870},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"departure"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">952</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"deploy"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">44</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"diophantine"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">645</span><span style="color: #0000FF;">},</span>
{"centipede", -658},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"efferent"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">54</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"elysee"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">326</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"eradicate"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">376</span><span style="color: #0000FF;">},</span>
{"cobol", 362},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"escritoire"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">856</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"exorcism"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">983</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"fiat"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">170</span><span style="color: #0000FF;">},</span>
{"covariate", 590},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"filmy"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">874</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"flatworm"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">503</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"gestapo"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">915</span><span style="color: #0000FF;">},</span>
{"departure", 952},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"infra"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">847</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"isis"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">982</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"lindholm"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">999</span><span style="color: #0000FF;">},</span>
{"deploy", 44},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"markham"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">475</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"mincemeat"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">880</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"moresby"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">756</span><span style="color: #0000FF;">},</span>
{"diophantine", 645},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"mycenae"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">183</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"plugging"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">266</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"smokescreen"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">423</span><span style="color: #0000FF;">},</span>
{"efferent", 54},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"speakeasy"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">745</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"vein"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">813</span><span style="color: #0000FF;">}})</span>
{"elysee", -326},
{"eradicate", 376},
<span style="color: #008080;">function</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">={})</span>
{"escritoire", 856},
<span style="color: #008080;">if</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- got a full set</span>
{"exorcism", -983},
<span style="color: #008080;">if</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
{"fiat", 170},
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">),</span>
{"filmy", -874},
<span style="color: #000000;">scw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"items"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
{"flatworm", 503},
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scw</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)})</span>
{"gestapo", 915},
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
{"infra", -847},
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{"isis", -982},
<span style="color: #008080;">elsif</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">+</span><span style="color: #000000;">needed</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
{"lindholm", 999},
<span style="color: #000080;font-style:italic;">-- get all combinations with and without the next item:</span>
{"markham", 475},
<span style="color: #000000;">done</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
{"mincemeat", -880},
<span style="color: #008080;">if</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span><span style="color: #000000;">needed</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">[</span><span style="color: #000000;">done</span><span style="color: #0000FF;">]))</span>
{"moresby", 756},
<span style="color: #008080;">or</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span><span style="color: #000000;">needed</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
{"mycenae", 183},
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
{"plugging", -266},
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{"smokescreen", 423},
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{"speakeasy", -745},
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
{"vein", 813}})
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function comb(sequence pool, integer needed, done=0, sequence chosen={})
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">)</span>
if needed=0 then -- got a full set
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
integer t = 0
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(chosen) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: No zero-sum subsets of that length\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
t += weights[chosen[i]]
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if t=0 then
<!--</syntaxhighlight>-->
for i=1 to length(chosen) do
chosen[i] = words[chosen[i]]
end for
printf(1,"%d: %s\n",{length(chosen),sprint(chosen)})
return 1
end if
elsif done+needed<=length(pool) then
-- get all combinations with and without the next item:
done += 1
if comb(pool,needed-1,done,append(chosen,pool[done]))
or comb(pool,needed,done,chosen) then
return 1
end if
end if
return 0
end function
 
integer n = length(weights)
for i=1 to n do
if comb(tagset(n),i)=0 then
printf(1,"%d: No zero-sum subsets of that length\n",{i})
end if
end for</lang>
{{out}}
<pre>
1: No zero-sum subsets of that length
1: No zero-sum subsets of that length
2: {"archbishop" archbishop,"gestapo"}
3: {"centipede" centipede,"markham","mycenae"}
4: {"alliance" alliance,"balm","deploy","mycenae"}
5: {"alliance" alliance,"brute","covariate","deploy","mincemeat"}
6: {"alliance" alliance,"archbishop","balm","deploy","gestapo","mycenae"}
7: {"alliance" alliance,"archbishop","bonnet","cobol","departure","exorcism","moresby"}
8: {"alliance" alliance,"archbishop","balm","bonnet","fiat","flatworm","isis","lindholm"}
9: {"alliance" alliance,"archbishop","balm","bonnet","brute","covariate","eradicate","mincemeat","plugging"}
10: {"alliance" alliance,"archbishop","balm","bonnet","brute","centipede","cobol","departure","deploy","mincemeat"}
11: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","departure","infra","moresby","speakeasy"}, (11 items)
12: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","diophantine","efferent","elysee","infra"}, (12 items)
13: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","efferent","eradicate","filmy","isis"}, (13 items)
14: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","elysee","filmy","markham","speakeasy"}, (14 items)
15: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","elysee","exorcism","flatworm","infra","mycenae"}, (15 items)
16: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","elysee","exorcism","filmy","gestapo","infra"}, (16 items)
17: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","exorcism","isis","mincemeat","mycenae","plugging","vein"}, (17 items)
18: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","exorcism","filmy","isis","mycenae","vein"}, (18 items)
19: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","eradicate","exorcism","fiat","infra","isis","smokescreen"}, (19 items)
20: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","eradicate","exorcism","gestapo","infra","isis","smokescreen","speakeasy"}, (20 items)
21: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","eradicate","exorcism","flatworm","infra","lindholm","mincemeat","plugging","speakeasy"}, (21 items)
22: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","eradicate","escritoire","exorcism","fiat","filmy","flatworm","mincemeat","plugging","speakeasy"}, (22 items)
23: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","eradicate","escritoire","exorcism","infra","isis","mincemeat","moresby","mycenae","smokescreen","speakeasy"}, (23 items)
24: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","efferent","elysee","exorcism","filmy","gestapo","infra","markham","mincemeat","moresby","mycenae","plugging","smokescreen","speakeasy"}, (24 items)
25: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","eradicate","exorcism","fiat","filmy","flatworm","infra","isis","lindholm","markham","mincemeat","moresby","mycenae","plugging","speakeasy"}, (25 items)
26: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","cobol","covariate","departure","deploy","diophantine","elysee","eradicate","escritoire","exorcism","fiat","filmy","gestapo","infra","isis","markham","mincemeat","mycenae","plugging","speakeasy","vein"}, (26 items)
27: {"alliance" alliance,"archbishop","balm","bonnet","brute"...,"centipede","covariate","departure","deploy","efferent","elysee","eradicate","escritoire","exorcism","fiat","filmy","flatworm","infra","isis","lindholm","markham","mincemeat","moresby","mycenae","plugging","smokescreen","speakeasy"}, (27 items)
28: No zero-sum subsets of that length
28: No zero-sum subsets of that length
29: No zero-sum subsets of that length
29: No zero-sum subsets of that length
30: No zero-sum subsets of that length
30: No zero-sum subsets of that length
31: No zero-sum subsets of that length
31: No zero-sum subsets of that length
</pre>
===Alternative===
Using the harder set of weights from Go, and the version 1 approach of Python, (modified to omit words and
using a dictionary so that fractional weights can be accomodated).<br>
Note that fractional weights may trigger all the usual IEEE-754 issues such as 0.3+0.6-0.9 != 0, so you may need to use sprintf()'d keys with matching to_number()'s.<br>
This is significantly faster (near instant, in fact) than an "all possible combinations" approach.<br>
This is significantly faster (near instant, in fact) than an "all possible combinations" approach.
Note that new_dict(tid) has been introduced for this task in 0.8.0, which has not yet been shipped.
 
Shows the first zero-sum subset found, only.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant weights = {-61, 1, 32, 373, 311, 249, 311, 32, -92, -185, -433,
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
-402, -247, 156, 125, 249, 32, -464, -278, 218, 32,
<span style="color: #008080;">constant</span> <span style="color: #000000;">weights</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">61</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">373</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">311</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">249</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">311</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">92</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">185</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">433</span><span style="color: #0000FF;">,</span>
-123, -216, 373, -185, -402, 156, -402, -61, -31, 902 }
<span style="color: #0000FF;">-</span><span style="color: #000000;">402</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">247</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">156</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">125</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">249</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">464</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">278</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">218</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span>
 
<span style="color: #0000FF;">-</span><span style="color: #000000;">123</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">216</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">373</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">185</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">402</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">156</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">402</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">61</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">31</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">902</span> <span style="color: #0000FF;">}</span>
integer sums = new_dict()
 
for w=1 to length(weights) do
-- make a separate modifiable copy of sums, otherwise
-- it c/would mark sums[weights[w]*{2,3,etc}] as valid,
-- ie there cannot be any w in the getd_by_index(node).
integer s = new_dict(sums)
atom v = weights[w]
if getd_index(v,s)=NULL then setd(v,{w},s) end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">sums</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
sequence sk = getd_all_keys(s)
for i=1 to length(sk) do
<span style="color: #008080;">for</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer node = getd_index(sk[i],sums)
<span style="color: #000080;font-style:italic;">-- make a separate modifiable copy of sums, otherwise
if node!=NULL and getd_index(sk[i]+v,s)=0 then
-- it c/would mark sums[weights[w]*{2,3,etc}] as valid,
setd(sk[i]+v,getd_by_index(node,sums)&w,s)
-- ie there cannot be any w in the getd_by_index(node).</span>
end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #004080;">atom</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">weights</span><span style="color: #0000FF;">[</span><span style="color: #000000;">w</span><span style="color: #0000FF;">]</span>
destroy_dict(sums)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">},</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sums = s
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sk</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_all_keys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
integer node = getd_index(0,sums)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if node!=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span>
sequence s0 = getd_by_index(node,sums)
<span style="color: #008080;">if</span> <span style="color: #000000;">node</span><span style="color: #0000FF;">!=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span>
atom t = 0 -- (sanity check)
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">))&</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=1 to length(s0) do
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer si = s0[i]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
t += weights[si]
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span>
s0[i] = weights[si]
<span style="color: #000000;">sums</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
end for
printf(1,"Total %d for %s\n",{t,sprint(s0)})
<span style="color: #004080;">integer</span> <span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span>
exit
<span style="color: #008080;">if</span> <span style="color: #000000;">node</span><span style="color: #0000FF;">!=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span>
end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">))</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Total %d for %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span><span style="color: #000000;">t</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Total 0 for {-61,32,373,311,249,311,32,-92,-185,-433,-402,-247,156,125,249,32,-464,-278,218,32,-123,-216,373,-185,-402,156,-402,-61,902}
</pre>
 
=={{header|Picat}}==
Using constraint modelling.
 
<syntaxhighlight lang="picat">import cp.
 
%
% Get any solution.
%
go =>
things(Things),
subset_sum(Things, X, Z),
println(z=Z),
println(x=X),
println([Things[I] : I in 1..Things.length, X[I] == 1]),
nl.
 
%
% Solve subset problem for any length (wrapper).
%
subset_sum(Things, X, Z) =>
subset_sum(Things,_,X,Z).
 
%
% Solve for a specific length (if Len2 is nonvar).
%
subset_sum(Things, Len2, X, Z) =>
Weights = [ T[2] : T in Things],
Len = Things.length,
X = new_list(Len),
X :: 0..1,
 
Z #= sum(X),
if nonvar(Len2) then
Z #= Len2
end,
Z #> 0, % require at least one thing
sum([X[I] * Weights[I] : I in 1..Len]) #= 0,
solve($[ff], X).
 
things(Things) =>
Things = [
[alliance, -624],
[archbishop, -915],
[balm, 397],
[bonnet, 452],
[brute, 870],
[centipede, -658],
[cobol, 362],
[covariate, 590],
[departure, 952],
[deploy, 44],
[diophantine, 645],
[efferent, 54],
[elysee, -326],
[eradicate, 376],
[escritoire, 856],
[exorcism, -983],
[fiat, 170],
[filmy, -874],
[flatworm, 503],
[gestapo, 915],
[infra, -847],
[isis, -982],
[lindholm, 999],
[markham, 475],
[mincemeat, -880],
[moresby, 756],
[mycenae, 183],
[plugging, -266],
[smokescreen, 423],
[speakeasy, -745],
[vein, 813 ]
].</syntaxhighlight>
 
{{out}}
<pre>z = 8
x = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,1,1,1,1]
[[flatworm,503],[infra,-847],[lindholm,999],[mincemeat,-880],[plugging,-266],[smokescreen,423],[speakeasy,-745],[vein,813]]
</pre>
 
Some other experiments.
 
Get a solution - if possible - for a specific number of selected things.
<syntaxhighlight lang="picat">go2 =>
things(Things),
foreach(Len in 1..Things.length)
println(len=Len),
(subset_sum(Things, Len, X, _Z) ->
println([Things[I,1] : I in 1..Things.length, X[I] == 1])
;
true
),
nl
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>len = 1
 
len = 2
[archbishop,gestapo]
 
len = 3
[exorcism,fiat,vein]
 
len = 4
[exorcism,gestapo,speakeasy,vein]
 
len = 5
[fiat,infra,lindholm,smokescreen,speakeasy]
 
len = 6
[exorcism,flatworm,gestapo,isis,plugging,vein]
 
len = 7
[fiat,flatworm,infra,isis,lindholm,plugging,smokescreen]
 
len = 8
[flatworm,infra,lindholm,mincemeat,plugging,smokescreen,speakeasy,vein]
 
len = 9
[exorcism,filmy,flatworm,infra,markham,moresby,plugging,smokescreen,vein]
 
len = 10
[fiat,filmy,flatworm,gestapo,isis,markham,mincemeat,moresby,mycenae,plugging]
 
len = 11
[exorcism,fiat,flatworm,gestapo,infra,isis,lindholm,plugging,smokescreen,speakeasy,vein]
 
len = 12
[eradicate,fiat,infra,isis,lindholm,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 13
[escritoire,fiat,filmy,gestapo,infra,isis,lindholm,markham,mincemeat,moresby,plugging,smokescreen,speakeasy]
 
len = 14
[escritoire,exorcism,fiat,flatworm,infra,isis,lindholm,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 15
[efferent,elysee,escritoire,exorcism,fiat,filmy,flatworm,infra,isis,lindholm,moresby,mycenae,smokescreen,speakeasy,vein]
 
len = 16
[diophantine,elysee,eradicate,fiat,filmy,flatworm,infra,isis,lindholm,markham,mincemeat,moresby,mycenae,plugging,speakeasy,vein]
 
len = 17
[deploy,diophantine,elysee,eradicate,escritoire,exorcism,fiat,filmy,flatworm,infra,isis,lindholm,markham,mincemeat,moresby,speakeasy,vein]
 
len = 18
[deploy,diophantine,efferent,escritoire,exorcism,fiat,filmy,gestapo,infra,isis,lindholm,markham,mincemeat,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 19
[departure,deploy,diophantine,efferent,elysee,eradicate,exorcism,fiat,filmy,flatworm,infra,isis,lindholm,markham,mincemeat,mycenae,smokescreen,speakeasy,vein]
 
len = 20
[covariate,diophantine,efferent,elysee,eradicate,exorcism,fiat,filmy,flatworm,gestapo,infra,isis,markham,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 21
[cobol,deploy,diophantine,efferent,elysee,eradicate,escritoire,exorcism,fiat,filmy,flatworm,infra,isis,lindholm,markham,mincemeat,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 22
[brute,centipede,cobol,covariate,deploy,diophantine,efferent,elysee,escritoire,exorcism,fiat,filmy,flatworm,infra,isis,markham,mincemeat,moresby,plugging,smokescreen,speakeasy,vein]
 
len = 23
[bonnet,centipede,cobol,departure,deploy,diophantine,efferent,elysee,eradicate,escritoire,exorcism,fiat,filmy,infra,isis,markham,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 24
[archbishop,brute,centipede,cobol,covariate,deploy,diophantine,efferent,elysee,escritoire,exorcism,fiat,filmy,flatworm,gestapo,infra,isis,markham,mincemeat,moresby,plugging,smokescreen,speakeasy,vein]
 
len = 25
[archbishop,bonnet,centipede,cobol,departure,deploy,diophantine,efferent,elysee,eradicate,escritoire,exorcism,fiat,filmy,gestapo,infra,isis,markham,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 26
[alliance,archbishop,bonnet,brute,centipede,covariate,departure,deploy,efferent,elysee,eradicate,exorcism,fiat,filmy,flatworm,gestapo,infra,isis,lindholm,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 27
[alliance,archbishop,balm,brute,centipede,cobol,covariate,deploy,diophantine,efferent,elysee,eradicate,exorcism,fiat,filmy,flatworm,gestapo,infra,isis,lindholm,mincemeat,moresby,mycenae,plugging,smokescreen,speakeasy,vein]
 
len = 28
 
len = 29
 
len = 30
 
len = 31</pre>
 
There are in total 349167 solutions of any number of items (the empty set is not a solution in our book).
<syntaxhighlight lang="picat">go3 =>
things(Things),
Count = count_all(subset_sum(Things, _X, _Z)),
println(count=Count),
nl.
</syntaxhighlight>
 
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de *Words
(alliance . -624) (archbishop . -915) (balm . 397) (bonnet . 452)
(brute . 870) (centipede . -658) (cobol . 362) (covariate . 590)
Line 1,711 ⟶ 2,241:
(infra . -847) (isis . -982) (lindholm . 999) (markham . 475)
(mincemeat . -880) (moresby . 756) (mycenae . 183) (plugging . -266)
(smokescreen . 423) (speakeasy . -745) (vein . 813) )</langsyntaxhighlight>
Minimal brute force solution:
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l") # For 'subsets'
 
(pick
Line 1,719 ⟶ 2,249:
(find '((L) (=0 (sum cdr L)))
(subsets N *Words) ) )
(range 1 (length *Words)) )</langsyntaxhighlight>
{{Out}}
<pre>-> ((archbishop . -915) (gestapo . 915))</pre>
Line 1,725 ⟶ 2,255:
=={{header|Python}}==
===Version 1===
<langsyntaxhighlight lang="python">words = { # some values are different from example
"alliance": -624, "archbishop": -925, "balm": 397,
"bonnet": 452, "brute": 870, "centipede": -658,
Line 1,759 ⟶ 2,289:
for x in s[-neg]:
print(x, words[x])
break</langsyntaxhighlight>
{{out}}<pre>
('mycenae', 183)
Line 1,770 ⟶ 2,300:
</pre>
===Brute force===
<langsyntaxhighlight lang="python">>>> from itertools import combinations
>>>
>>> word2weight = {"alliance": -624, "archbishop": -915, "balm": 397, "bonnet": 452,
Line 1,790 ⟶ 2,320:
>>> answer
[('archbishop', -915), ('gestapo', 915)]</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,828 ⟶ 2,358:
(for ([i (sub1 len)]) (loop l len (add1 i) '() 0)))
(zero-subsets words)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,841 ⟶ 2,371:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @pairs =
alliance => -624, archbishop => -915, balm => 397, bonnet => 452,
brute => 870, centipede => -658, cobol => 362, covariate => 590,
Line 1,858 ⟶ 2,388:
default { "Length $n: (none)" }
}
}</langsyntaxhighlight>
{{out}}
<pre>Length 1: (none)
Line 1,898 ⟶ 2,428:
Added was "que pasa" informational messages. &nbsp; The sum &nbsp; (which is zero for this task) &nbsp; can be any number,
<br>and can be specifiable on the command line.
<langsyntaxhighlight lang="rexx">/*REXX program finds some non─null subsets of a weighted list whose sum eqals zero.*/
parse arg target stopAt chunkette . /*option optional arguments from the CL*/
if target=='' | target=="," then target= 0 /*Not specified? Then use the default.*/
Line 1,987 ⟶ 2,517:
call tello
call tello 'There are ' N " entries in the (above)" arg(1) 'table.'
call tello; return</langsyntaxhighlight>
Output note: &nbsp; this program also writes the displayed output to file(s): &nbsp; SUBSET.nnn
<br> ──────── where &nbsp; nnn &nbsp; is the ''chunk'' number.
Line 2,053 ⟶ 2,583:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Subset sum problem
 
Line 2,134 ⟶ 2,664:
next
return alist
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,147 ⟶ 2,677:
=={{header|Ruby}}==
a brute force solution:
<langsyntaxhighlight lang="ruby">weights = {
'alliance' =>-624, 'archbishop'=>-915, 'balm' => 397, 'bonnet' => 452,
'brute' => 870, 'centipede' =>-658, 'cobol' => 362, 'covariate'=> 590,
Line 2,169 ⟶ 2,699:
puts "no subsets of length #{n} sum to zero"
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,207 ⟶ 2,737:
=={{header|Scala}}==
{{Out}}Best seen running in your browser by [https://scastie.scala-lang.org/KnhJimmSTL6QXIGTAdZjBQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object SubsetSum extends App {
private val LIMIT = 5
private val n = items.length
Line 2,269 ⟶ 2,799:
zeroSum(0, 0)
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var pairs = Hash(
alliance => -624, archbishop => -915,
brute => 870, centipede => -658,
Line 2,304 ⟶ 2,834:
})
found || say "Length #{n}: (none)"
}</langsyntaxhighlight>
{{out}}
<pre style="height: 40ex; overflow: scroll">
Line 2,340 ⟶ 2,870:
=={{header|Tcl}}==
As it turns out that the problem space has small subsets that sum to zero, it is more efficient to enumerate subsets in order of their size rather than doing a simple combination search. This is not true of all possible input data sets though; the problem is known to be NP-complete after all.
<langsyntaxhighlight lang="tcl">proc subsetsOfSize {set size} {
if {$size <= 0} {
return
Line 2,366 ⟶ 2,896:
# Nothing was found
return -code error "no subset sums to zero"
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">set wordweights {
alliance -624
archbishop -915
Line 2,402 ⟶ 2,932:
}
set zsss [searchForSubset $wordweights]
puts "Found zero-summing subset: [join [lsort $zsss] {, }]"</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,410 ⟶ 2,940:
=={{header|Ursala}}==
This solution scans the set sequentially while maintaining a record of all distinct sums obtainable by words encountered thus far, and stops when a zero sum is found.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import int
 
Line 2,452 ⟶ 2,982:
#cast %zm
 
main = nullset weights</langsyntaxhighlight>
The name of the function that takes the weighted set is <code>nullset</code>. It manipulates a partial result represented as a list of pairs, each containing a subset of weighted words and the sum of their weights. Here is a rough translation:
* <code>=><></code> fold right combinator with the empty list as the vacuuous case
Line 2,477 ⟶ 3,007:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
class Item {
Line 2,552 ⟶ 3,082:
 
System.print("The weights of the following %(LIMIT) subsets add up to zero:\n")
zeroSum.call(0, 0)</langsyntaxhighlight>
 
{{out}}
Line 2,670 ⟶ 3,200:
isis -982
smokescreen 423
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
def \Items\ Wd, Wt;
def N = 31;
int Set(N), Items;
 
proc SubSum(I, Weight);
int I, Weight, J;
[if I#0 & Weight=0 then
[for J:= 0 to I-1 do
Print("%s%s", if J then " " else "", Items(Set(J),Wd));
Print("\n");
exit;
];
for J:= if I#0 then Set(I-1)+1 else 0 to N-1 do
[Set(I):= J;
SubSum(I+1, Weight+Items(J,Wt));
];
];
 
[Items:= [
["alliance", -624],
["archbishop", -915],
["balm", 397],
["bonnet", 452],
["brute", 870],
["centipede", -658],
["cobol", 362],
["covariate", 590],
["departure", 952],
["deploy", 44],
["diophantine", 645],
["efferent", 54],
["elysee", -326],
["eradicate", 376],
["escritoire", 856],
["exorcism", -983],
["fiat", 170],
["filmy", -874],
["flatworm", 503],
["gestapo", 915],
["infra", -847],
["isis", -982],
["lindholm", 999],
["markham", 475],
["mincemeat", -880],
["moresby", 756],
["mycenae", 183],
["plugging", -266],
["smokescreen", 423],
["speakeasy", -745],
["vein", 813] ];
SubSum(0, 0);
]</syntaxhighlight>
{{out}}
<pre>
alliance archbishop balm bonnet brute centipede cobol covariate departure deploy diophantine efferent elysee eradicate escritoire exorcism fiat filmy flatworm mincemeat plugging speakeasy
</pre>
 
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">var items=T(
T("alliance", -624), T("archbishop", -915), T("balm", 397),
T("bonnet", 452), T("brute", 870), T("centipede", -658),
Line 2,700 ⟶ 3,290:
 
set:=List.createLong(items.len(),0);
try{ subSum(set,0,0); }catch(TheEnd){}</langsyntaxhighlight>
{{out}}
<pre>
9,482

edits