The Computer Language
Benchmarks Game

pidigits Hack program

source code

<?hh 
/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
   
   contributed by Isaac Gouy 
   PHP as HHVM/Hack by Isaac Gouy
   
   php -q pidigits.php 27
*/


class Transformation {
   var $q, $r, $s, $t, $k;

   function Transformation($q, $r, $s, $t){
      $this->q = $q;
      $this->r = $r;      
      $this->s = $s;
      $this->t = $t;               
   }
   
   function Unity(){
      return new Transformation("1", "0", "0", "1");              
   }   
   
   function Zero(){
      return new Transformation("0", "0", "0", "0");              
   }      
   
      
   function Compose($a){
      $qq = bcmul($this->q, $a->q);
      $qrrt = bcadd(bcmul($this->q, $a->r), bcmul($this->r, $a->t));
      $sqts = bcadd(bcmul($this->s, $a->q), bcmul($this->t, $a->s));
      $srtt = bcadd(bcmul($this->s, $a->r), bcmul($this->t, $a->t));   
      return new Transformation($qq, $qrrt, $sqts, $srtt);
   }
   
   function Extract($j){
      $bigj = strval($j);
      $qjr = bcadd(bcmul($this->q, $bigj), $this->r);
      $sjt = bcadd(bcmul($this->s, $bigj), $this->t);
      $d = bcdiv($qjr, $sjt);
      return floor($d);
   }
      
   function Next(){ 
      $this->k = $this->k + 1;
      $this->q = strval($this->k);
      $this->r = strval(4*$this->k + 2);
      $this->s = "0";
      $this->t = strval(2*$this->k + 1);
      return $this;      
   }                
}



class PiDigitStream {
   var $z, $x, $inverse;

   function PiDigitStream(){
      $this->z = Transformation::Unity();
      $this->x = Transformation::Zero();      
      $this->inverse = Transformation::Zero();   
   }
   
   function Produce($j){
      $i = $this->inverse;
      $i->q = "10";
      $i->r = strval(-10*$j);
      $i->s = "0";
      $i->t = "1";
      return $i->Compose($this->z);
   }   

   function Consume($a){
      return $this->z ->Compose($a);  
   }
   
   function Digit(){
      return $this->z ->Extract(3);  
   }  
   
   function IsSafe($j){
      return $j == ($this->z ->Extract(4));  
   }    

   function Next(){
      $y = $this->Digit();
      if ($this->IsSafe($y)){
         $this->z = $this->Produce($y);
         return $y;
      } else {
         $this->z = $this->Consume($this->x ->Next());
         return $this->Next();      
      }
   } 
}


$n = $argv[1];
$i = 0;
$length = 10;
$pidigit = new PiDigitStream;

while ($n > 0){
   if ($n < $length){
      for ($j=0; $j<$n; $j++) printf("%d",$pidigit->Next());
      for ($j=$n; $j<$length; $j++)  print " ";
      $i += $n;
   } else {
      for ($j=0; $j<$length; $j++) printf("%d",$pidigit->Next());
      $i += $length;   
   }
   print "\t:$i\n";
   $n -= $length;
}

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
HipHop VM 3.18.0 (rel)
Compiler: tags/HHVM-3.18.0-0-g9b285191feb2bb1558bb6682da135263bd2a9e60
Repo schema: cc987335f4691fa6ed98336c2378d8e71a751b22


Wed, 15 Feb 2017 03:13:47 GMT

MAKE:
/usr/bin/hh_client
No errors!
0.02s to complete and log all make actions

COMMAND LINE:
/usr/bin/hhvm  pidigits.hack 2000

UNEXPECTED OUTPUT 

1,6d0
< 
< Strict Warning: Non-static method Transformation::Unity() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 63
< 
< Strict Warning: Non-static method Transformation::Zero() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 64
< 
< Strict Warning: Non-static method Transformation::Zero() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 65

PROGRAM OUTPUT:

Strict Warning: Non-static method Transformation::Unity() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 63

Strict Warning: Non-static method Transformation::Zero() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 64

Strict Warning: Non-static method Transformation::Zero() should not be called statically in /home/dunham/benchmarksgame/bench/pidigits/pidigits.hack on line 65
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