Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Last active March 30, 2019 10:08
Show Gist options
  • Select an option

  • Save nilforooshan/d534c4217db81d9731a12311c7f93767 to your computer and use it in GitHub Desktop.

Select an option

Save nilforooshan/d534c4217db81d9731a12311c7f93767 to your computer and use it in GitHub Desktop.
f90: Estimate the factorial of full/half-integer positive and half-integer negative values. *.exe and *.out are the Windows and Linux executables.
PROGRAM factorial
IMPLICIT NONE
! Declarations
REAL(8):: c, d, h, pi=3.1416
INTEGER(8):: f, f2, g, fact
CHARACTER(1):: exiit ! f=n! and f2=(2n)!
! Opening prints
PRINT*,
PRINT*,
PRINT*, 'This program is written by Mohammad A. Nilforooshan.'
PRINT*, 'All rights reserved.'
PRINT*, 'http://sites.google.com/site/mannprofile/'
! Ask for the input value
PRINT*,
PRINT*,
PRINT*, 'Insert the input value.'
READ*, c
! Do the half-integer checkup
d=c-AINT(c)
IF (d/=-0.5 .AND. d/=0 .AND. d/=0.5) THEN
PRINT*,
PRINT*,
PRINT*, 'The input value is not full or half integer.'
STOP
ELSE IF (c<0 .AND. d==0) THEN
PRINT*,
PRINT*,
PRINT*, 'No negative and full integer input'
STOP
ELSE IF (c==0) THEN
PRINT*,
PRINT*,
PRINT*, '0 ! = 1'
STOP
ELSE IF (c>0 .AND. d==0) THEN
g=INT(c)
f=fact(g)
h=f
GO TO 66
ELSE IF (c>0) THEN
g=INT(CEILING(c))
f=fact(g)
f2=fact(2*g)
h=f2*SQRT(pi)/REAL((4**g)*f)
GO TO 66
ELSE
g=-INT(CEILING(c))
f=fact(g)
f2=fact(2*g)
h=(-4**g)*f*SQRT(pi)/REAL(f2)
GO TO 66
END IF
! Print the output on the screen
66 PRINT*,
PRINT*,
PRINT*, g
PRINT*, f
PRINT*, f2
WRITE(*,*) c,'! =',h
PRINT*,
PRINT*,
PRINT*, 'Press any key to exit.'
READ*, exiit
IF (exiit=='a') THEN
GO TO 82
END IF
82 END PROGRAM factorial
! Introducing the factorial functionn
INTEGER(8) FUNCTION fact(n)
INTEGER(8):: a, b, i, n
fact=1
a=n
DO i=1,n/2
b=a*(a-1)
fact=fact*b
a=a-2
END DO
RETURN
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment