如何从 Fortran 中用户确定的范围中找到孪生素数
How find a twin prime number from a range determined by the user in Fortran
我想编写一个程序,在从 n 到 m 的特定范围内找到孪生素数。这是我目前所拥有的:
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,但在这两种情况下都没有 return ,这是为什么呢?
我决定使用函数调用重写您的代码。当有重复代码时,我总是尽量使用函数和子程序。在这种情况下,检查整数是否为质数是显而易见的选择。
我还减少了循环以仅查看 m
和 n
之间的数字(我将它们交换过来因为我很有趣)并且一旦在该数字之间找到了素数和 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
原始代码有几个问题,计数变量没有为每个循环重新初始化。一旦这个问题得到解决,检查质数时就会出现问题。到目前为止,我发现不可能保持原来的结构和 return 只有真正的素数。 mod(j, i)
检查出现问题。当i > j
时,码returnsj
为质数。当所有i
都不是j
的公因子时,它return是一个质数。
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
我想编写一个程序,在从 n 到 m 的特定范围内找到孪生素数。这是我目前所拥有的:
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,但在这两种情况下都没有 return ,这是为什么呢?
我决定使用函数调用重写您的代码。当有重复代码时,我总是尽量使用函数和子程序。在这种情况下,检查整数是否为质数是显而易见的选择。
我还减少了循环以仅查看 m
和 n
之间的数字(我将它们交换过来因为我很有趣)并且一旦在该数字之间找到了素数和 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
原始代码有几个问题,计数变量没有为每个循环重新初始化。一旦这个问题得到解决,检查质数时就会出现问题。到目前为止,我发现不可能保持原来的结构和 return 只有真正的素数。 mod(j, i)
检查出现问题。当i > j
时,码returnsj
为质数。当所有i
都不是j
的公因子时,它return是一个质数。
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