c-----------------------------------------------------------
c Chapter 5: Next composition of n into k parts(p50)
c-----------------------------------------------------------
c   Name of subroutine: NEXCOM
c
c   Algorithm:Generating next composition of n into k parts
c
c   input:    n=6, k=3
c   complier: f77 nexcom_2.f
c-----------------------------------------------------------

c-----   The driver program is in nexcom_2.f   -------------

c-----Subroutine begins here   -----------------------------
      subroutine nexcom(n,k,r,mtc)
      integer r(k),t
      logical mtc
      data klast,nlast/0,0/
10    if (n.eq.nlast.and.k.eq.klast) go to 60
20    nlast=n
      klast=k
      do 21  i=1,k
21    r(i)=0
      r(1)=n
30    mtc=(r(k).ne.n)
      return
60    if(.not.mtc) go to 20
70    do 71  i=1,k
      if(r(i).ne.0) go to 100
71    continue
100   t=r(i)
      r(i)=0
      r(1)=t-1
      r(i+1)=r(i+1)+1
      go to 30
      end


