Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Fortran中从用户确定的范围中查找双素数_Fortran_Primes - Fatal编程技术网

如何在Fortran中从用户确定的范围中查找双素数

如何在Fortran中从用户确定的范围中查找双素数,fortran,primes,Fortran,Primes,我想做一个程序,在一定范围内,从n到m,寻找双素数。以下是我迄今为止的情况: program twin implicit none integer i, count1, n, m, count2, j, k, pri1, pri2 count1 = 0 count2 = 0 read(5,*)n

我想做一个程序,在一定范围内,从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,但在这两种情况下它都没有返回任何结果,为什么会这样?

我决定使用函数调用重写代码。当有重复的代码时,我总是尽量使用函数和子程序。在本例中,检查整数是否为素数是一个明显的选择

我还减少了循环,只查看
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
原始代码有几个问题,没有为每个循环重新初始化计数变量。一旦这个问题得到解决,在检查素数时就会出现问题。到目前为止,我发现不可能保持原来的结构,只返回真正的素数。
mod(j,i)
检查时出现问题。当
i>j
时,代码返回
j
作为素数。当所有
i
都不是
j
的公因数时,它返回一个素数

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

你得到了什么输出?为什么错了?在我看来,你似乎没有将
count1
count2
初始化为0。你为什么认为它不起作用?它做什么?除了(3,5)外,所有的孪生素数都是(6n-1,6n+1)的形式。@LíviaDantas对于这样的代码,它有助于手工写出循环。对于2和5的示例,您知道应该只输出3和5。把你的问题分解成小块也会有帮助。通过对素数进行检查,当您确信自己的小部分代码可以工作时,您可以更加确信您的大代码可以工作。这是单元测试背后的基本思想。我认为您还应该解释初始测试的错误code@Ross我添加了一个小评论。它奏效了,谢谢。但是我仍然不知道如何使用子例程和函数,你能解释一下我的代码出了什么问题吗?@LíviaDantas我添加了一个没有函数调用的版本。@muddle谢谢,我会多练习,这样这些错误就不会再发生了