Subset sum problem: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: re-did the REXX program, combined some statements, removed some blank lines. -- ~~~~)
Line 811: Line 811:
call tello 'Found' ?? "subset"s(??) 'whose summed weight's(??) "=" target
call tello 'Found' ?? "subset"s(??) 'whose summed weight's(??) "=" target
exit /*stick a fork in it, we done.*/
exit /*stick a fork in it, we done.*/

/*──────────────────────────────────COMBN subroutine────────────────────*/
/*──────────────────────────────────COMBN subroutine────────────────────*/
combN: procedure expose @. #. ?? stopAt target; parse arg x,y; !.=0
combN: procedure expose @. #. ?? stopAt target; parse arg x,y; !.=0
base=x+1; bbase=base-y; ym=y-1 /*!.n are the combination digits*/
base=x+1; bbase=base-y; ym=y-1 /*!.n are the combination digits*/

do n=1 for y; !.n=n; end /*build 1st combination*/
do n=1 for y; !.n=n; end /*build 1st combination*/


Line 836: Line 834:
end /*u*/
end /*u*/
return 0 /*go back & sum this combination.*/
return 0 /*go back & sum this combination.*/

/*──────────────────────────────────ESORT subroutine────────────────────*/
/*──────────────────────────────────ESORT subroutine────────────────────*/
esort: procedure expose #. @.; parse arg N; h=N
esort: procedure expose #. @.; parse arg N; h=N
do while h>1; h=h%2
do while h>1; h=h%2
do i=1 for N-h; j=i; k=h+i
do i=1 for N-h; j=i; k=h+i
do while #.k<#.j
do while #.k<#.j; parse value @.j @.k #.j #.k with @.k @.j #.k #.j
parse value @.j @.k #.j #.k with @.k @.j #.k #.j
if h>=j then leave; j=j-h; k=k-h
if h>=j then leave; j=j-h; k=k-h
end /*while #.k<#.j*/
end /*while #.k<#.j*/
end /*i*/
end /*i*/
end /*while h>1*/
end /*while h>1*/
return
return

/*──────────────────────────────────NSORT subroutine────────────────────*/
/*──────────────────────────────────NSORT subroutine────────────────────*/
nSort: procedure expose names.; parse arg many; h=many
nSort: procedure expose names.; parse arg many; h=many
do while h>1; h=h%2
do while h>1; h=h%2
do i=1 for many-h; j=i; k=h+i
do i=1 for many-h; j=i; k=h+i
do while names.k<names.j
do while names.k<names.j; parse value names.j names.k with names.k names.j
parse value names.j names.k with names.k names.j
if h>=j then leave; j=j-h; k=k-h
if h>=j then leave; j=j-h; k=k-h
end /*while names.k<names.j*/
end /*while names.k<names.j*/
end /*i*/
end /*i*/
end /*while h>1*/
end /*while h>1*/
return
return

/*──────────────────────────────────S subroutine────────────────────────*/
/*──────────────────────────────────S subroutine────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*plural*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*plural*/

/*──────────────────────────────────TELLO subroutine────────────────────*/
/*──────────────────────────────────TELLO subroutine────────────────────*/
tello: say arg(1); call lineout 'SUBSET.'y,arg(1) /*write to file*/; return
tello: say arg(1); call lineout 'SUBSET.'y,arg(1) /*write to file*/; return

/*──────────────────────────────────TELLY subroutine────────────────────*/
/*──────────────────────────────────TELLY subroutine────────────────────*/
telly: ??=??+1; nameL= /*start with a "null" name list. */
telly: ??=??+1; nameL= /*start with a "null" name list. */
Line 886: Line 877:
end
end
return /*go back and keep on truckin'. */
return /*go back and keep on truckin'. */

/*──────────────────────────────────TELLZ subroutine────────────────────*/
/*──────────────────────────────────TELLZ subroutine────────────────────*/
tellz: do J=1 for N /*show list of names and weights.*/
tellz: do j=1 for N /*show list of names and weights.*/
call tello right('['j']',30) right(@.j,11) right(#.j,5)
call tello right('['j']',30) right(@.j,11) right(#.j,5)
end
end /*j*/
call tello; call tello 'There are' N "entries in the (above)" arg(1) 'table.'
call tello; call tello 'There are' N "entries in the (above)" arg(1) 'table.'
call tello
call tello