How does this pi-generating algorithm work?
===

I used this algorithm for a program but I'm not quite sure how it works.  I would assume its an implementation of an infinite series approximation. Here is the code in C. The function takes a buffer and a number of digits to return:

        #ifndef _PI_H
        #define _PI_H
        #define SCALE 10000
        #define ARRINIT 2000
        #ifdef GFP_KERNEL
        #define MALLOC(s) kmalloc(s, GFP_KERNEL)
        #define FREE(s) kfree(s)
        #else
        #define MALLOC(s) malloc(s)
        #define FREE(s) free(s)
        #endif

        void pi(char *buffer, int m)
        {
            int i, j;
            int carry = 0;
            //it seems we need 14 'iterations' to get 4 digits correctly.
            int max = (m/4 ) * 14;
            int *arr = (int *)MALLOC(sizeof(int)*(max+15));
            strcpy(buffer,"");

    //Here there be dragons...
    //I have no clue how this algorithm works
    for (i = 0; i <= max; ++i)
        arr[i] = ARRINIT;
    for (i = max; i>0; i -= 14)
    {
     	int sum = 0;
        char temp[5];

        for (j = i; j > 0; --j)
        {
            sum = sum*j + SCALE*arr[j];
            arr[j] = sum % (j*2-1);
            sum /= (j*2-1);
        }
        //we seem to generate 4 digits at a time.
        sprintf(temp, "%04d", carry + sum/SCALE);
        strcat(buffer, temp);        carry = sum % SCALE;
    }
    FREE(arr);
}

#endif


        