!-----  Ex8_10: Partition of Integer   -----
      INTEGER :: n, i, kind, id
      INTEGER, ALLOCATABLE :: parts(:)
      DO
        PRINT*, " Select Case(0): N into arbitrary positive integers"
        PRINT*, "        Case(1): N into distinct positive integers"
        PRINT*, "        Default: Exit"
        WRITE(*,'(A)', ADVANCE='no') "Case? "
        READ *, id;  IF(id<0 .OR. id>1) EXIT
        WRITE(*,'(A)', ADVANCE='no') "  n ? ";  READ*, n
        ALLOCATE( parts(n) ); i = 0;  kind = 0;  parts = 0;  PRINT*
        CALL Partition(n, n)
        DEALLOCATE(parts)
        PRINT "(' Problem (', I1,') for n =', I3 /)", id, n
      END DO
!
   CONTAINS
      RECURSIVE SUBROUTINE Partition(p, px)
         INTEGER, INTENT(IN) :: p, px
         INTEGER :: k, ist
         IF(p == 0) THEN
            kind = kind + 1;
            PRINT "( I6,') ', 80I3)", kind, parts(1:i)
         ELSE
            DO k = px, 1, -1
               ist = i;  i = i + 1;  parts(i) = k
               CALL Partition( p-k, min(p-k, k-id) )
               i = ist
            END DO
         END IF
      END SUBROUTINE Partition
   END