SUBROUTINE COMB(N, P, L, C) COM 10 C THIS SUBROUTINE FINDS THE COMBINATION SET OF N THINGS C TAKEN P AT A TIME FOR A GIVEN LEXICOGRAPHICAL INDEX. C N - NUMBER OF THINGS IN THE SET C P - NUMBER OF THINGS IN EACH COMBINATION C L - LEXICOGRAPHICAL INDEX OF COMBINATION SOUGHT C C - OUTPUT ARRAY CONTAINING THE COMBINATION SET C THE FOLLOWING RELATIONSHIPS MUST EXIST AMONG THE INPUT C VARIABLES. L MUST BE GREATER THAN OR EQUAL TO 1 AND LESS C THAN OR EQUAL TO THE MAXIMUM LEXICOGRAPHICAL INDEX. C P MUST BE LESS THAN OR EQUAL TO N AND GREATER THAN ZERO. INTEGER N, P, L, C(P), K, R, P1, BINOM, I C SPECIAL CASE CODE IF P = 1 IF(P.EQ.1)THEN C(1) = L RETURN ENDIF C INITIALIZE LOWER BOUND INDEX AT ZERO K = 0 C LOOP TO SELECT ELEMENTS IN ASCENDING ORDER P1 = P - 1 C(1) = 0 DO 20 I=1,P1 C SET LOWER BOUND ELEMENT NUMBER FOR NEXT ELEMENT VALUE IF (I.NE.1) C(I) = C(I-1) C LOOP TO CHECK VALIDITY OF EACH ELEMENT VALUE 10 C(I) = C(I) + 1 R = BINOM(N-C(I),P-I) K = K + R IF (K.LT.L) GO TO 10 K = K - R 20 CONTINUE C(P) = C(P1) + L - K RETURN END INTEGER FUNCTION BINOM(M, N) BIN 10 C ACM ALGORITHM 160 TRANSLATED TO FORTRAN. CALCULATES THE C NUMBER OF COMBINATIONS OF M THINGS TAKEN N AT A TIME. INTEGER M, N, P, I, N1, R N1 = N P = M - N1 IF (N1.GE.P) GO TO 10 P = N1 N1 = M - P 10 R = N1 + 1 IF (P.EQ.0) R = 1 IF (P.LT.2) GO TO 30 DO 20 I=2,P R = (R*(N1+I))/I 20 CONTINUE 30 BINOM = R RETURN END