Subset sum problem: Difference between revisions

m
→‎version 1: replaced version 1 with the latest copy, just to be sure. -- ~~~~
(→‎version 2: remove the implication that this is not a brute-force method)
m (→‎version 1: replaced version 1 with the latest copy, just to be sure. -- ~~~~)
Line 636:
call tello 'Found' solutions "subsets whose summed weights =" target
exit
/*─────────────────────────────────────telly subroutine─────────────────*/
 
/*─────────────────────────────────────tell subroutine──────────────────*/
telly: solutions=solutions+1; names=
 
Line 654 ⟶ 653:
 
tello: say arg(1);call lineout 'OUTPUT.'y,arg(1) /*write to file*/; return
 
/*─────────────────────────────────────combN subroutine─────────────────*/
combN: procedure expose @. #. solutions stopAt target; parse arg x,y; !.=0
parsebase=x+1; arg x, bbase=base-y; ym=y-1 /*!.n X are itemsthe taken Y at a time.combination digits*/
do n=1 for y; !.n=n; end /*builtbuild 1st combination*/
!.=0; base=x+1; bbase=base-y; /*!.n are the combination digits*/
do n=1 for y; !.n=n; end /*built 1st combination*/
 
do j=1; _=!.1; s=#._
do k=2 for y-1ym; _=!.k; s=s+#._; end end /*sum the weights.*/
if s==target then call telly
!.y=!.y+1; if !.y==base then if .combUp(ym) then leave
end /*j*/
 
Line 670 ⟶ 667:
 
.combUp: procedure expose !. y bbase; parse arg d; if d==0 then return 1
p=!.d; do u=d to y; !.u=p+1
if !.u>=bbase+u then return .combUp(u-1)
p=!.u
end /*u*/
return 0</lang>
'''output''' when using the input of: <tt> 0 100 </tt>