Yee.936 2022-11-24 17:42 采纳率: 0%
浏览 25

Fortran如何用FUNTION和subroutine编写判断素数的子程序

fortran:编写一个判断素数的外部函数子程序,在主程序中输入一个整数,调用外部函数
只能写出不含子程序的代码,实在不会改成子程序

img

  • 写回答

1条回答 默认 最新

  • 地球屋里老师 2022-11-25 10:46
    关注
    
      function isPrimeNumber(n) result(res)
      implicit none
      integer, intent(in):: n
      logical res
      integer i
      res = .true.
      do i = 2, int(sqrt(n+1.0))
        if(mod(n,i)==0) then
          res = .false.
          exit
        end if
      end do
      end function
    
      program Test
      implicit none
      integer n
      logical Lgc
      logical, external::isPrimeNumber
      read(*,*) n
      Lgc = isPrimeNumber(n)
      if(Lgc) then
        print*,'yes'
      else
        print*,'no'
      end if
      end program
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月24日