Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this code calculate pi with high precision? [closed]

Tags:

c

precision

pi

Here is the code:

#include <stdio.h>

long f[2801];

int main()
{
    long i = 0, c = 2800, d = 0, e = 0, g = 0;
    for (i = 0; i < c; ++i)
        f[i] = 2000;
    for (;;) {
        d = 0; 
        g = c * 2;
        if (!g)
            break;
        i = c;
        for(;;) {
            d += f[i] * 10000;
            --g;
            f[i] = d % g;
            d /= g;
            --g;
            --i;
            if (!i) break;
            d *= i;
        }
        printf("%.4ld",e+d/10000);
        e = d % 10000;
        c -= 14;
    }
    return 0;
}

My question is: How does this code calculate pi with high decimal precision and what is the math formula it uses?

like image 538
pjpj Avatar asked Oct 18 '22 01:10

pjpj


1 Answers

This is a formatted copy of the PI program written by Dik T. Winter of the CWI institute of Holland. Originally written in an obfuscated form, in two or three lines, there are several variations by Dik and other that output different numbers of places of PI (e.g. 800, 15,000, etc.) based on evaluation of a mathematical series.

It is a class of programs known as 'spigot algorithms', designed to output a specific number of digits. You can find out more via a Google search on Dik Winter and 'spigot algorithms'. Some example hits:

Computing Pi in C a detailed analysis of the algorithm with unanswered questions.

Pi the Number, not the Movie

like image 89
cdlane Avatar answered Oct 31 '22 19:10

cdlane