nperm.f 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. PROGRAM PARTITION
  2. IMPLICIT NONE
  3. INTEGER NMAX
  4. PARAMETER (NMAX = 1000)
  5. INTEGER N, NP, PART (NMAX, NMAX)
  6. INTEGER PATTERNS(NMAX,NMAX)
  7. CHARACTER (LEN = 10) STR
  8. INTEGER IARG
  9. INTEGER I, J
  10. CHARACTER (LEN=5) STRN
  11. CALL GETARG(1,STR)
  12. READ(STR,*) N
  13. CALL AUP ( N , PART, NP)
  14. WRITE(STRN,'(I5)') N
  15. DO 1, I=1,NP
  16. 1 PRINT "(I5,A3,"//STRN//"I5)", NP-I+1, " : ",(PART(I,J), J=1,N)
  17. PRINT*
  18. WRITE (6,6) N, NP
  19. 6 FORMAT("Number of unique partitions of ", I5, " is ", I5)
  20. END
  21. SUBROUTINE AUP ( N , PART, NP )
  22. IMPLICIT NONE
  23. INTEGER NMAX
  24. PARAMETER (NMAX = 1000)
  25. INTEGER P (NMAX)
  26. INTEGER N, NP
  27. INTEGER PART(NMAX,NMAX)
  28. INTEGER K, I
  29. INTEGER R
  30. INTEGER IT
  31. NP = 0
  32. K = 1
  33. P(K) = N
  34. 1 CONTINUE
  35. NP = NP + 1
  36. DO 5, I=1,K
  37. 5 PART(NP,I) = P(I)
  38. C PRINT*, (P(I), I=1,K)
  39. R = 0
  40. 2 CONTINUE
  41. IF ( K .GE. 1 .AND. P(K).EQ.1 ) THEN
  42. R = R + P(K)
  43. K = K - 1
  44. GOTO 2
  45. ENDIF
  46. IF ( K .LT. 1 ) GOTO 10
  47. P(K) = P(K) - 1
  48. R = R + 1
  49. 3 CONTINUE
  50. IF ( R.GT.P(K) ) THEN
  51. P(K+1) = P(K)
  52. R = R - P(K)
  53. K = K + 1
  54. GOTO 3
  55. ENDIF
  56. P(K+1) = R
  57. K = K + 1
  58. C PRINT* , K, R
  59. GOTO 1
  60. 10 CONTINUE
  61. RETURN
  62. END