Subset sum problem: Difference between revisions

m
→‎{{header|REXX}}: changed a REXX variable's name to make the code easier to read. -- ~~~~
(→‎{{header|REXX}}: added the results. -- ~~~~)
m (→‎{{header|REXX}}: changed a REXX variable's name to make the code easier to read. -- ~~~~)
Line 611:
<br>out of the 31 names, take a "chunk" at a time).
<br>Showing of the timing (elapsed time) was also added, as well as "que pasa" informational messages.
<br>The sum can be anything, not just zero as specifiable on the command line.
<lang rexx>/*REXX pgm finds some non-null subsets of a weighted list whose sum = 0.*/
arg target stopAt chunkette . /*get args from the command line*/
Line 635 ⟶ 636:
call esort N /*sort the names with weights.*/
call tellZ 'sorted' /*show the sorted list. */
solutions??=0 /*number of solutions so far. */
chunkStart=1 /*default place to start. */
chunkEnd =N /* " " " end. */
Line 650 ⟶ 651:
end /*chunk*/
 
if solutions??==0 then solutions??='no' /*Englishize solutions number.*/
call tello 'Found' solutions?? "subset"s(solutions??) 'whose summed weight's(solutions??) "=" target
exit /*stick a fork in it, we done.*/
/*──────────────────────────────────combN subroutine────────────────────*/
combN: procedure expose @. #. solutions?? stopAt target; parse arg x,y; !.=0
base=x+1; bbase=base-y; ym=y-1 /*!.n are the combination digits*/
 
Line 702 ⟶ 703:
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
/*──────────────────────────────────telly subroutine────────────────────*/
telly: solutions??=solutions??+1; nameL= /*start with a "null" name list. */
 
do gi=1 for y; ggg=!.gi /*build dup array (to be sorted).*/
Line 715 ⟶ 716:
 
call tello '['y" name"s(y)']' space(nameL)
if solutions??>=stopAt &, /*see if we reached a (the) limit*/
stopAt\==0 then do
call tello 'Stopped after finding' solutions?? "subset"s(solutions??)'.'
exit /*a short-timer, we have to quit.*/
end