chap05/kadai6.f90

サンプルコードのダウンロード

 1program answer
 2  implicit none
 3
 4  integer :: i, j, n
 5  logical, allocatable :: prime(:)
 6
 7  read(*, *) n
 8  allocate(prime(n))
 9
10  prime = .true.
11  do i = 2, n
12    if(.not. prime(i)) then
13      cycle
14    endif
15
16    write(*, *) 'prime number : ', i
17
18    j = 1
19    do while(i * j <= n)
20      prime(i * j) = .false.
21      j = j + 1
22    enddo
23  enddo
24
25  deallocate(prime)
26
27  stop
28endprogram answer