找回密码
 快速注册
搜索
查看: 91|回复: 2

计算$e$和$π$

[复制链接]

3147

主题

8384

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65372
QQ

显示全部楼层

hbghlyj 发表于 2022-6-29 22:51 |阅读模式
本帖最后由 hbghlyj 于 2022-8-27 18:36 编辑

A tiny program to compute e

This is a tiny C program from Xavier Gourdon to compute 9000 decimal digits of $e$ on your computer. A program of the same kind exists for $π$ and for some other constants defined by mean of hypergeometric series.
  1. #include <stdio.h>
  2. int main(){int N=9009,n=N,a[9009],x;
  3. while(--n)a[n]=1+1/n;
  4. for(;N>9;printf("%d",x))
  5. for(n=N--;--n;a[n]=x%n,x=10*a[n-1]+x/n);}
复制代码
第3行while(--n)a[n]=1+1/n;的作用是把数组a设为{0,2,1,1,1,⋯,1}
Horner's way $$\mathrm{e}=\left(\left(\left(\left((\ldots+1) \frac{1}{6}+1\right) \frac{1}{5}+1\right) \frac{1}{4}+1\right) \frac{1}{3}+1\right) \frac{1}{2}+2$$so that, if we evaluate it by starting with the last terms of series (3), only divisions by small integers will be required (the addition of 1 being of a quasi-null cost when working with a large accuracy) and fewer storage memory is needed. The algorithm, which converges to $e-1$, now looks like:
$\left\{\array{\vphantom{f}\\\vphantom{f}}\right.$u=(1+u)/n
--n
(10)
with initial values $u=1, n=K$ and (10) should be repeated until $n=1$.

3147

主题

8384

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65372
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-6-30 06:28

Tiny programs for constants computation

本帖最后由 hbghlyj 于 2022-8-27 18:36 编辑

The following tiny C code computes digits of $π$.
  1. int a=10000,b,c=8400,d,e,f[8401],g;
  2. int main(){
  3. for(;b-c;)f[b++]=a/5;
  4. for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
  5. for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}
复制代码
numbers.computation.free.fr/Constants/TinyPrograms/tinycodes.html

3147

主题

8384

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65372
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-8-28 01:30


hbghlyj 发表于 2022-6-29 23:28
The following tiny C code computes digits of .

int a=10000,b,c=8400,d,e,f[8401],g;
int main(){
for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}
它的原理是什么呢
crypto.stanford.edu

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 19:24

Powered by Discuz!

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