!! Barlow and Barnett, Computing for Scientists 1998
!! page 185                 C++ and Fortran 90

! A program to calculate and display prime numbers.

PROGRAM primes_fort

!! note changed values for ktest, nprimes and inner DO LOOP index

INTEGER :: i , j , ktest = 2 ,  nprimes = 1       ! start with 2nd prime = 3
INTEGER :: nrows , ntodo , primes(1 : 201) = 2    ! initialise array
LOGICAL :: prime

PRINT * , ' How many rows of primes (1-20)?'
READ  * , nrows
ntodo = 10*nrows
IF(ntodo > 200) STOP 'too many numbers for array size'
outer:    DO i=1 , ntodo
             prime = .FALSE.
middle:      DO WHILE( .NOT. prime)
                ktest = ktest+1
inner:          DO j = 1 ,  nprimes
                   IF(MOD(ktest , primes(j)) == 0) cycle middle
                ENDDO inner
                prime = .TRUE.
             ENDDO middle
             nprimes = nprimes+1
             primes(nprimes) = ktest
          ENDDO outer          
     PRINT 100
100       FORMAT(1X , 57('='))
     PRINT '(10I5 , I8)' , ((primes(10*(i-1)+j) , j=1,10) , 10*i , i=1,nrows)  
     PRINT 100

     STOP ' formatted primes all done'
END  PROGRAM primes_fort
