社区导航

 

搜索
查看: 773|回复: 1

[讨论] 各种语言计算圆周率的程序

[复制链接]

1万

TA的帖子

14

TA的资源

版主

Rank: 6Rank: 6

测评达人

发表于 2020-4-2 10:18 | 显示全部楼层 |阅读模式

无意中看到一个介绍各种语言计算任意精度圆周率的网站,常见的编程语言都有,分享给大家。

 

image.png

https://rosettacode.org/wiki/Pi

 

 

10 PRINT CHR$(147)
20 n = 100
30 ln = int(10*n/4)
40 nd = 1
50 dim a(ln)
60 n9 = 0
70 pd = 0 :rem First predigit is a 0
80 :
90 for j = 1 to ln
100    a(j-1) = 2 :rem Start with 2s
110 next j
120 :
130 for j = 1 to n
140     q = 0
150     for i = ln to 1 step -1 :rem Work backwards
160         x = 10*a(i-1) + q*i
170         a(i-1) = x - (2*i-1)*int(x/(2*i-1)) :rem X - INT ( X / Y) * Y
180         q = int(x/(2*i - 1))
190     next i
200     a(0) = q-10*int(q/10)
210     q = int(q/10)
220     if q=9 then n9 = n9 + 1 : goto 450
240     if q<>10 then 350
250     rem q == 10
260        d = pd+1 : gosub 500
270        if n9 < 0 then 320
280           for k = 1 to n9
290              d = 0: gosub 500
300           next k
310        rem end if
320        pd = 0
330        n9 = 0
335        goto 450
340     rem q <> 10
350        d = pd: gosub 500
360        pd = q
370        if n9 = 0 then 450
380           for k = 1 to n9
390              d = 9 : gosub 500
400           next k
410           n9 = 0
450 next j
460 print mid$(str$(pd),2,1)
470 end
480 :
490 rem outputd
500 if nd=0 then print mid$(str$(d),2,1); : return
510 if d=0 then return
520 print mid$(str$(d),2,1);".";
530 nd = 0
550 return
def calcPi():
    q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
    while True:
        if 4*q+r-t < n*t:
            yield n
            nr = 10*(r-n*t)
            n  = ((10*(3*q+r))//t)-10*n
            q  *= 10
            r  = nr
        else:
            nr = (2*q+r)*l
            nn = (q*(7*k)+2+(r*l))//(t*l)
            q  *= k
            t  *= l
            l  += 2
            k += 1
            n  = nn
            r  = nr
 
import sys
pi_digits = calcPi()
i = 0
for d in pi_digits:
    sys.stdout.write(str(d))
    i += 1
    if i == 40: print(""); i = 0

 

type AnyWriteableObject={write:((textToOutput:string)=>any)};
 
function calcPi(pipe:AnyWriteableObject) {
    let q = 1n, r=0n, t=1n, k=1n, n=3n, l=3n;
    while (true) {
        if (q * 4n + r - t < n* t) {
            pipe.write(n.toString());
            let nr = (r - n * t) * 10n;
            n  = (q * 3n + r) * 10n / t - n * 10n ;
            q  = q * 10n;
            r  = nr;
        } else {
            let nr = (q * 2n + r) * l;
            let nn = (q * k * 7n + 2n + r * l) / (t * l);
            q = q * k;
            t = t * l;
            l = l + 2n;
            k = k + 1n;
            n  = nn;
            r  = nr;
        }
    }
}
 
calcPi(process.stdout);

 



回复

使用道具 举报

2371

TA的帖子

4

TA的资源

版主

Rank: 6Rank: 6

发表于 2020-4-2 20:05 | 显示全部楼层

我以前还用VB,C之类的语言编辑过各种计算,比如24点什么的,感觉编程是一种乐趣



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

关闭

站长推荐上一条 1/8 下一条

  • 论坛活动 E手掌握

    扫码关注
    EEWORLD 官方微信

  • EE福利  唾手可得

    扫码关注
    EE福利 唾手可得

Archiver|手机版|小黑屋|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2020-5-29 12:09 , Processed in 0.116047 second(s), 22 queries , Gzip On, MemCache On.

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