소개

Data

HOME > RESOURCES > Data 16

prime2.f90
Dongwoo Sheen   2008.05.27   6912

! Simple program to determine if n is a prime ! http://www.nasc.snu.ac.kr/sheen ! Finds all the primes <= n. ! Also counts the number of primes <= n. program prime implicit none integer:: p, q, j, n, cnt integer, allocatable :: primes(:) write(6,*) "Type n" read(5,*) n q = 2*int(sqrt(dble(n))) allocate(primes(1:q)) write(6,*) "Type n, q=sqrt(n)", n, q write(6,*) "size of primes =", size(primes) cnt = 1 primes(1) = 2 do p = 3, n, 2 q = int(sqrt(dble(p))) do j = 1, cnt if (mod(p, primes(j)) == 0) then go to 10 end if end do ! write(6,*) p, "is a prime" cnt = cnt + 1 primes(cnt) = p 10 continue end do write(6,91) n, cnt 91 format("The number of primes <= ", 4i, " is ", 4i) write(6,*) primes(1:cnt) end program prime

Comment









List


18 2009.02.11
17 2008.06.09
16 2008.05.27
15 2008.01.14
14 2007.07.19