c Fortran 77 program to return c all _r_ combinations of _n_ c c converted from F95 array subscripting c c Written by Jeffrey Ryan c jeff _dot_ a _dot_ ryan _at_ gmail _dot_ com c Copyright 2007 c Distributed under the MIT-style license subroutine ncr(n,r,b,a,ret) c Usage: c c n integer number of elements to choose from c r integer number of elements chosen c b set of integer elements to choose from c a all.comb flag: 1 - show all, 0 - show next c ret return array of integers c integer n,r,b(r),a,ret(*) integer cout(r),cl,mx,w,pos c pos=1 cout = b c F95 version: ret(pos:r) = b c F77 do loop instead do 10 i=1,r ret(i) = b(i) 10 continue c cl current column c mx column maximum value cl=r mx=n 1 do 20 while(1 .eq. 1) c c if can increase column, do so if(cout(cl) .lt. mx) then w = cout(cl) do i=0,(r-cl) cout(cl+i) = w+i+1 end do cl = r mx = n pos = pos + r c F95 version: ret(pos:(pos+r-1)) = cout c F77 do loop instead j = 1 do 30 i=pos,(pos+r-1) ret(i) = cout(j) j = j + 1 30 continue if(a .eq. 0) exit c F95 version: cycle c F77 use goto statement instead goto 1 end if c if can't increase column, move left cl = cl - 1 c and decrease maximum by one mx = mx - 1 c if if(cl .eq. 0) exit 20 continue end