Anonymous user
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
_=!.1; s=@._
do k=2 for
if s==@.0 then call telly /*element @.0 is the target sum.*/
!.y=!.y+1; if !.y==base then if .combUp(
end
Line 610 ⟶ 608:
end
return 0</lang>
<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>
<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.
|