program main !*****************************************************************************80 ! !! PRIME counts the prime numbers between 2 and N. ! ! Discussion: ! ! We divide the calculation up into P parts. ! ! Part 0 checks 2+0, 2+P+0, 2+2*P+0, ... ! Part 2 checks 2+1, 2+P+1, 2+2*P+1, ... ! Part P-1 checks 2+P-1, 2+2*P-1, 2+3*P-1, ... ! ! Right now, the outer loop executes all P parts. ! But it would be simple to make an MPI version, in which the ! program with identifier ID only executes part ID. ! ! Modify this program so that it runs under MPI. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 07 September 2013 ! ! Author: ! ! John Burkardt ! implicit none integer id integer :: n = 100000 integer p integer total integer total_part write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PRIME' write ( *, '(a)' ) ' FORTRAN90 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 ) == 0 ) then prime = .false. exit end if end do if ( prime ) then total_part = total_part + 1 end if end do return end