Return true if n
is prime accordingly division test, false if n
is composite.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=I4P), | intent(in) | :: | n | Number inquired. |
Result of the test.
pure function is_prime(n) result(is_prime_)
!---------------------------------------------------------------------------------------------------------------------------------
!< Return true if `n` is prime accordingly division test, false if `n` is composite.
!---------------------------------------------------------------------------------------------------------------------------------
integer(I4P), intent (in) :: n !< Number inquired.
logical :: is_prime_ !< Result of the test.
integer(I4P) :: i !< Counter.
!---------------------------------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------------------------------
if (n<2.or.mod(n, 2_I4P)==0_I4P.or.mod(n, 3_I4P)==0_I4P) then
is_prime_ = .false.
return
elseif (n>1.and.n<=3) then
is_prime_ = .true.
return
endif
is_prime_ = .true.
do i=3, int(sqrt(real(n, R8P)), I4P), 2
if (mod(n, i)==0) then
is_prime_ = .false.
exit
endif
enddo
!---------------------------------------------------------------------------------------------------------------------------------
endfunction is_prime