Anonymous user
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─────────────────*/
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
▲ do n=1 for y; !.n=n; end /*built 1st combination*/
do j=1; _=!.1; s=#._
do k=2 for
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>
|