    PROGRAM g05nafe

!      G05NAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kbf, g05naf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: n = 8, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, igen, j, k, m
!      .. Local Arrays ..
       INTEGER                         :: index(n), iseed(4)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05NAF Example Program Results'
       WRITE (nout,*)
       m = 10
!      Initialize the seed to a repeatable sequence
       iseed(1) = 1762543
       iseed(2) = 9324783
       iseed(3) = 42344
       iseed(4) = 742355
!      IGEN identifies the stream.
       igen = 1
       CALL g05kbf(igen,iseed)
       WRITE (nout,99998) m, ' Permutations of first ', n, ' integers'
       WRITE (nout,*)
       DO j = 1, m
          DO i = 1, n
             index(i) = i
          END DO
          ifail = 1
          CALL g05naf(index,n,igen,iseed,ifail)

          IF (ifail==0) THEN
             WRITE (nout,99999) (index(k),k=1,n)
          ELSE
             WRITE (nout,99997) ifail
             GO TO 20
          END IF

       END DO
20     CONTINUE

99999  FORMAT (1X,8I3)
99998  FORMAT (1X,I2,A,I1,A)
99997  FORMAT (1X,' ** G05NAF returned with IFAIL = ',I5)
    END PROGRAM g05nafe
