C NCLFORTSTART
      REAL FUNCTION GAMMA(X)
      real x
C NCLEND

!
!-----------------------------------------------------------------------
!     Gamma function of X
!-----------------------------------------------------------------------
!
      print *,x
      X1 = X
      FAC = 1
      IF (X1 .LT. 0) THEN
         DO 10, I = 0, -INT(X1)
         FAC = FAC / X1
         X1 = X1 + 1
   10    CONTINUE
      ENDIF
!
      GAMMA = EXP(ALGAMA(X1)) * FAC
      print *,gamma
      END FUNCTION GAMMA

C NCLFORTSTART
      REAL FUNCTION ALGAMA(X)
      real x
C NCLEND
!
! EVALUATES THE NATURAL LOGARITHM OF  GAMMA(X)  FOR ALL POSITIVE X,
! ACCURATE TO 10 DECIMAL PLACES. STIRLING'S FORMULA IS USED FOR THE
! CENTRAL POLYNOMIAL PART OF THE PROCEDURE.
! ALGORITHM FROM PIKE AND HILL: CACM ALG. NO. 291 (1966)
!
      T = X
!
      IF (T .LT. 7.0) THEN
         F = 1.0
         Z = T
    1    IF (Z .LT. 7.0) THEN
            T = Z
            F = F * Z
            Z = Z + 1.0
            GO TO 1
         ENDIF
         T = T + 1.0
         F = -LOG(F)
      ELSE
         F = 0.0
      ENDIF
!
      Z = 1.0 / T**2
      ALGAMA = F + (T - 0.5) * LOG(T) - T + .918938533204673D0 +    
     $     ((( -.000595238095238D0 * Z + .000793650793651D0) * Z - 
     $     .002777777777778D0) * Z + 0.083333333333333D0) / T

      END FUNCTION ALGAMA


