Implementing the Spigot algorithm for π (pi)

后端 未结 3 1484
终归单人心
终归单人心 2021-02-06 02:16

I\'m having a hard time understanding the Spigot algorithm for π (pi) found here at the bottom of the page.

I\'m getting lost at the bottom of part 2 \"Put A

3条回答
  •  轮回少年
    2021-02-06 02:46

    // Spigot program for pi to NDIGITS decimals.
    // 4 digits per loop.
    // Thanks to Dik T. Winter and Achim Flammenkamp who originally made a compressed version of this. 
    
    #include "stdafx.h"
    #include 
    #include 
    
    #define NDIGITS 15536          //max digits to compute
    #define LEN (NDIGITS/4+1)*14   //nec. array length
    
    long a[LEN];                   //array of 4-digit-decimals
    long b;                        //nominator prev. base
    long c = LEN;                  //index
    long d;                        //accumulator and carry
    long e = 0;                    //save previous 4 digits
    long f = 10000;                //new base, 4 decimal digits
    long g;                        //denom previous base
    long h = 0;                    //init switch
    
    int main(void) {
            for(; (b=c-=14) > 0 ;){    //outer loop: 4 digits per loop
                    for(; --b > 0 ;){      //inner loop: radix conv
                            d *= b;            //acc *= nom prev base
                            if( h == 0 )
                                    d += 2000*f;   //first outer loop
                            else
                                    d += a[b]*f;   //non-first outer loop
                            g=b+b-1;           //denom prev base
                            a[b] = d % g;
                            d /= g;            //save carry
                    }
                    h = printf("%d",e+d/f);//print prev 4 digits
                    d = e = d % f;         //save currently 4 digits
                                           //assure a small enough d
            }
            getchar();
            return 0;
    }
    

提交回复
热议问题