[Fortran] 纯文本查看 复制代码
module SuShu
implicit none
integer :: even(2), odd(3)
private :: isprime
Contains
! ---------------------------------------------------------------
logical function fenjie( x )
implicit none
integer,intent(in) :: x
integer :: i,j,k
fenjie = .false.
if( mod(x, 2) .eq. 0 ) then
do i = 2, x
if( .not.isprime( i ) ) cycle
do j = 2, x
if( .not.isprime( j ) ) cycle
if( (i+j - x) .eq. 0 ) then
even(1) = i
even(2) = j
fenjie = .true.
return
end if
end do
end do
else
do i = 2, x
if( .not.isprime( i ) ) cycle
do j = 2, x
if( .not.isprime( j ) ) cycle
do k = 2, x
if( .not.isprime( k ) ) cycle
if( (i+j+k - x) .eq. 0 ) then
odd(1) = i
odd(2) = j
odd(3) = k
fenjie = .true.
return
end if
end do
end do
end do
end if
end function fenjie
! ---------------------------------------------------------------
! ---------------------------------------------------------------
logical function isprime(a)
implicit none
integer,intent(in) :: a
integer :: i
if( (a .eq. 2) ) then
isprime = .true.
return
end if
do i = 2, int( sqrt( a*1. ) ) + 1
if( mod(a,i) .eq. 0) then
isprime = .false.
return
end if
end do
isprime = .true.
return
end function isprime
! ---------------------------------------------------------------
end module SuShu
program main
use SuShu
implicit none
integer :: n,i
open(11, file = "odd_number_results.txt") ! 奇数结果
open(12, file = "even_number_results.txt") ! 偶数结果
do i = 6, 50 ! 验证范围
if ( fenjie(i) ) then
if( mod(i, 2) .eq. 0 ) then
write(12,100) i," = ",even(1)," + ",even(2)
else
write(11,101) i," = ",odd(1)," + ",odd(2)," + ",odd(3)
end if
else
if( mod(i, 2) .eq. 0 ) then
write(12,*) "No. ",i," does not satisfy Goldbach's conjecture"
else
write(11,*) "No. ",i," does not satisfy Goldbach's conjecture"
end if
end if
end do
close(11)
close(12)
100 format( i3,a,i3,a,i3 )
101 format( i3,a,i3,a,i3,a,i3 )
end program main