计算pi
c语言版
#include <stdio.h> int main() { int r[2800 + 1]; int i, k; int b, d; int c = 0; for (i = 0; i < 2800; i++) { r[i] = 2000; } for (k = 2800; k > 0; k -= 14) { d = 0; i = k; for (;;) { d += r[i] * 10000; b = 2 * i - 1; r[i] = d % b; d /= b; i--; if (i == 0) break; d *= i; } printf("%.4d", c + d / 10000); c = d % 10000; } return 0; }
gcc pi.c -o pi
用 shell 的 bc 命令计算很方便
EXAMPLES In /bin/sh, the following will assign the value of "pi" to the shell variable pi. pi=$(echo "scale=10; 4*a(1)" | bc -l)
测试记录
bash time pi=$(echo "scale=10000; 4*a(1)" | bc -l) # a is arctan, and this give pi to 10000 digits.
2022-01-13 MacBook Pro (14-inch, 2021) Apple M1 Max
real 0m50.483s
user 0m49.896s
sys 0m0.179s
2020-10-27 MacBook Pro (16-inch, 2019) 2.6 GHz 6-Core Intel Core i7
real 1m17.887s
user 1m17.080s
sys 0m0.184s
2020-11-21 Mac mini (2018) 3.2 GHz 6-Core Intel Core i7
real 1m8.796s
user 1m8.647s
sys 0m0.066s
2020-11-21 windows (WSL) centos7 AMD 锐龙5 3600X 处理器 (r5)7nm 6核12线程 3.8GHz 95W AM4接口 盒装CPU
yum install bc
real 1m16.686s
user 1m16.609s
sys 0m0.047s
2019? MacBook Pro (15-inch, 2017) 耗时 real 1m56.849s
Leave a Reply