【发布时间】:2011-05-06 07:54:33
【问题描述】:
我只是想稍微熟悉一下 Fortran(因为我可以),所以我编写了这个使用 Eratosthenes 筛生成素数的小程序。这是程序:
program prime
implicit none
integer num_primes, at, found, i
logical is_prime
integer, allocatable, dimension(:) :: primes ! array that will hold the primes
print *, "How many primes would you like to find?"
read (*, *) num_primes
allocate (primes(num_primes))
primes(1) = 2
at = 2
found = 1
do
is_prime = .true. ! assume prime
do i = 1, found
if (modulo(at, primes(i)) == 0) then ! if divisible by any other element
is_prime = .false. ! in the array, then not prime.
at = at + 1
continue
end if
end do
found = found + 1
primes(found) = at
print *, at
at = at + 1
if (found == num_primes) then ! stop when all primes are found
exit
endif
end do
结束程序主要
运行此程序将显示错误是什么,例如,尝试找到 10 个素数将产生以下数字:3、5、7、11、13、16、17、19、23。显然 16 不是素数。有什么问题?
【问题讨论】: