Jump to content

Subset sum problem: Difference between revisions

m
→‎{{header|REXX}}: optimized the combN subroutine a small bit. -- ~~~~
m (→‎{{header|REXX}}: add a comment in the header section. -- ~~~~)
m (→‎{{header|REXX}}: optimized the combN subroutine a small bit. -- ~~~~)
Line 571:
call tello 'Found' @.sols "subsets whose summed weights =" target
exit
 
/*─────────────────────────────────────tell subroutine──────────────────*/
telly: @.sols=@.sols+1; names=
Line 589 ⟶ 588:
 
tello: say arg(1);call lineout 'OUTPUT.'y,arg(1) /*write to file*/; return
 
/*─────────────────────────────────────combN subroutine─────────────────*/
combN: procedure expose @.; parse arg x,y /*X items taken Y at a time.*/
!.=0; base=x+1; bbase=base-y; ym=y-1
do i=1 for y; !.i=i; end /*i*/
 
do j=1; L=!.d; do d=2 for y-1ym; L=L !.d; end /*d*/
_=!.1; s=@._
do k=2 for y-1ym; _=!.k; s=s+@._; end /*sum the weights.*/
if s==@.0 then call telly /*element @.0 is the target sum.*/
!.y=!.y+1; if !.y==base then if .combUp(y-1ym) then leave
end
 
Line 610 ⟶ 608:
end
return 0</lang>
Output'''output''' when using the input of: <tt> 0 100 </tt>
<br>(The above arguments set the target sum to zero and limits finding the solutions to one hundred.)
<br>This output was produced with a version of the REXX program that included the weights with the name.
Line 638 ⟶ 636:
Stopped after finding 100 subsets.
</pre>
Output'''output''' when using the input of: <tt> 0 100 </tt>
<br>(The above arguments set the target sum to zero and limits finding the solutions to one hundred.)
<br>Echoing of the input data was elided.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.