
Create a program to continually calculate and output the next decimal digit of   



π


{\displaystyle \pi }

   (pi).

The program should continue forever (until it is aborted by the user) calculating and outputting each decimal digit in succession.

The output should be a decimal sequence beginning   3.14159265 ...



Note: this task is about   calculating   pi.   For information on built-in pi constants see Real constants and functions.



Related Task Arithmetic-geometric mean/Calculate Pi

var [const] BN=Import("zklBigNum"),
   one=BN(1), two=BN(2), three=BN(3), four=BN(4), seven=BN(7), ten=BN(10);
 
fcn calcPiDigits{
   reg q=BN(1), r=BN(0), t=BN(1), k=BN(1), n=BN(3), l=BN(3);
   first:=True; N:=0;
   while(True){ if((N+=1)==1000){ GarbageMan.collect(); N=0; } // take a deep breath ...
      if(four*q + r - t < n*t){
         n.print(); if(first){ print("."); first=False; }
	 nr:=(r - n*t).mul(ten);	// 10 * (r - n * t);
	 n=(three*q).add(r).mul(ten)	// ((10*(3*q + r))/t) - 10*n;
	   .div(t).sub(ten*n);
	 q.mul(ten);			// q *= 10;
	 r=nr;
      }else{
	 nr:=(two*q).add(r).mul(l);	// (2*q + r)*l;
	 nn:=(q*seven).mul(k).add(two)	// (q*(7*k + 2) + r*l)/(t*l);
	     .add(r*l).div(t*l);
	 q.mul(k);   t.mul(l);		// q*=k; t*=l;
	 l.add(two); k.add(one);	// l+=2; k++;
	 n=nn; r=nr;
      }
   }
}();