program main c*********************************************************************72 c cc PRIME counts the prime numbers between 2 and N. c c Discussion: c c We divide the calculation up into P parts. c c Part 0 checks 2+0, 2+P+0, 2+2*P+0, ... c Part 2 checks 2+1, 2+P+1, 2+2*P+1, ... c Part P-1 checks 2+P-1, 2+2*P-1, 2+3*P-1, ... c c Right now, the outer loop executes all P parts. c But it would be simple to make an MPI version, in which the c program with identifier ID only executes part ID. c c Modify this program so that it runs under MPI. c c Licensing: c c This code is distributed under the GNU LGPL license. c c Modified: c c 07 September 2013 c c Author: c c John Burkardt c implicit none integer id integer n parameter ( n = 100000 ) integer p integer total integer total_part write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PRIME' write ( *, '(a)' ) ' FORTRAN77 version' p = 4 total = 0 do id = 0, p - 1 call prime_part ( id, p, n, total_part ) total = total + total_part end do write ( *, '(a)' ) ' ' write ( *, '(a,i8,a,i8,a)' ) & ' From 2 to ', n, ' there are ', total, ' primes.' stop end subroutine prime_part ( id, p, n, total_part ) implicit none integer i integer id integer j integer n integer p logical prime integer total_part total_part = 0 do i = 2 + id, n, p prime = .true. do j = 2, i - 1 if ( mod ( i, j ) .eq. 0 ) then prime = .false. go to 10 end if end do if ( prime ) then total_part = total_part + 1 end if 10 continue end do return end