FORTRAN Computer Games


Pi

The following FORTRAN 77 implementation of the Spigot algorithm calculates the digits of Pi:

$ ./pi
3.1415926535897932384626433832795028841971693993751058209749445923078164
06286208998628034825342117067...

Change the parameter NDIGIT to the number of digits of Pi you want to output. On a modern computer, the computation of more than 1 million digits may still take a couple of minutes.

Program Listing

Copy and save the program in file pi.f locally.

C     ******************************************************************
C
C     SPIGOT ALGORITHM FOR THE DIGITS OF PI.
C
C     ******************************************************************
      PROGRAM PI
      INTEGER NDIGIT, M
      PARAMETER (NDIGIT=2**14 - 1, M=(NDIGIT/4 + 1) * 14)
      INTEGER      I, J
      INTEGER      A, D, E, L
      INTEGER      B(M + 1)
      CHARACTER*14 FMT

      DATA B /M * 20, 0/

      E = 0
      L = 2

      DO 20 I = M, 14, -7
      D = 0
      A = I * 2 - 1

      DO 30 J = I, 1, -1
      D = D * J + B(J) * 100
      B(J) = MOD(D, A)
      D = D / A
      A = A - 2
   30 CONTINUE

      IF (D .EQ. 99) THEN
        E = E * 100 + D
        L = L + 2
      ELSE IF (I .EQ. M) THEN
        PRINT 100, (D / 100) / 10.0
        E = MOD(D, 100)
      ELSE
        WRITE (FMT, 200) L, L
        PRINT FMT, E + D / 100
        E = MOD(D, 100)
        L = 2
      END IF
   20 CONTINUE

  100 FORMAT (F3.1,$)
  200 FORMAT ('(I',I4.4,'.',I4.4,',$)')
      END

Build Instructions

UNIXFlang/F18$ flang -o pi pi.f
GNU Fortran$ gfortran -o pi pi.f
Intel Fortran Compiler$ ifort -o pi pi.f
Win32Digital/Compaq Visual Fortran> fl32.exe pi.f /Fe=pi.exe

References


Home