12 echo "read $count lines, summing to $sum; target $target"
13 # sum n... # set $sum, return 0 for exact, 1 for too high, 2 for too low
19 if [[ $sum -gt $target ]]; then
21 elif [[ $sum -lt $target ]]; then
26 # product n... # set $product, possibly $min
30 product
=$
((product
*i
))
32 ((product
< min
)) && min
=$product
36 # pick n prefix sum list... # choose n more items from list, add to prefix/sum
37 # output the list on success, return 1 if no output issued
40 local n=$1 p=$2 s=$3 i l r=1
42 if [[ $n == 1 ]]; then
44 ((calls % 10000)) || echo " $calls"
48 product $p $((target-s))
57 pick $((n-1)) "$l $p" $((s+l)) $* && r=0
61 # shave off early rounds
63 while { sum ${size[*]: -$tail}; [[ $? == 2 ]]; }; do
66 echo "last $tail items give sum of $sum, skipping to combinations of $tail"
67 # iterate until we find a winner
68 while ! pick $tail '' 0 ${size[*]}; do
71 echo "made $calls calls, lowest quantum in group of $tail is $min"