Fortran Coder

查看: 8013|回复: 1

[数理统计] 验证哥德巴赫猜想Fortran程序初探

[复制链接]

63

帖子

9

主题

0

精华

专家

超凡脱俗

F 币
474 元
贡献
237 点
发表于 2017-11-19 17:29:06 | 显示全部楼层 |阅读模式
本帖最后由 Jackdaw 于 2017-11-19 17:30 编辑

偶然兴起,写了一个代码验证哥德巴赫猜想,话不多说,上代码

[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

天下英雄出我辈,一入江湖岁月催。

鸿图霸业谈笑间,不胜人生一场醉。

2010

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1548 元
贡献
666 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2017-11-19 20:04:55 | 显示全部楼层
这代码风格就是帅~~
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-10-11 09:41

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表