如何从用户在Fortran中确定的范围内找到双素数 [英] How find a twin prime number from a range determined by the user in Fortran
问题描述
我想制作一个程序,在n到m的某个范围内找到孪生素数。这是我到目前为止:
I want to make a program that finds twin prime numbers in a certain range, from n to m. Here it is what I have so far:
program twin
implicit none
integer i, count1, n, m, count2, j, k, pri1, pri2
count1 = 0
count2 = 0
read(5,*)n
read(5,*)m
do i = 1,m
do j = n,m
if (mod(j,i) ==0) then
count1 = count1 +1
else
count1 = count1
if(count1 ==0) then
pri1 = j
do k=j,m
if (mod(k,i)==0) then
count2 = count2 +1
else
count2 = count2
if(count2 ==0) then
pri2 = k
if (pri2-pri1 == 2) then
write(*,*)j,k
end if
end if
end if
end do
end if
end if
end do
end do
end program twin
我试过n = 4和m = 8,期望得到5和7,n = 70和m = 74,想要71和73,但在两种情况下, 't什么都不返回,为什么会这样?
I tried n = 4 and m = 8, expecting to get 5 and 7, the n = 70 and m = 74, wanting 71 and 73, but in both cases it doesn't return nothing, why is that?
推荐答案
我决定用函数调用重写你的代码。重复代码时,我总是尽可能地使用函数和子程序。在这种情况下,检查整数是否为素数是一个明显的选择。
I decided to rewrite your code using a function call. I always try to use functions and subroutines as much as possible when there is repeated code. In this case, the check to see if the integer was a prime is an obvious choice.
我也减少了循环来查看 m
和 n
(我换了它们,因为我很搞笑),一旦在该数字和ñ
。
I also reduced the loops to only look at numbers between m
and n
(I swapped them round because I'm funny like that) and once a prime has been found between that number and n
.
program twin
implicit none
integer :: m, n, i, j, prime1, prime2
read(*,*)m
read(*,*)n
do i = m, n
if (is_prime(i)) then
prime1 = i
do j = i, n
if (is_prime(j)) then
prime2 = j
if (prime2-prime1 == 2) then
write(*,*)i, j
end if
end if
end do
end if
end do
contains
function is_prime(num) result(output)
implicit none
integer, intent(in) :: num
logical :: output
integer :: i
integer :: count
count = 0
if (num > 1) then
do i = 2, num-1
if (mod(num, i) == 0) then
count = count + 1
end if
end do
else
count = count + 1
end if
if (count .eq. 0) then
output = .true.
else
output = .false.
end if
end function is_prime
end program twin
原始代码有几个问题,每个循环的计数变量都没有重新初始化。一旦这个问题得到解决,检查质数时就会出现问题。迄今为止,我发现维持原有结构并仅返回真正的素数是不可能的。问题产生于 mod(j,i)
检查。当 i> j
,代码返回 j
作为素数。当所有 i
不是 j
的公因子时,它返回一个素数。
There are several issues with the original code, the count variables are not reinitialized for each loop. Once this is fixed, there are problems when checking for a prime number. I have found it impossible, thus far, to maintain the original structure and return only genuine prime numbers. Problems arise from the mod(j, i)
check. When i > j
, the code returns j
as a prime. When all i
are not common factors of j
, it returns a prime.
program twin
implicit none
integer i, count1, n, m, count2, j, k, pri1, pri2
count1 = 0
count2 = 0
pri1 = 0
pri2 = 0
read(5,*)n
read(5,*)m
do i = n, m
count1 = 0
do j = 2, i - 1
if (mod(i, j) == 0) then
count1 = count1 + 1
else
count1 = count1
end if
end do
if (count1 == 0) then
pri1 = i
do k = i, m
count2 = 0
do j = 2, k - 1
if (mod(k, j) == 0) then
count2 = count2 + 1
else
count2 = count2
end if
end do
if (count2 == 0) then
pri2 = k
if (pri2 - pri1 == 2) then
write(*,*) pri1, pri2
end if
end if
end do
end if
end do
end program twin
这篇关于如何从用户在Fortran中确定的范围内找到双素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!