The Computer Language
Benchmarks Game

pidigits Node.js #2 program

source code

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by Zach Kelling
// modified by Roman Pletnev

var bignum = require('bignum');

function pad(i, last) {
  var res = i.toString(), count;
  if (count = 10 - res.length) {
    while (count--) last ? res += ' ' : res = 0 + res;
  }
  return res;
}

function calculatePi(N) {
  var i = 0,
      k = 0,
      k1 = 1,
      ns = 0,

      a = bignum(0),
      d = bignum(1),
      m = bignum(0),
      n = bignum(1),
      t = bignum(0),
      u = bignum(0);

  while (1) {
    k += 1;
    k1 += 2;
    t = n.shiftLeft(1);
    n = n.mul(k);
    a = a.add(t).mul(k1);
    d = d.mul(k1);

    if (a.cmp(n) >= 0) {
      m = n.mul(3).add(a);


      t = m.div(d);
      u = m.mod(d).add(n);

      if (d.cmp(u) > 0) {
        ns = ns * 10 + t.toNumber();
        i += 1;

        var last = i >= N;
        if (i % 10 === 0 || last) {
          console.log(pad(ns, last) + '\t:' + i);
          ns = 0;
        }

        if (last) break;

        a = a.sub(d.mul(t)).mul(10);
        n = n.mul(10);
      }
    }
  }
}

// Uses only one bigint division instead of two when checking a produced digit's validity.
// calculatePi(process.argv[2] || 10);

    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
v6.9.0


Wed, 19 Oct 2016 18:55:24 GMT

MAKE:
cp -L pidigits.node-2.node pidigits.node-2.js
0.01s to complete and log all make actions

COMMAND LINE:
/usr/local/src/node-v6.9.0-linux-x86/bin/node pidigits.node-2.js 2000

UNEXPECTED OUTPUT 

0a1,200
> 3141592653	:10
> 5897932384	:20
> 6264338327	:30
> 9502884197	:40
> 1693993751	:50
> 0582097494	:60
> 4592307816	:70
> 4062862089	:80
> 9862803482	:90
> 5342117067	:100
> 9821480865	:110
> 1328230664	:120
> 7093844609	:130
> 5505822317	:140
> 2535940812	:150
> 8481117450	:160
> 2841027019	:170
> 3852110555	:180
> 9644622948	:190
> 9549303819	:200
> 6442881097	:210
> 5665933446	:220
> 1284756482	:230
> 3378678316	:240
> 5271201909	:250
> 1456485669	:260
> 2346034861	:270
> 0454326648	:280
> 2133936072	:290
> 6024914127	:300
> 3724587006	:310
> 6063155881	:320
> 7488152092	:330
> 0962829254	:340
> 0917153643	:350
> 6789259036	:360
> 0011330530	:370
> 5488204665	:380
> 2138414695	:390
> 1941511609	:400
> 4330572703	:410
> 6575959195	:420
> 3092186117	:430
> 3819326117	:440
> 9310511854	:450
> 8074462379	:460
> 9627495673	:470
> 5188575272	:480
> 4891227938	:490
> 1830119491	:500
> 2983367336	:510
> 2440656643	:520
> 0860213949	:530
> 4639522473	:540
> 7190702179	:550
> 8609437027	:560
> 7053921717	:570
> 6293176752	:580
> 3846748184	:590
> 6766940513	:600
> 2000568127	:610
> 1452635608	:620
> 2778577134	:630
> 2757789609	:640
> 1736371787	:650
> 2146844090	:660
> 1224953430	:670
> 1465495853	:680
> 7105079227	:690
> 9689258923	:700
> 5420199561	:710
> 1212902196	:720
> 0864034418	:730
> 1598136297	:740
> 7477130996	:750
> 0518707211	:760
> 3499999983	:770
> 7297804995	:780
> 1059731732	:790
> 8160963185	:800
> 9502445945	:810
> 5346908302	:820
> 6425223082	:830
> 5334468503	:840
> 5261931188	:850
> 1710100031	:860
> 3783875288	:870
> 6587533208	:880
> 3814206171	:890
> 7766914730	:900
> 3598253490	:910
> 4287554687	:920
> 3115956286	:930
> 3882353787	:940
> 5937519577	:950
> 8185778053	:960
> 2171226806	:970
> 6130019278	:980
> 7661119590	:990
> 9216420198	:1000
> 9380952572	:1010
> 0106548586	:1020
> 3278865936	:1030
> 1533818279	:1040
> 6823030195	:1050
> 2035301852	:1060
> 9689957736	:1070
> 2259941389	:1080
> 1249721775	:1090
> 2834791315	:1100
> 1557485724	:1110
> 2454150695	:1120
> 9508295331	:1130
> 1686172785	:1140
> 5889075098	:1150
> 3817546374	:1160
> 6493931925	:1170
> 5060400927	:1180
> 7016711390	:1190
> 0984882401	:1200
> 2858361603	:1210
> 5637076601	:1220
> 0471018194	:1230
> 2955596198	:1240
> 9467678374	:1250
> 4944825537	:1260
> 9774726847	:1270
> 1040475346	:1280
> 4620804668	:1290
> 4259069491	:1300
> 2933136770	:1310
> 2898915210	:1320
> 4752162056	:1330
> 9660240580	:1340
> 3815019351	:1350
> 1253382430	:1360
> 0355876402	:1370
> 4749647326	:1380
> 3914199272	:1390
> 6042699227	:1400
> 9678235478	:1410
> 1636009341	:1420
> 7216412199	:1430
> 2458631503	:1440
> 0286182974	:1450
> 5557067498	:1460
> 3850549458	:1470
> 8586926995	:1480
> 6909272107	:1490
> 9750930295	:1500
> 5321165344	:1510
> 9872027559	:1520
> 6023648066	:1530
> 5499119881	:1540
> 8347977535	:1550
> 6636980742	:1560
> 6542527862	:1570
> 5518184175	:1580
> 7467289097	:1590
> 7772793800	:1600
> 0816470600	:1610
> 1614524919	:1620
> 2173217214	:1630
> 7723501414	:1640
> 4197356854	:1650
> 8161361157	:1660
> 3525521334	:1670
> 7574184946	:1680
> 8438523323	:1690
> 9073941433	:1700
> 3454776241	:1710
> 6862518983	:1720
> 5694855620	:1730
> 9921922218	:1740
> 4272550254	:1750
> 2568876717	:1760
> 9049460165	:1770
> 3466804988	:1780
> 6272327917	:1790
> 8608578438	:1800
> 3827967976	:1810
> 6814541009	:1820
> 5388378636	:1830
> 0950680064	:1840
> 2251252051	:1850
> 1739298489	:1860
> 6084128488	:1870
> 6269456042	:1880
> 4196528502	:1890
> 2210661186	:1900
> 3067442786	:1910
> 2203919494	:1920
> 5047123713	:1930
> 7869609563	:1940
> 6437191728	:1950
> 7467764657	:1960
> 5739624138	:1970
> 9086583264	:1980
> 5995813390	:1990
> 4780275900	:2000

PROGRAM OUTPUT: