Determine Any Number of Digits in PI

[Total: 0    Average: 0/5]

Determine any number of digits in PI.

Head Code:

<script LANGUAGE="JavaScript">
<!-- 
//
//  arctangent series for Pi a la mode by Bohr
//
var vectorsize = 53;          // number of elements in each of four arrays 
var nCells = 10;              // number of array elements displayed

// constant
var BASE = 10000.0;           // The number base 
var TENTHOUSANDTH = 0.0001;   // avoids floating point division 
var SQ_239E4 = 571210000.0;   // = BASE x 239 squared 
var SQ_5E4 = 250000.0;        // = BASE x 5 squared
var REPAIR = 0.000005;        // roundoff correction 
var RECIPR_25 = 0.04;         // the reciprocal of 25 is 0.04 
var RECIPR_239= 1.0 / 57121;  // reciprocal of 239 squared

// arrays
var term5 = null;             // = (1/5)^(2n+1), integer array
var term239 = null;           // = (1/239)^(2n+1), integer array
var sum = null;               // = 16term5-4term239, integer array
var series = null;            // = PI

// floating or integer
var TwoNplus1 = 1.0;          // 2n+1 = 1,3,5,7,... 
var Basex2n_1 = BASE;         // = 10000*(2n+1)  
var Currdigit = 1;            // index of the series[] element being printed 
var sgn = 1;                  // = (-1)^n; takes the values -1 and 1 

// strings
var IntroString = " Pi = 3.";

function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) {
       this[i] = 0;
   }
   return this;
}
function PiSetup() {
   term5   = new MakeArray(vectorsize);
   term239 = new MakeArray(vectorsize);
   sum     = new MakeArray(vectorsize);
   series  = new MakeArray(vectorsize);

   term5[1] = 5;
   term239[1] = 239;
}
function DivideTerms() {
    var total5 = term5[1];
    var total239 = term239[1];

    // Divide the terms by 25 or 57121
    for (var i = Currdigit; i <= nCells + 2; i++) {
        term5[i] = Math.floor( RECIPR_25 * total5 + REPAIR );
        total5 = BASE * total5 - SQ_5E4 * term5[i] + term5[i+1];
        term239[i] = Math.floor( total239 * RECIPR_239 + REPAIR );
        total239 = BASE * total239 - SQ_239E4 * term239[i] + term239[i+1];
    }
}
function SubtractTerms() {
    var carry = 0;
    var total = 0;

    for (var i = nCells + 1; i > Currdigit; i--) {
        total = 16.0 * term5[i] - 4.0 * term239[i] + carry + 60000.0;
        carry = Math.floor( total * TENTHOUSANDTH + REPAIR ) - 6.0;
        sum[i] = Math.floor( total - BASE * carry - 60000.0 );
    }
}
function DivideSum() {
    var total = sum[1];
    var reciprocal = 1.0 / TwoNplus1;

    for( var i = Currdigit; i <= nCells + 2; i++) {
        sum[i] = Math.floor( total * reciprocal + REPAIR );
        total = BASE * total - Basex2n_1 * sum[i] + sum[i+1];
    }
}
function AddShowDigits(nchars,lwidth) {
    var i = 0;
    var total = 0;
    var carry = 0;
    var ccount = nchars / 4;
    var strg = "x";
    var old1 = series[Currdigit+1];
    var old2 = series[Currdigit+2];

    // Add the sum to the series 
    for (i = nCells + 1; i >= Currdigit; i--) {
        total = series[i] + sgn * sum[i] + carry + 30000.0;
        carry = Math.floor( total * TENTHOUSANDTH + REPAIR ) - 3.0;
        series[i] = Math.floor( total - BASE * carry - 30000.0 );
    } 

    // if stable, display digits
    if((old1==series[Currdigit+1]) && (old2==series[Currdigit+2])) {
        Currdigit++;
        ccount++;
        if (ccount > lwidth / 4) {
            document.write("<BR>")
            ccount = 1;
        }
        strg = (BASE + series[Currdigit]) + " ";
        document.write(strg.substring(1,5))
    }

    return (4 * ccount);
}

function Crunch(fontname,fontsize) { 
    var linelength = IntroString.length;
    var time0 = new Date();
    var linewidth = 4 * Math.floor(50.0 / fontsize)

    window.status = ".. calculating "+(4 * nCells)+" digits, please wait .."
    document.write('<UL><FONT SIZE=' + fontsize + ' FACE="' + fontname + '">')
    document.write(IntroString)

    while (Currdigit < nCells) {
        DivideTerms()
        SubtractTerms()
        DivideSum()
        linelength = AddShowDigits(linelength, linewidth)

        // Set up the next value of n 
        TwoNplus1 += 2.0;  
        Basex2n_1 = BASE * TwoNplus1;
        sgn = 0 - sgn;
    }

    var time1 = new Date();
    var elapsed = time1.getTime() - time0.getTime();
    document.write("</FONT>")
    document.write("<BR><BR><B>" + (4 * nCells) + " digits calculated in ")
    document.write((Math.floor(elapsed)/1000.0) + " seconds.</B>")
    document.write("</UL>")
}

PiSetup()

// -->
</SCRIPT>

Body Code:

<script LANGUAGE="JavaScript">
Crunch("Courier New", 4)
</SCRIPT>

Times Viewed: 4

Leave a Reply

Your email address will not be published. Required fields are marked *