Fortran Coder

查看: 13374|回复: 2
打印 上一主题 下一主题

[求助] 取e小数点后20个10位数的质数,求debug!

[复制链接]

7

帖子

2

主题

0

精华

入门

F 币
42 元
贡献
25 点

规矩勋章

跳转到指定楼层
楼主
发表于 2014-9-26 21:21:31 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
运行后发现 第一部分求e的后10000位可以得到结果
第二部分就卡住了

我觉得可能是定义或者mod的问题,因为求的是10位数的质数,用tmp(10位数组)除数得到的余数不准确附了代码
求指明方向!!!!拜托了
[Fortran] 纯文本查看 复制代码
Program jiayi

  Implicit Real *8(A, C-H, O-S, U-Z)
  Integer, Parameter :: nk = 10000
  Dimension :: ns(nk), na(nk), nt(nk), prime(20)
  Integer :: i, j, b, it, l

  Do i = 1, nk
    na(i) = 0
    ns(i) = 0
    nt(i) = 0
  End Do

  na(1) = 1
  ns(1) = 2

  n = 1
  Do j = 1, 5000
    n = n + 1

    Do i = 2, nk
      nd = mod(na(i-1), n)*10 + nt(i)
      nt(i) = nd/n
      na(i) = mod(nd, n)
    End Do

    Do i = 1, nk
      na(i) = nt(i)
      ns(i) = ns(i) + nt(i)
    End Do

    Do i = nk, 2, -1
      ns(i-1) = ns(i-1) + ns(i)/10
      ns(i) = mod(ns(i), 10)
    End Do

  End Do

  Print *, 'e=2.'
  Write (*, '(8x,77i1)')(ns(i), i=2, 77)
  Write (*, '(80i1)')(ns(i), i=78, nk)

  b = 1

loop_utmost: Do i = 2, nk - 10
    tmp = 0
    If (ns(i)/=0) Then
loop_tmp: Do j = i, i + 9 !loop 10 times to get the 10 digit number, and save it as "tmp"
        tmp = tmp*10 + ns(j)
      End Do loop_tmp

      l = sqrt(tmp+1) !choose the squareroot of the number itself as the biggest divisor to save memory

loop_check: Do it = 2, l !To check whether tmp can be devided by any numbers in the range or not
        If (mod(tmp,real(it))==0) Then
          Cycle loop_utmost

        Else If (it<=l) Then
          Cycle loop_check
        Else
          b = b + 1
          If (b<=20) Then
            prime(b) = tmp
            Cycle loop_utmost
          Else
            Exit
          End If
        End If
      End Do loop_check
    End If
  End Do loop_utmost

  Print *, 'The ten digital primes in e:'
  Write (*, '(i5,f15.1)')(prime(i), i=1, b)

End Program jiayi

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-18 09:32

Powered by Tencent X3.4

© 2013-2024 Tencent

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