I found this program that calculates the value of pi somewhere on the net:

Code:

#include <stdio.h>
#define SCALE 10000
#define MAXARR 2800
#define ARRINIT 2000
main()
{
int i, j;
int carry = 0;
int arr[MAXARR+1];
for (i = 0; i <= MAXARR; ++i)
arr[i] = ARRINIT;
for (i = MAXARR; i; i -= 14) {
int sum = 0;
for (j = i; j > 0; --j) {
sum = sum*j + SCALE*arr[j];
arr[j] = sum % (j*2-1);
sum /= (j*2-1);
}
printf("%04d", carry + sum/SCALE);
carry = sum % SCALE;
}
}

Output:

Code:

31415926535897932384626433832795028841971693993751058209749445923078164062862089
98628034825342117067982148086513282306647093844609550582231725359408128481117450
28410270193852110555964462294895493038196442881097566593344612847564823378678316
52712019091456485669234603486104543266482133936072602491412737245870066063155881
74881520920962829254091715364367892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854807446237996274956735188575272
48912279381830119491298336733624406566430860213949463952247371907021798609437027
70539217176293176752384674818467669405132000568127145263560827785771342757789609
17363717872146844090122495343014654958537105079227968925892354201995611212902196
08640344181598136297747713099605187072113499999983729780499510597317328160963185

Can someone explain what's happening here and why it works?