      function rapfact(n)

C*************************************
C
C This little routine returns n factiorial.
C If n is negative, the result returned is
C zero - this does not represent the real
C value n!, but acts as a flag value, since
C n! for integer n<0 is infinite. Note that
C the factorial is chosen to be double precision
C rather than integer due to the very large 
C numbers involved in factorials
C
C Started:4/12/95
C
C (c) Richard Townsend 1995
C
C*************************************

C Turn off implicit typing

      implicit none

C Declare the passed variables

      double precision rapfact		! The factorial function
      integer n				! The value to get rapfact for

C Declare the in-program variables

      integer i				! Counter

C Abort if n<0 with rapfact=0

      if(n.lt.0) then
        rapfact=0.D0
        return
      endif

C Initialise rapfact

      rapfact=1.D0

C Loop over n times

      do i=1,n,1
        rapfact=rapfact*i
      enddo

C Return

      return

      end
