pi计算

计算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)

发表评论

电子邮件地址不会被公开。 必填项已用*标注