Jump to content

Pi: Difference between revisions

39,606 bytes added ,  3 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
(44 intermediate revisions by 22 users not shown)
Line 14:
Related Task [[Arithmetic-geometric mean/Calculate Pi]]
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">V ndigits = 0
V q = BigInt(1)
V r = BigInt(0)
V t = q
V k = q
V n = BigInt(3)
V l = n
 
V first = 1B
L ndigits < 1'000
I 4 * q + r - t < n * t
print(n, end' ‘’)
ndigits++
I ndigits % 70 == 0
print()
I first
first = 0B
print(‘.’, end' ‘’)
V nr = 10 * (r - n * t)
n = ((10 * (3 * q + r)) I/ t) - 10 * n
q *= 10
r = nr
E
V nr = (2 * q + r) * l
V nn = (q * (7 * k + 2) + r * l) I/ (t * l)
q *= k
t *= l
l += 2
k++
n = nn
r = nr</syntaxhighlight>
 
{{out}}
<pre>
3.141592653589793238462643383279502884197169399375105820974944592307816
4062862089986280348253421170679821480865132823066470938446095505822317
2535940812848111745028410270193852110555964462294895493038196442881097
5665933446128475648233786783165271201909145648566923460348610454326648
2133936072602491412737245870066063155881748815209209628292540917153643
6789259036001133053054882046652138414695194151160943305727036575959195
3092186117381932611793105118548074462379962749567351885752724891227938
1830119491298336733624406566430860213949463952247371907021798609437027
7053921717629317675238467481846766940513200056812714526356082778577134
2757789609173637178721468440901224953430146549585371050792279689258923
5420199561121290219608640344181598136297747713099605187072113499999983
7297804995105973173281609631859502445945534690830264252230825334468503
5261931188171010003137838752886587533208381420617177669147303598253490
4287554687311595628638823537875937519577818577805321712268066130019278
76611195909216420198
</pre>
 
=={{header|360 Assembly}}==
{{trans|FORTRAN}}
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* Spigot algorithm do the digits of PI 02/07/2016
PISPIG CSECT
USING PISPIG,R13 base register
Line 121 ⟶ 175:
NBUF EQU 201 number of 5 decimals
NVECT EQU 3350 nvect=ceil(nbuf*50/3)
END PISPIG</langsyntaxhighlight>
{{out}}
<pre>
Line 152 ⟶ 206:
uses same algorithm as Go solution, from http://web.comlab.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
;pi_digits.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line;
with Ada.Text_IO;
with GNU_Multiple_Precision.Big_Integers;
Line 263 ⟶ 317:
end if;
Print_Pi (N);
end Pi_Digits;</langsyntaxhighlight>
output:
<pre> 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5 1 0 5 8 2 0 9 7 4 9 4 4 5 9 2 3 0 7 8 1 6 4 0 6 2 8 6 2 0 8 9 9 8 6 2 8 0 3 4 8 2 5 3 4 2 1 1 7 0 6 7</pre>
Line 273 ⟶ 327:
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
This codes uses 33 decimals places as a test case. Performance is O(2) based on the number of decimal places required.
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
INT base := 10;
Line 323 ⟶ 377:
# OD #);
print(new line)
)</langsyntaxhighlight>
Output:
<pre>
3141592653589793238462643383279502
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">q: 1
r: 0
t: 1
k: 1
n: 3
l: 3
d: 0
 
dotWritten: false
 
while [true][
if? (n*t) > (4*q)+r-t [
d: d+1
prints n
unless dotWritten [
prints "."
dotWritten: true
d: d+1
]
 
if 0 = d%80 -> prints "\n"
nr: 10*(r - n*t)
n: ((10*(r + 3*q)) / t) - 10*n
q: q*10
r: nr
]
else [
nr: (r + 2*q) * l
nn: ((q*(2 + 7*k)) + r*l) / (t*l)
q: q*k
t: t*l
l: l+2
k: k+1
n: nn
r: nr
]
]</syntaxhighlight>
 
{{out}}
 
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208
99862803482534211706798214808651328230664709384460955058223172535940812848111745
02841027019385211055596446229489549303819644288109756659334461284756482337867831
65271201909145648566923460348610454326648213393607260249141273724587006606315588
17488152092096282925409171536436789259036001133053054882046652138414695194151160
94330572703657595919530921861173819326117931051185480744623799627495673518857527
24891227938183011949129833673362440656643086021394946395224737190702179860943702
77053921717629317675238467481846766940513200056812714526356082778577134275778960
91736371787214684409012249534301465495853710507922796892589235420199561121290219
60864034418159813629774771309960518707211349999998372978049951059731732816096318
59502445945534690830264252230825334468503526193118817101000313783875288658753320
83814206171776691473035982534904287554687311595628638823537875937519577818577805
32171226806613001927876611195909216420198938095257201065485863278865936153381827
96823030195203530185296899577362259941389124972177528347913151557485724245415069
59508295331168617278558890750983817546374649393192550604009277016711390098488240
12858361603563707660104710181942955596198946767837449448255379774726847104047534
64620804668425906949129331367702898915210475216205696602405803815019351125338243
00355876402474964732639141992726042699227967823547816360093417216412199245863150
30286182974555706749838505494588586926995690927210797509302955321165344987202755
96023648066549911988183479775356636980742654252786255181841757467289097777279380
00816470600161452491921732172147723501414419735685481613611573525521334757418494
68438523323907394143334547762416862518983569485562099219222184272550254256887671
79049460165346680498862723279178608578438382796797668145410095388378636095068006
42251252051173929848960841284886269456042419652850222106611863067442786220391949
45047123713786960956364371917287467764657573962413890865832645995813390478027590
...</pre>
 
=={{header|AutoHotkey}}==
{{libheader|MPL}}
Could be optimized with Ipp functions, but runs fast enough for me as-is. Does not work in AHKLx64.
<langsyntaxhighlight lang="autohotkey">#NoEnv
#SingleInstance, Force
SetBatchLines, -1
Line 397 ⟶ 520:
, MP_CPY(r, nr)
}
}</langsyntaxhighlight>
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight lang="basic">10 REM ADOPTED FROM COMMODORE BASIC
20 N = 100: REM N MAY BE INCREASED, BUT WILL SLOW EXECUTION
30 LN = INT(10*N/3)+16
Line 454 ⟶ 577:
550 RETURN
</syntaxhighlight>
</lang>
 
==={{header|Atari 8-bit}}===
<langsyntaxhighlight lang="basic">10 REM ADOPTED FROM COMMODORE BASIC
20 N = 100: REM N MAY BE INCREASED, BUT WILL SLOW EXECUTION
30 LN = INT(10*N/3)+16
Line 508 ⟶ 631:
530 ND = 0
550 RETURN
</syntaxhighlight>
</lang>
 
 
==={{header|BASIC256}}===
{{Trans|Pascal}} below, and originally published by Stanley Rabinowitz in [http://www.mathpropress.com/stan/bibliography/spigot.pdf].
<syntaxhighlight lang="basic256">cls
<lang BASIC256>cls
 
n =1000
Line 571 ⟶ 694:
print d;
end if
return</langsyntaxhighlight>
 
Output:
Line 577 ⟶ 700:
3.14159265358979323846264338327950288419716939937510582097494459230781...
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|Applesoft BASIC}}
{{works with|MSX_BASIC}}
{{works with|QBasic}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">100 REM adopted from Applesoft BASIC
110 n = 100 : rem n may be increased, but will slow execution
120 ln = int(10*n/3)+16
130 nd = 1
140 dim a(ln)
150 n9 = 0
160 pd = 0 : rem First pre-digit is a 0
170 rem
180 for j = 1 to ln
190 a(j-1) = 2 : rem Start wirh 2S
200 next j
210 rem
220 for j = 1 to n
230 q = 0
240 for i = ln to 1 step -1 : rem Work backwards
250 x = 10*a(i-1)+q*i
260 a(i-1) = x-(2*i-1)*int(x/(2*i-1)) : rem X - Int ( X / Y) * Y
270 q = int(x/(2*i-1))
280 next i
290 a(0) = q-10*int(q/10)
300 q = int(q/10)
310 if q = 9 then n9 = n9+1 : goto 510
320 if q <> 10 then goto 440
330 rem Q == 10
340 d = pd+1 : gosub 560
350 if n9 <= 0 then goto 400
360 for k = 1 to n9
370 d = 0 : gosub 560
380 next k
390 rem End If
400 pd = 0
410 n9 = 0
420 goto 510
430 rem Q <> 10
440 d = pd : gosub 560
450 pd = q
460 if n9 = 0 then goto 510
470 for k = 1 to n9
480 d = 9 : gosub 560
490 next k
500 n9 = 0
510 next j
520 print str$(pd)
530 end
540 rem
550 rem Output digits
560 if nd = 0 then print str$(d); : return
570 if d = 0 then return
580 print str$(d);".";
590 nd = 0
600 return</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
This works with Commodore Basic V2
 
<langsyntaxhighlight lang="basic">10 PRINT CHR$(147)
20 N = 100: REM N MAY BE INCREASED, BUT WILL SLOW EXECUTION
30 LN = INT(10*N/3)+16
Line 623 ⟶ 804:
410 N9 = 0
450 NEXT J
460 PRINT RIGHT$(STR$(PD),1)
470 END
480 REM
490 REM OUTPUT DIGITS
500 IF ND=0 THEN PRINT RIGHT$(STR$(D),1);: RETURN
510 IF D=0 THEN RETURN
520 PRINT RIGHT$(STR$(D),1);".";
530 ND = 0
550 RETURN
</syntaxhighlight>
</lang>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|Integer Basic}}===
 
Integer version was derived from the Pascal_spigot without any optimisation. It is more than 33% faster than the Applesoft version since it runs natively with integers.
 
<langsyntaxhighlight lang="basic"> 10 REM PI CALCULATION WITH SPIGOT
100 N=100: REM MAX N=260 TO AVOID OVERFLOW
110 LEN=(10*N)/3
Line 683 ⟶ 867:
1080 IF I>0 THEN GOTO 1040
1090 RETURN
</syntaxhighlight>
</lang>
 
==={{header|Osborne 1 MBASICIS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "PI.bas"
110 LET N=100 ! Nuber of digits
120 LET LN=INT(10*N/3)+16
130 DIM A(LN)
140 LET PD,N9=0:LET ND=1
150 FOR J=1 TO LN
160 LET A(J-1)=2
170 NEXT
180 FOR J=1 TO N
190 LET Q=0
200 FOR I=LN TO 1 STEP-1
210 LET X=10*A(I-1)+Q*I
220 LET A(I-1)=X-(2*I-1)*INT(X/(2*I-1))
230 LET Q=INT(X/(2*I-1))
240 NEXT
250 LET A(0)=Q-10*INT(Q/10)
260 LET Q=INT(Q/10)
270 SELECT CASE Q
280 CASE 9
290 LET N9=N9+1
300 CASE 10
310 LET D=PD+1:CALL WRITE
320 IF N9>0 THEN
330 FOR K=1 TO N9
340 LET D=0:CALL WRITE
350 NEXT
360 END IF
370 LET PD,N9=0
380 CASE ELSE
390 LET D=PD:CALL WRITE
400 LET PD=Q
410 IF N9<>0 THEN
420 FOR K=1 TO N9
430 LET D=9:CALL WRITE
440 NEXT
450 LET N9=0
460 END IF
470 END SELECT
480 NEXT
490 PRINT STR$(PD)(1)
500 END
510 DEF WRITE
520 IF ND=0 THEN
530 PRINT STR$(D)(1);
540 ELSE IF D<>0 THEN
550 PRINT STR$(D)(1);".";
560 LET ND=0
570 END IF
580 END DEF</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|Osborne 1 MBASIC}}===
Osborne 1 program is slightly different to allow it to keep the numbers all on the main screen rather than scrolling off to the right...
 
<langsyntaxhighlight lang="basic">10 REM ADOPTED FROM COMMODORE BASIC
15 CR=0
20 N = 100: REM N MAY BE INCREASED, BUT WILL SLOW EXECUTION
Line 742 ⟶ 979:
530 ND = 0
550 RETURN
</syntaxhighlight>
</lang>
 
==={{header|TRS-80 Model 4 BASIC}}===
 
<langsyntaxhighlight lang="basic"> 10 REM ADOPTED FROM COMMODORE BASIC
20 N = 100: REM N MAY BE INCREASED, BUT WILL SLOW EXECUTION
30 LN = INT(10*N/3)+16
Line 797 ⟶ 1,034:
530 ND = 0
550 RETURN
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
===BASIC version===
<langsyntaxhighlight lang="bbcbasic"> WIDTH 80
M% = (HIMEM-END-1000) / 4
DIM B%(M%)
Line 823 ⟶ 1,060:
E% = D% MOD 100 : L% = 2
ENDCASE
NEXT</langsyntaxhighlight>
 
===Assembler version===
{{works with|BBC BASIC for Windows}}
The first 250,000 digits output have been verified.
<langsyntaxhighlight lang="bbcbasic"> DIM P% 32
[OPT 2 :.pidig mov ebp,eax :.pi1 imul edx,ecx : mov eax,[ebx+ecx*4]
imul eax,100 : add eax,edx : cdq : div ebp : mov [ebx+ecx*4],edx
Line 850 ⟶ 1,087:
E% = D% MOD 100 : L% = 2
ENDCASE
NEXT</langsyntaxhighlight>
'''Output:'''
<pre>
Line 873 ⟶ 1,110:
{{works with|GNU bc}}
{{works with|OpenBSD bc}}
<langsyntaxhighlight lang="bc">#!/usr/bin/bc -l
 
scaleinc= 20
Line 902 ⟶ 1,139:
scale= wantscale
oldpi= pi / 1
}</langsyntaxhighlight>
Output:
<pre>
Line 931 ⟶ 1,168:
=={{header|Bracmat}}==
{{trans|Icon_and_Unicon}}
<langsyntaxhighlight lang="bracmat"> ( pi
= f,q r t k n l,first
. !arg:((=?f),?q,?r,?t,?k,?n,?l)
Line 959 ⟶ 1,196:
)
)
& pi$((=.put$!arg),1,0,1,1,3,3)</langsyntaxhighlight>
Output:
<pre>3.1415926535897932384626433832795028841971693993751058209749445923078164062
Line 1,028 ⟶ 1,265:
 
Using Machin's formula. The "continuous printing" part is silly: the algorithm really calls for a preset number of digits, so the program repeatedly calculates Pi digits with increasing length and chop off leading digits already displayed. But it's still faster than the unbounded Spigot method by an order of magnitude, at least for the first 100k digits.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
Line 1,095 ⟶ 1,332:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{libheader|System.Numerics}}
'''Translation of:''' Java
{{trans|Java}}
 
<langsyntaxhighlight lang="csharp">using System;
using System.Numerics;
 
Line 1,149 ⟶ 1,387:
}
}
}</langsyntaxhighlight>
 
Adopted Version:
{{libheader|System.Numerics}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,222 ⟶ 1,461:
}
}
}</langsyntaxhighlight>
{{out}}
Output:
<langpre>Digits of PI
=========================================================================
Decimal : 141592653589793238462643383279502884197169399375105820974944
Line 1,238 ⟶ 1,477:
Dwords : 243f6a88 85a308d3 13198a2e 03707344 a4093822 299f31d0
=========================================================================
* press any key to exit *</langpre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
 
using namespace boost::multiprecision;
 
class Gospers
{
cpp_int q, r, t, i, n;
 
public:
 
// use Gibbons spigot algorith based on the Gospers series
Gospers() : q{1}, r{0}, t{1}, i{1}
{
++*this; // move to the first digit
}
 
// the ++ prefix operator will move to the next digit
Gospers& operator++()
{
n = (q*(27*i-12)+5*r) / (5*t);
 
while(n != (q*(675*i-216)+125*r)/(125*t))
{
r = 3*(3*i+1)*(3*i+2)*((5*i-2)*q+r);
q = i*(2*i-1)*q;
t = 3*(3*i+1)*(3*i+2)*t;
i++;
 
n = (q*(27*i-12)+5*r) / (5*t);
}
 
q = 10*q;
r = 10*r-10*n*t;
 
return *this;
}
 
// the dereference operator will give the current digit
int operator*()
{
return (int)n;
}
};
 
int main()
{
Gospers g;
 
std::cout << *g << "."; // print the first digit and the decimal point
 
for(;;) // run forever
{
std::cout << *++g; // increment to the next digit and print
}
}</syntaxhighlight>
{{out}}
<pre>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862 . . .
</pre>
 
=={{header|Clojure}}==
{{Trans|Python}}
<langsyntaxhighlight lang="lisp">(ns pidigits
(:gen-class))
 
Line 1,280 ⟶ 1,581:
(println))
(print q))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,291 ⟶ 1,592:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun pi-spigot ()
(labels
((g (q r t1 k n l)
Line 1,315 ⟶ 1,616:
(* t1 l)))
(+ l 2))))))
(g 1 0 1 1 3 3)))</langsyntaxhighlight>
{{out}}
<pre>CL-USER> (pi-spigot)
Line 1,322 ⟶ 1,623:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require "big"
 
def pi
Line 1,352 ⟶ 1,653:
 
pi { |digit_or_dot| print digit_or_dot; STDOUT.flush }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,359 ⟶ 1,660:
=={{header|D}}==
This modified [[wp:Spigot_algorithm|Spigot algorithm]] does not continue infinitely, because its required memory grow as the number of digits need to print.
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.string;
 
struct PiDigits {
Line 1,396 ⟶ 1,697:
foreach (d; PiDigits(100))
writeln(d);
}</langsyntaxhighlight>
{{out}}
Output:
<pre>314159265
358979323
Line 1,410 ⟶ 1,711:
534211706</pre>
===Alternative version===
<langsyntaxhighlight lang="d">import std.stdio, std.bigint;
 
void main() {
Line 1,442 ⟶ 1,743:
}
}
}</langsyntaxhighlight>
{{out}}
Output:
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816
4062862089986280348253421170679821480865132823066470938446095505822317
Line 1,469 ⟶ 1,770:
 
introcs dot cs dot princeton dot edu slash java slash data slash pi-10million.txt
<langsyntaxhighlight lang="delphi">
unit Pi_BBC_Main;
 
Line 1,607 ⟶ 1,908:
 
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,623 ⟶ 1,924:
With only a few changes (noted in the comments), the same code can be used to
output the first 2070 digits of e.
<langsyntaxhighlight lang="edsac">
[EDSAC program, Initial Orders 2.
Calculates digits of pi by spigot algorithm.
Line 1,843 ⟶ 2,144:
E 11 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,854 ⟶ 2,155:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Pi do
def calc, do: calc(1,0,1,1,3,3,0)
Line 1,870 ⟶ 2,171:
end
 
Pi.calc</langsyntaxhighlight>
 
{{out}}
Line 1,893 ⟶ 2,194:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(pi_calculation).
-export([main/0]).
Line 1,922 ⟶ 2,223:
end
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>31415926535897932384626433832795028841971693993751
Line 1,952 ⟶ 2,253:
 
=={{header|F_Sharp|F#}}==
===Translation of Haskell===
{{trans|Haskell}}
<langsyntaxhighlight lang="fsharp">let rec g q r t k n l = seq {
if 4I*q+r-t < n*t
then
Line 1,966 ⟶ 2,268:
Seq.take 1 π |> Seq.iter (printf "%A.")
// 6 digits beginning at position 762 of π are '9'
Seq.take 767 (Seq.skip 1 π) |> Seq.iter (printf "%A")</langsyntaxhighlight>
{{out}}
<pre>3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999</pre>
<pre>3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066
 
470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831
===As an Unfold===
652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903
Haskell can probably do this as an unfold, it has not so I shall in F#
600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527
<syntaxhighlight lang="fsharp">
248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051
// Generate Pi as above using unfold. Nigel Galloway: March 15th., 2022
320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219
let π()=Seq.unfold(fun(q,r,t,k,n,l)->Some(if 4I*q+r-t < n*t then(Some(int n),((10I*q),(10I*(r-n*t)),t,k,((10I*(3I*q+r))/t-10I*n),l)) else (None,((q*k),((2I*q+r)*l),(t*l),(k+1I),((q*(7I*k+2I)+r*l)/(t*l)),(l+2I)))))(1I,0I,1I,1I,3I,3I)|>Seq.choose id
6086403441815981362977477130996051870721134999999</pre>
π()|>Seq.take 767|>Seq.iter(printf "%d")
</syntaxhighlight>
 
=={{header|Factor}}==
{{trans|Oforth}}
<langsyntaxhighlight lang="factor">USING: combinators.extras io kernel locals math prettyprint ;
IN: rosetta-code.pi
 
Line 1,998 ⟶ 2,302:
] forever ;
 
MAIN: calc-pi-digits</langsyntaxhighlight>
 
=={{header|Fortran}}==
This is a modernized version of the example Fortran programme written by S. Rabinowitz in 1991. It works in base 100000 and the key step is the initialisation of all elements of VECT to 2. The format code of I5.5 means I5 output but with all leading spaces made zero so that 66 comes out as "00066", not " 66".
 
<syntaxhighlight lang="fortran">
<lang Fortran>
program pi
implicit none
Line 2,024 ⟶ 2,328:
write (*,'(i2,"."/(1x,10i5.5))') buffer
end program pi
</syntaxhighlight>
</lang>
The output is accumulated in BUFFER then written in one go at the end, but it could be written as successive values as each is calculated without much extra nitpickery: instead of <code>BUFFER(N) = MORE + K</code> for example just <code>WRITE (*,"(I5.5)") MORE + K</code> and no need for array BUFFER.
<pre>
Line 2,052 ⟶ 2,356:
This is an alternate version using an unbounded spigot. Higher precision is accomplished by using the Fortran Multiple Precision
Library, FMLIB (http://myweb.lmu.edu/dmsmith/fmlib.html), provided by Dr. David M. Smith (dsmith@lmu.edu), Mathematics Professor (Emeritus) at Loyola Marymount University. We use the default precision which is about 50 significant digits.
<syntaxhighlight lang="fortran">
<lang Fortran>
!================================================
program pi_spigot_unbounded
Line 2,101 ⟶ 2,405:
 
end program
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 05-07-2018
' compile with: fbc -s console
 
Line 2,158 ⟶ 2,462:
mpz_set(r, tmp2)
End If
Loop</langsyntaxhighlight>
{{out}}
<pre>3.
Line 2,176 ⟶ 2,480:
The code for <code>compute_pi()</code> is from [http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf]. The number of digits may be given on the command line as an argument. If there's no argument, the program will run until interrupted.
 
<langsyntaxhighlight lang="funl">def compute_pi =
def g( q, r, t, k, n, l ) =
if 4*q + r - t < n*t
Line 2,195 ⟶ 2,499:
print( d )
 
println()</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
This old-school code still works on Mac OS Monterey and is expected to work on Ventura, but it needs a modern refactoring.
<lang futurebasic>
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"
 
_maxlong = 0x7fffffff
dim as long kf, ks
 
begin globals
long kf, ks
xref mf(_maxLong - 1) as long
xref ms(_maxLong - 1) as long
dim as long cnt, n, temp, nd
dim as long col, col1
dim as long lloc, stor(50)
 
end globals
 
local mode
local fn FmtStr( nn as long, s as Str255 ) as Str255
dim l as long l
dim as Str255 f
l = s[0]
select case
case ( nn => l ) : f = string$( nn-l, 32 ) + s
case ( -nn > l ) : f = s + string$( -nn-l, 32 ) + s
case (else : -nnf >= l)s
end select
f = s + string$( -nn-l, 32 )
case else
f = s
end select
end fn = f
 
 
local mode
local fn FmtInt( nn as long, s as Str255 ) as Str255
if ( left$( s, 1 ) = " " ) then s = mid$( s, 2 )
end fn = fn FmtStr( nn, s )
 
 
local fn yprint( m as long )
if ( cnt < n )
col++
if ( col == 11 )
col = 1
col1++
long if ( col1 == 6 )
col1 = 0
print
print fn FmtInt( 4, str$( m mod 10) );
else
print fn FmtInt( 3, str$ (m mod 10) );
end if
else
print mid$( str$( m ), 2 ) ;
end if
end cnt++if
cnt++
end if
end fn
 
 
local fn xprint( m as long )
dim as long ii, wk, wk1
 
if ( m < 8 )
ii = 1
while ( ii <= lloc )
fn yprint( stor(ii) )
ii++
wend
lloc = 0
else
if ( m > 9 )
wk = m / 10
m = m mod 10
wk1 = lloc
while ( wk1 >= 1 )
wk += stor(wk1)
stor(wk1) = wk mod 10
wk = wk/10
wk1--
wend
lloc = 0
else
if ( m > 9 )
wk = m / 10
m = m mod 10
wk1 = lloc
while ( wk1 >= 1 )
wk += stor(wk1)
stor(wk1) = wk mod 10
wk = wk/10
wk1--
wend
end if
end if
lloc++
stor(lloc) = m
end fn
 
 
local mode
local fn shift( l1 as ^long, l2 as ^long, lp as long, lmod as long )
dim as long k
 
if ( l2.nil& > 0 )
k = ( l2.nil& ) / lmod
else
k = -( -l2.nil& / lmod ) - 1
end if
l2.nil& = l2.nil& - k*lmod
l1.nil& = l1.nil& + k*lp
end fn
 
 
local fn Main( nDig as long )
dim as long i
 
n = nDig
stor(0) = 0
 
mf = fn malloc( ( n + 10 ) * sizeof(long) )
if ( 0 == mf ) then stop "Out of memory"
 
ms = fn malloc( ( n + 10 ) * sizeof(long) )
if ( 0 == ms ) then stop "Out of memory"
 
print : printprintf @"Approximation of π to"; n;%ld digits", digits"n
 
cnt = 0
kf = 25
ks = 57121
mf(1) = 1
 
i = 2
while ( i <= n )
mf(i) = -16
mf(i + 1) = 16
i += 2
wend
 
i = 1
while ( i <= n )
ms(i) = -4
ms(i + 1) = 4
i += 2
wend
 
print : print " 3.";
 
while ( cnt < n )
i = 0
i++
while ( i <= n - cnt )
mf(i) = mf(i) * 10
ms(i) = ms(i) * 10
i++
wend
 
i = ( n - cnt + 1 )
i--
while ( i >= 2 )
temp = 2 * i - 1
fn shift( @mf(i - 1), @mf(i), temp - 2, temp * kf )
Line 2,347 ⟶ 2,649:
i--
wend
nd = 0
fn shift( @nd, @mf(1), 1, 5 )
fn shift( @nd, @ms(1), 1, 239 )
fn xprint( nd )
wend
print : print "Done"
fn free( ms )
fn free( mf )
end fn
 
window 1
nd = 0
 
CFTimeInterval t
fn shift( @nd, @mf(1), 1, 5 )
fn shift( @nd, @ms(1), 1, 239 )
fn xprint( nd )
 
t = fn CACurrentMediaTime
wend
// Here we specify the number of decimal places
fn Main( 4000 )
print : printf @"Compute time: %.3f ms",(fn CACurrentMediaTime-t)*1000
 
HandleEvents
print : print "Done"
</syntaxhighlight>
fn free( ms )
fn free( mf )
end fn
 
 
dim as unsigned long ticks
ticks = fn TickCount()
// Here we specify the number of decimal places
fn Main( 4000 )
ticks = fn TickCount() - ticks
print "Elapsed time:" str$( ticks ) " ticks
</lang>
 
Output:
Line 2,443 ⟶ 2,748:
4411010446 8232527162 0105265227 2111660396
Done
Elapsed time: 70729.405 ticksms
 
</pre>
Line 2,450 ⟶ 2,755:
Code below is a simplistic translation of Haskell code in [http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/spigot.pdf Unbounded Spigot Algorithms for the Digits of Pi]. This is the algorithm specified for the [http://shootout.alioth.debian.org/u64q/performance.php?test=pidigits pidigits] benchmark of the [http://shootout.alioth.debian.org/ Computer Language Benchmarks Game].
(The standard Go distribution includes [http://golang.org/test/bench/shootout/pidigits.go source] submitted to the benchmark site, and that code runs stunning faster than the code below.)
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,532 ⟶ 2,837:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
{{trans|Java}}
Solution:
<langsyntaxhighlight lang="groovy">BigInteger q = 1, r = 0, t = 1, k = 1, n = 3, l = 3
String nn
boolean first = true
Line 2,546 ⟶ 2,851:
: ['' , first, q*k , (2*q + r)*l , t*l, k + 1, (q*(7*k + 2) + r*l)/(t*l), l + 2]
print nn
}</langsyntaxhighlight>
 
Output (thru first 1000 iterations):
Line 2,553 ⟶ 2,858:
=={{header|Haskell}}==
The code from [http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf]:
<langsyntaxhighlight lang="haskell">pi_ = g (1, 0, 1, 1, 3, 3)
where
g (q, r, t, k, n, l) =
Line 2,571 ⟶ 2,876:
, k + 1
, div (q * (7 * k + 2) + r * l) (t * l)
, l + 2)</langsyntaxhighlight>
 
===Complete command-line program===
Line 2,577 ⟶ 2,882:
{{Works with|GHC|7.4.1}}
 
<langsyntaxhighlight lang="haskell">#!/usr/bin/runhaskell
 
import Control.Monad
Line 2,594 ⟶ 2,899:
main = do
hSetBuffering stdout $ BlockBuffering $ Just 80
forM_ digs putChar</langsyntaxhighlight>
 
{{out}}
Line 2,603 ⟶ 2,908:
===Quicker, Unverified Algorithm ===
Snippet verbatim from source .pdf:
<langsyntaxhighlight Haskelllang="haskell">piG3 = g(1,180,60,2) where
g(q,r,t,i) = let (u,y)=(3*(3*i+1)*(3*i+2),div(q*(27*i-12)+5*r)(5*t))
in y : g(10*q*i*(2*i-1),10*u*(q*(5*i-2)+r-y*t),t*u,i+1)</langsyntaxhighlight>
This is more efficient because each term converges in less than one step, so no checking needs to be done partway through the iteration. Only caveat is that the convergence is ''on average'' slightly over one digit, so there is a chance that, if one checked enough digits, one may find a gap where a digit would be incorrect. Though it seems to be OK for the first 100k digits, or so.
 
Line 2,612 ⟶ 2,917:
=={{header|Icon}} and {{header|Unicon}}==
{{Trans|PicoLisp}} based on Jeremy Gibbons' Haskell solution.
<langsyntaxhighlight lang="icon">procedure pi (q, r, t, k, n, l)
first := "yes"
repeat { # infinite loop
Line 2,639 ⟶ 2,944:
procedure main ()
every (writes (pi (1,0,1,1,3,3)))
end</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">pi=: 3 :0
smoutputecho"0 '3.1'
ni=. 0 while.n=.n+1 do.
while. i=. i + 1 do.
smoutput-/1 10*<.@o.10x^1 0+n
echo -/ 1 10 * <.@o. 10x ^ 1 0 + i
end.
)</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="j"> pi''
3
.
Line 2,661 ⟶ 2,967:
5
3
...</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Icon}}
<langsyntaxhighlight lang="java">import java.math.BigInteger ;
 
public class Pi {
Line 2,709 ⟶ 3,015:
p.calcPiDigits() ;
}
}</langsyntaxhighlight>
 
Output :
Line 2,722 ⟶ 3,028:
process.stdout.write will work in Node.js; to make this work in a browser, change it to document.body.textContent += .
 
<syntaxhighlight lang="text">let q = 1n, r = 180n, t = 60n, i = 2n;
for (;;) {
let y = (q*(27n*i-12n)+5n*r)/(5n*t);
Line 2,732 ⟶ 3,038:
process.stdout.write(y.toString());
if (i === 3n) { process.stdout.write('.'); }
}</langsyntaxhighlight>
 
=== Web Page version ===
Line 2,738 ⟶ 3,044:
This shows how to load the previous code into a webpage that writes digits out without freezing the browser
 
<syntaxhighlight lang="html"><html><head><script src='https://rawgit.com/andyperlitch/jsbn/v1.1.0/index.js'></script></head>
<lang html>
<html><head><script src='https://rawgit.com/andyperlitch/jsbn/v1.1.0/index.js'></script></head>
<body style="width: 100%"><tt id="pi"></tt><tt>...</tt>
<script async defer>
Line 2,782 ⟶ 3,087:
calcPi();
</script>
</body></html></syntaxhighlight>
 
</lang>
=== Web Page using BigInt ===
 
Above converted to use BigInt
 
<syntaxhighlight lang="html"><html>
<head>
</head>
<body style="width: 100%">
<tt id="pi"></tt>
<tt>...</tt>
<script async defer>
function calcPi() {
let q=1n, r=0n, t=1n, k=1n, n=3n, l=3n, nr, nn, digit=0, firstrun=1;
const p=document.getElementById('pi');
function w(s) { p.appendChild(document.createTextNode(s));}
// function continueCalcPi(q, r, t, k, n, l) { // (see note)
function continueCalcPi() {
while (true) {
if (q*4n+r-t < n*t) {
w(n.toString());
if (digit==0 && firstrun==1) { w('.'); firstrun=0; };
digit = (digit+1) % 256;
nr = (r-n*t)*10n;
n = (q*3n+r)*10n/t-n*10n;
q *= 10n;
r = nr;
if (digit%8==0) {
if (digit%64==0) {
p.appendChild(document.createElement('br'));
}
w('\xA0');
// return setTimeout(function() { continueCalcPi(q, r, t, k, n, l); }, 50);
return setTimeout(continueCalcPi, 50);
};
} else {
nr = (q*2n+r)*l;
nn = (q*k*7n+2n+r*l)/(t*l);
q *= k;
t *= l;
l += 2n;
k += 1n;
n = nn;
r = nr;
}
}
}
continueCalcPi(q, r, t, k, n, l);
}
calcPi();
</script>
</body>
</html></syntaxhighlight>
Note: removing the parameters to continueCalcPi() as shown may eat (even) more memory, not entirely sure about that.
 
=== Simple Approximation ===
Returns an approximation of Pi.
 
<syntaxhighlight lang="text">var calcPi = function() {
var n = 20000;
var pi = 0;
Line 2,800 ⟶ 3,159:
}
return pi;
}</langsyntaxhighlight>
 
=={{header|jq}}==
Line 2,819 ⟶ 3,178:
 
The annotation defined here results in a triple for each digit of pi:
[index, digit, space], where "space" is the the sum of the lengths of
the strings in the six-dimensional state vector, [q, r, t, k, n, l].
The output shows that the space requirements of the Gibbons
spigot grow very slightly more than linearly.
 
<langsyntaxhighlight lang="jq"># The Gibbons spigot, in the mold of the [[#Groovy]] and ython[[#Python]] programs shown on this page.
# The "bigint" functions needed are:
needed are:# long_minus long_add long_multiply long_div
 
def pi_spigot:
Line 2,882 ⟶ 3,241:
;
 
pi_spigot</langsyntaxhighlight>
{{out}}
<div style="overflow:scroll; height:200px;">
<langsyntaxhighlight lang="sh">$ jq -M -n -c -f pi.bigint.jq
[0,9,"3"]
[1,14,"1"]
Line 3,190 ⟶ 3,549:
[302,8623,"2"]
...
</langsyntaxhighlight></div>
 
=={{header|Julia}}==
Julia comes with built-in support for computing π in arbitrary precision (using the GNU MPFR library). This implementation computes π at precisions that are repeatedly doubled as more digits are needed, printing one digit at a time and never terminating (until it runs out of memory) as specified:
<langsyntaxhighlight lang="julia">let prec = precision(BigFloat), spi = "", digit = 1
while true
if digit > lastindex(spi)
Line 3,204 ⟶ 3,563:
digit += 1
end
end</langsyntaxhighlight>
 
Output:
Line 3,211 ⟶ 3,570:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.math.BigInteger
Line 3,255 ⟶ 3,614:
}
 
fun main(args: Array<String>) = calcPi()</langsyntaxhighlight>
 
{{out}}
Line 3,261 ⟶ 3,620:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745...
</pre>
 
=={{header|Lambdatalk}}==
1) We can build a lambdatalk function using the lib_BN javascript library.
 
<syntaxhighlight lang="scheme">
{require lib_BN}
 
{def genpi
{def genpi.loop
{lambda {:n :pi :q :r :t :i :z}
{if {> :z :n}
then :pi
else {let { {:n :n} {:pi :pi} {:q :q} {:r :r} {:t :t} {:i :i} {:z :z}
{:digit {BN./ {BN.+ {BN.* {BN.- {BN.* :i 27} 12} :q}
{BN.* :r 5} }
{BN.* :t 5} } }
{:u {BN.* {BN.+ {BN.* :i 3} 1}
{BN.* 3 {BN.+ {BN.* :i 3} 2} } } }
} {genpi.loop :n
{BN.+ :pi :digit}
{BN.* {BN.* :q 1}
{BN.* :i {BN.- {BN.* :i 2} 1} }}
{BN.* {BN.* :u 1}
{BN.+ {BN.* :q {BN.- {BN.* :i 5} 2} }
{BN.- :r {BN.* :t :digit} }}}
{BN.* :t :u}
{BN.+ :i 1}
{+ :z 1}} }}}}
{lambda {:n}
{genpi.loop :n # 1 180 60 2 0} }}
-> genpi
 
We can generate π with 72 digits in about 500ms.
 
{BN.DEC 72}
-> 72 digits
{genpi 60}
-> 3.141592653589793238462643383279502884197169399375105820974944592307816406
</syntaxhighlight>
 
To go further the best is to build a javascript primitive using the script special form.
 
<syntaxhighlight lang="scheme">
{script
LAMBDATALK.DICT["spigot"] = function() {
function generateDigitsOfPi(max) {
var pi = "";
var z = 0;
var q = 1n;
var r = 180n;
var t = 60n;
var i = 2n;
while (z < max) {
var digit = ((i * 27n - 12n) * q + r * 5n) / (t * 5n);
pi += digit;
var u = (i * 3n + 1n) * 3n * (i * 3n + 2n);
r = u * 10n * (q * (i * 5n - 2n) + (r - t * digit));
q = q * 10n * i * (i * 2n - 1n);
i = i + 1n;
t = t * u;
z++;
}
return pi
}
var args = arguments[0].trim();
return generateDigitsOfPi( args );
};
}
 
We can generate 1000 digits of π in about 70ms
 
3.{W.rest {spigot 100}}
-> 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198
 
</syntaxhighlight>
 
=={{header|Lasso}}==
Based off [http://crypto.stanford.edu/pbc/notes/pi/code.html Dik T. Winter's C implementation of Beeler et al. 1972, Item 120].
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
define generatePi => {
Line 3,294 ⟶ 3,728:
loop(200) => {
stdout(#pi_digits())
}</langsyntaxhighlight>
Output (first 100 places):
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
Line 3,301 ⟶ 3,735:
=={{header|Liberty BASIC}}==
Pretty slow if you run for over 100 digits...
<langsyntaxhighlight lang="lb"> ndigits = 0
 
q = 1
Line 3,335 ⟶ 3,769:
wend
 
end</langsyntaxhighlight>
<pre>
3.141592653589793238462643383279502884197
Line 3,344 ⟶ 3,778:
=={{header|Lua}}==
{{trans|Pascal}}
<langsyntaxhighlight lang="lua">a = {}
n = 1000
len = math.modf( 10 * n / 3 )
Line 3,384 ⟶ 3,818:
end
end
print( predigit )</langsyntaxhighlight>
<pre>03141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086 ...</pre>
 
Line 3,394 ⟶ 3,828:
 
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkpi {
Module FindPi(Digits){
Line 3,490 ⟶ 3,924:
Modules ? ' current module exist
Stack ' Stack of values ' has to be empty, we didn't use current stack for values.
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
User can interrupt computation using "Alt+." or "Cmd+." on a Mac.
<langsyntaxhighlight Mathematicalang="mathematica">WriteString[$Output, "3."];
For[i = -1, True, i--,
WriteString[$Output, RealDigits[Pi, 10, 1, i][[1, 1]]]; Pause[.05]];</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Requires the Variable Precision Integer (vpi) Toolbox
Matlab and Octave use double precision numbers per default, and pi is a builtin constant value. Arbitrary precision is only implemented in some additional toolboxes (e.g. symbolic toolbox).
<syntaxhighlight lang="matlab">
<lang MATLAB>pi</lang>
function pi_str = piSpigot(N)
% Return N digits of pi using Gibbons's first spigot algorithm.
% If N is omitted, the digits are printed ad infinitum.
% Uses the expansion
% pi = sum_{i=0} (i!)^2 2^{i+1} /(2i+1)!
% = 2 + 1/3 * ( 2 + 2/5 * (2 + 3/7 * ( 2 + 4/9 * ( ..... )))))
% = (2 + 1/3 *)(2 + 2/5 *)(2 + 3/7 *)...
% where the terms in the last expression represent Linear Fractional
% Transforms (LFTs).
%
% Requires the Variable Precision Integer (vpi) Toolbox
%
% Reference:
% "Unbounded Spigot Algorithms for the Digits of Pi" by J. Gibbons, 2004
% American Mathematical Monthly, vol. 113.
if nargin < 1
N = Inf;
lineLength = 50;
else
pi_str = repmat(' ',1,N);
end
 
q = vpi(1);
r = vpi(0);
t = vpi(1);
k = 1; % If printing more than 3E15 digits, use k = vpi(1);
 
i = 1;
first_digit = true;
while i <= N
threeQplusR = 3*q + r;
n = double(threeQplusR / t);
if q+threeQplusR < (n+1)*t
d = num2str(n);
if isinf(N)
fprintf(1,'%s', d);
if first_digit
fprintf(1,'.');
first_digit = false;
i = i+1;
end
if i == lineLength
fprintf(1,'\n');
i = 0;
end
else
pi_str(i) = d;
end
q = 10*q;
r = 10*(r-n*t);
i = i + 1;
else
t = (2*k+1)*t;
r = (4*k+2)*q + (2*k+1)*r;
q = k*q;
k = k + 1;
end
end
end
</syntaxhighlight>
<pre>
>> pipiSpigot
3.141592653589793238462643383279502884197169399375
ans = 3.1416
10582097494459230781640628620899862803482534211706
> printf('%.60f\n',pi)
79821480865132823066470938446095505822317253594081
3.141592653589793115997963468544185161590576171875000000000000>> format long
28481117450284102701938521105559644622948954930381
96442881097566593344612847564823378678316527120190
91456485669234603486104543266482133936072602491412
</pre>
 
=={{header|MiniScript}}==
Calculate pi using the Rabinowitz-Wagon algorithm
<syntaxhighlight lang="miniscript">digits = input("Enter number of digits to calculate after decimal point: ").val
// I've seen variations of this "precision" calculation from
// 10 * digits
// to
// floor(10 * digits / 3) + 16
// A larger value provides a more precise calculation but also
// takes longer to run. Based on my testing, this calculation
// below for precision produces accurate output for inputs
// from 1 to 4000 - haven't tried larger than this.
precision = floor(10 * digits / 3) + 4
A = [2] * precision
nines = 0
predigit = 0
cnt = 0
while cnt <= digits
carry = 0
for i in range(precision - 1, 1, -1)
temp = 10 * A[i] + carry * i
A[i] = temp % (2 * i - 1)
carry = floor(temp/(2 * i - 1))
end for
A[1] = carry % 10
carry = floor(carry / 10)
current = carry
if current == 9 then
nines += 1
else if current == 10 then
print (predigit+1), ""
cnt += 1
if nines > 0 then
print "9" * nines, ""
cnt += nines
end if
predigit = 0
nines = 0
else
// the first digit produced is always a zero
// don't need to see that
if cnt != 0 then print predigit, ""
cnt += 1
predigit = current
if nines > 0 then
print "9" * nines, ""
cnt += nines
end if
nines = 0
end if
if cnt == 2 then print ".", ""
end while
print str(predigit) * (cnt < digits + 2)</syntaxhighlight>
 
{{out}}
<pre>
Enter number of digits to calculate after decimal point: 1000
Unfortunately this is not the correct value!
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903690113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051329005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710199031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066139019278766111959092164201989
3.14159265358979323846264338327950288419716939937510582
</pre>
=================??????????????????????????????????????</pre>
 
=={{header|Nanoquery}}==
Calling for 60 digit output does not produce 60 digits of precision. Once the sixteen digit precision of double precision is reached, the subsequent digits are determined by the workings of the binary to decimal conversion. The long decimal string is the exact decimal value of the binary representation of pi, which binary value is itself not exact because pi cannot be represented in a finite number of digits, be they decimal, binary or any other integer base...
{{trans|Java}}
<syntaxhighlight lang="nanoquery">q = 1; r = 0; t = 1
k = 1; n = 3; l = 3
 
nn = null; nr = null
first = true
 
while true
if (((4 * q) + r) - t) < (n * t)
print n
if first
print "."
first = false
end
nr = int(10 * (r - (n * t)))
n = int((10 * ((3 * q) + r)) / t) - (10 * n)
q *= 10
r = nr
else
nr = int(((2 * q) + r) * l)
nn = int((((q * (7 * k)) + 2) + (r * l)) / (t * l))
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
end if
end while</syntaxhighlight>
{{out}}
<pre>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679...
</pre>
 
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
import java.math.BigInteger
Line 3,523 ⟶ 4,108:
return
 
-- 07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)07:11, 27 August 2022 (UTC)~~
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
parse arg places .
Line 3,578 ⟶ 4,163:
method isFalse() private static returns boolean
return \isTrue()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,586 ⟶ 4,171:
=={{header|Nim}}==
{{libheader|bigints}}
<langsyntaxhighlight lang="nim">import bigints
 
var
Line 3,631 ⟶ 4,216:
echo ""
i = 0
eliminateDigit d</langsyntaxhighlight>
 
{{out}}
Line 3,643 ⟶ 4,228:
{{trans|D}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import bignum
 
proc calcPi() =
Line 3,674 ⟶ 4,259:
r = nr
 
calcPi()</langsyntaxhighlight>
 
{{out}}
Line 3,685 ⟶ 4,270:
=={{header|OCaml}}==
The Constructive Real library [http://www.lri.fr/~filliatr/creal.en.html Creal] contains an infinite-precision Pi, so we can just print out its digits.
<langsyntaxhighlight OCamllang="ocaml">open Creal;;
 
let block = 100 in
Line 3,696 ⟶ 4,281:
flush stdout;
incr counter
done</langsyntaxhighlight>
However that is cheating if you want to see an algorithm to generate Pi. Since the Spigot algorithm is already used in the [http://benchmarksgame.alioth.debian.org/u64q/program.php?test=pidigits&lang=ocaml&id=1 pidigits] program, this implements [http://mathworld.wolfram.com/Machin-LikeFormulas.html Machin's formula].
<langsyntaxhighlight OCamllang="ocaml">open Num
 
(* series for: c*atan(1/k) *)
Line 3,740 ⟶ 4,325:
incr npr; shift := !shift */ base;
) else (acc := !acc */ d_acc);
done</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: calcPiDigits
| q r t k n l |
1 ->q 0 ->r 1 ->t 1 ->k 3 ->n 3 -> l
Line 3,763 ⟶ 4,348:
k 1+ ->k
]
] ;</langsyntaxhighlight>
 
=={{header|Ol}}==
{{trans|Scheme}}
 
<langsyntaxhighlight lang="scheme">
; 'numbers' is count of numbers or #false for eternal pleasure.
(define (pi numbers)
Line 3,793 ⟶ 4,378:
 
(pi #false)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,811 ⟶ 4,396:
=={{header|PARI/GP}}==
Uses the built-in Brent-Salamin arithmetic-geometric mean iteration.
<langsyntaxhighlight lang="parigp">pi()={
my(x=Pi,n=0,t);
print1("3.");
Line 3,821 ⟶ 4,406:
print1(floor(x*10^n++)%10)
)
};</langsyntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift 4.2}}
<syntaxhighlight lang="swift">
//
// main.swift
// pi digits
//
// Created by max goren on 11/11/21.
// Copyright © 2021 maxcodes. All rights reserved.
//
 
import Foundation
 
var r = [Int]()
var i = 0
var k = 2800
var b = 0
var c = 0
var d = 0
 
for _ in 0...2800 {
r.append(2000);
}
while k > 0 {
d = 0;
i = k;
while (true) {
d = d + r[i] * 10000
b = 2 * i - 1
r[i] = d % b
d = d / b
i = i - 1
if i == 0 {
break;
}
d = d * i;
}
print(c + d / 10000, "")
c = d % 10000
k = k - 14
}
</syntaxhighlight>
 
=={{header|Pascal}}==
Line 3,827 ⟶ 4,455:
With minor editing changes as published by Stanley Rabinowitz in [http://www.mathpropress.com/stan/bibliography/spigot.pdf].
Minor improvement of <user>Mischi</user> { speedup ~2 ( n=10000 , rumtime 4s-> 1,44s fpc 2.6.4 -O3 }, by calculating only necessary digits up to n.
<langsyntaxhighlight lang="pascal">Program Pi_Spigot;
const
n = 1000;
Line 3,890 ⟶ 4,518:
end;
writeln(predigit);
end.</langsyntaxhighlight>
Output:
<pre>% ./Pi_Spigot
Line 3,901 ⟶ 4,529:
This takes a numer-of-digits argument, but we can make it large (albeit using memory and some startup time). Unlike the other two, this uses no modules and does not require bigints so is worth showing.
 
<langsyntaxhighlight lang="perl">sub pistream {
my $digits = shift;
my(@out, @a);
Line 3,941 ⟶ 4,569:
# We've closed the spigot. Print the remainder without rounding.
print join "", @out[$i-15+4 .. $digits-2], "\n";
}</langsyntaxhighlight>
 
==== Raku spigot ====
Line 3,947 ⟶ 4,575:
 
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use bigint try=>"GMP";
sub stream {
my ($next, $safe, $prod, $cons, $z, $x) = @_;
Line 3,990 ⟶ 4,618:
$|++;
print $pi_stream->(), '.';
print $pi_stream->() while 1;</langsyntaxhighlight>
 
==== Machin's Formula ====
Line 3,996 ⟶ 4,624:
Here is an original Perl 5 code, using Machin's formula. Not the fastest program in the world. As with the previous code, using either Math::GMP or Math::BigInt::GMP instead of the default bigint Calc backend will make it run thousands of times faster.
 
<langsyntaxhighlight Perllang="perl">use bigint try=>"GMP";
 
# Pi/4 = 4 arctan 1/5 - arctan 1/239
Line 4,049 ⟶ 4,677:
$ns /= $g;
}
}</langsyntaxhighlight>
 
==== Modules ====
While no current CPAN module does continuous printing, there are (usually fast) ways to get digits of Pi. Examples include:
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">
use ntheory qw/Pi/;
say Pi(10000);
Line 4,073 ⟶ 4,701:
use Math::Big qw/pi/; # Very slow
say pi(10000);
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
I already had this golf entry to hand. Prints 2400 places, change the 8400 (derived from 2400*14/4) as needed, but I've not tested > that.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer a=10000,b,c=8400,d,e=0,g sequence f=repeat(floor(a/5),c+1) while c>0 do g=2*c d=0
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
b=c while b>0 do d+=f[b]*a g-=1 f[b]=remainder(d, g) d=floor(d/g) g-=1 b-=1 if b!=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">8400</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">while</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span>
d*=b end if end while printf(1,"%04d",e+floor(d/a)) c-=14 e = remainder(d,a) end while</lang>
<span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">c</span> <span style="color: #008080;">while</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">+=</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">a</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">-=</span><span style="color: #000000;">1</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">]=</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">/</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">-=</span><span style="color: #000000;">1</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">-=</span><span style="color: #000000;">1</span> <span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">d</span><span style="color: #0000FF;">*=</span><span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%04d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">/</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">-=</span><span style="color: #000000;">14</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
Someone was benchmarking the above against Lua, so I translated the Lua entry, and upped it to 2400 places, for a fairer test.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer n = 2400,
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
len = floor(10*n/3)
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2400</span><span style="color: #0000FF;">,</span>
sequence a = repeat(2,len)
<span style="color: #000000;">len</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
integer nines = 0,
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">)</span>
predigit = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">nines</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
string res = ""
<span style="color: #000000;">predigit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for j=1 to n do
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
integer q = 0
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
for i=len to 1 by -1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer x = 10*a[i]+q*i,
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">len</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
d = 2*i-1
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">q</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span>
a[i] = remainder(x,d)
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
q = floor(x/d)
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
a[1] = remainder(q,10)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
q = floor(q/10)
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
if q==9 then
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
nines = nines+1
<span style="color: #008080;">if</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">==</span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">nines</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nines</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
integer nine = '9'
<span style="color: #008080;">else</span>
if q==10 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">nine</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'9'</span>
predigit += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">==</span><span style="color: #000000;">10</span> <span style="color: #008080;">then</span>
q = 0
<span style="color: #000000;">predigit</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
nine = '0'
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end if
<span style="color: #000000;">nine</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'0'</span>
res &= predigit+'0'&repeat(nine,nines)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
predigit = q
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">predigit</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nine</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nines</span><span style="color: #0000FF;">)</span>
nines = 0
<span style="color: #000000;">predigit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q</span>
end if
<span style="color: #000000;">nines</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res &= predigit+'0'
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,res)</lang>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">predigit</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
=={{header|Picat}}==
{{trans|Erlang}}
<syntaxhighlight lang="picat">go =>
pi2(1,0,1,1,3,3,0),
nl.
 
pi2(Q,R,T,K,N,L,C) =>
if C == 50 then
nl,
pi2(Q,R,T,K,N,L,0)
else
if (4*Q + R-T) < (N*T) then
print(N),
P := 10*(R-N*T),
pi2(Q*10, P, T, K, ((10*(3*Q+R)) div T)-10*N, L,C+1)
else
P := (2*Q+R)*L,
M := (Q*(7*K)+2+(R*L)) div (T*L),
H := L+2,
J := K+ 1,
pi2(Q*K, P, T*L, J, M, H, C)
end
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>31415926535897932384626433832795028841971693993751
05820974944592307816406286208998628034825342117067
98214808651328230664709384460955058223172535940812
84811174502841027019385211055596446229489549303819
64428810975665933446128475648233786783165271201909
14564856692346034861045432664821339360726024914127
37245870066063155881748815209209628292540917153643
67892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854
80744623799627495673518857527248912279381830119491
29833673362440656643086021394946395224737190702179
86094370277053921717629317675238467481846766940513
[Ctrl-C]
</pre>
 
 
=={{header|PicoLisp}}==
The following script uses the spigot algorithm published by Jeremy Gibbons. Hit Ctrl-C to stop it.
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(de piDigit ()
Line 4,136 ⟶ 4,812:
(loop
(prin (piDigit))
(flush) )</langsyntaxhighlight>
Output:
<pre>3.14159265358979323846264338327950288419716939937510582097494459 ...</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">/* Uses the algorithm of S. Rabinowicz and S. Wagon, "A Spigot Algorithm */
/* for the Digits of Pi". */
(subrg, fofl, size):
Line 4,182 ⟶ 4,858:
put edit(predigit) (f(1));
end; /* of begin block */
end Pi_Spigot;</langsyntaxhighlight>
output:
<pre>
Line 4,204 ⟶ 4,880:
With some tweaking.
Prints 100 digits a time. Total possible output limited by available memory.
<langsyntaxhighlight lang="powershell">
Function Get-Pi ( $Digits )
{
Line 4,262 ⟶ 4,938:
}
}
</syntaxhighlight>
</lang>
 
Alternate version using .Net classes
<langsyntaxhighlight lang="powershell">
[math]::pi
</syntaxhighlight>
</lang>
Outputs:
<syntaxhighlight lang="text">.Net digits of pi
3.14159265358979
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Using coroutine with freeze/2 predicate:
 
<syntaxhighlight lang="prolog">
pi_spigot :-
pi(X),
forall(member(Y, X), write(Y)).
 
pi(OUT) :-
pi(1, 180, 60, 2, OUT).
 
pi(Q, R, T, I, OUT) :-
freeze(OUT,
( OUT = [Digit | OUT_]
-> U is 3 * (3 * I + 1) * (3 * I + 2),
Y is (Q * (27 * I - 12) + 5 * R) // (5 * T),
Digit is Y,
Q2 is 10 * Q * I * (2 * I - 1),
R2 is 10 * U * (Q * (5 * I - 2) + R - Y * T),
T2 is T * U,
I2 is I + 1,
pi(Q2, R2, T2, I2, OUT_)
; true)).</syntaxhighlight>
 
=={{header|PureBasic}}==
Calculate Pi, limited to ~24 M-digits for memory and speed reasons.
<langsyntaxhighlight PureBasiclang="purebasic">#SCALE = 10000
#ARRINT= 2000
 
Line 4,314 ⟶ 5,014:
Ctrl:
PrintN(#CRLF$+"Ctrl-C was pressed")
End</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">def calcPi():
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
while True:
Line 4,342 ⟶ 5,042:
sys.stdout.write(str(d))
i += 1
if i == 40: print(""); i = 0</langsyntaxhighlight>output
<pre>
3141592653589793238462643383279502884197
Line 4,362 ⟶ 5,062:
 
Quackery does not have variables, it has ancillary stacks. To expedite translation from Oforth, the first two definitions implement words equivalent to the [https://forth-standard.org/standard/core/VALUE Forth words VALUE and TO].
<langsyntaxhighlight Quackerylang="quackery"> [ immovable
]this[ share ]done[ ] is value ( --> x )
Line 4,393 ⟶ 5,093:
L 2 + to L
K 1+ to K ]
chcount again ]</langsyntaxhighlight>
 
{{out}}
Line 4,416 ⟶ 5,116:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
suppressMessages(library(gmp))
ONE <- as.bigz("1")
Line 4,467 ⟶ 5,167:
}
cat("\n")
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 4,489 ⟶ 5,189:
Utilizing Jeremy Gibbons spigot algorithm and racket generator:
 
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/generator)
Line 4,509 ⟶ 5,209:
(when (zero? i) (display "." ))
(when (zero? (modulo i 80)) (newline)))
</syntaxhighlight>
</lang>
 
Output:
 
<syntaxhighlight lang="text">
3.14159265358979323846264338327950288419716939937510...
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.10}}
<syntaxhighlight lang="raku" perl6line># based on http://www.mathpropress.com/stan/bibliography/spigot.pdf
 
sub stream(&next, &safe, &prod, &cons, $z is copy, @x) {
Line 4,552 ⟶ 5,252:
print $pi[$i];
once print '.'
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 4,585 ⟶ 5,285:
└─ ─┘
</pre>
<langsyntaxhighlight lang="rexx">/*REXX program spits out decimal digits of pi (one digit at a time) until Ctrl─Break.*/
parse arg digs oFID . /*obtain optional argument from the CL.*/
if digs=='' | digs=="," then digs= 1e6 /*Not specified? Then use the default.*/
Line 4,613 ⟶ 5,313:
say /*stick a fork in it, we're all done. */
exit: say; say n%2+1 'iterations took' format(time("Elapsed"),,2) 'seconds.'; exit 0
halt: say; say 'PI_SPIT halted via use of Ctrl─Break.'; signal exit /*show iterations.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; [until the &nbsp; Ctrl─Break &nbsp; key (or equivalent) was pressed]:}}
 
Line 4,634 ⟶ 5,334:
 
This algorithm is limited to the number of decimal digits as specified with the &nbsp; '''numeric digits ddd''' &nbsp; &nbsp; (line or statement six).
<langsyntaxhighlight lang="rexx">/*REXX program spits out decimal digits of pi (one digit at a time) until Ctrl-Break.*/
signal on halt /*───► HALT when Ctrl─Break is pressed.*/
parse arg digs oFID . /*obtain optional argument from the CL.*/
Line 4,665 ⟶ 5,365:
end /*forever*/
exit /*stick a fork in it, we're all done. */
halt: say; say 'PI_SPIT2 halted via use of Ctrl-Break.'; exit</langsyntaxhighlight> <br><br>
 
=={{header|RPL}}==
It is not easy to print character by character with RPL. Something could be done with the <code>DISP</code> instruction, but it would require to manage the cursor position - and anyway the emulator does not emulate <code>DISP</code> !
===Rabinowitz & Wagon algorithm===
{{trans|BBC Basic}}
There is probably a way to translate the BBC BASIC approach into something that uses only the stack, but it has been preferred here to use 'global' variables with the names used by the BBC BASIC program - except for <code>e</code>, which represents the Euler constant in RPL.
{{works with|Halcyon Calc|4.2.7}}
≪ '''IF''' 1 FS?C '''THEN''' "π = " 'output' STO '''END'''
SWAP →STR '''WHILE''' DUP2 SIZE > '''REPEAT''' "0" SWAP + '''END'''
output SWAP + 'output' STO DROP
'PRINT' STO
≪ → m
≪ 1 SF {} 1 m '''START''' #20d + '''NEXT'''
#0d 'ee' STO #2d 'l' STO
m 14 '''FOR''' c
#0 'd' STO c 2 * 1 - R→B 'a' STO
c 1 '''FOR''' p
DUP p GET 100 * d p * + 'd' STO
p d a / LAST ROT * - PUT
'd' a STO/ 'a' 2 STO-
-1 '''STEP'''
'''IF''' d #99d ==
'''THEN''' ee 100 * d + 'ee' STO #2h 'l' STO+
'''ELSE'''
'''IF''' c m ==
'''THEN'''
d 100 / B→R 10 / 0 PRINT
d 100 / LAST ROT * - 'ee' STO
'''ELSE'''
ee d 100 / + B→R l B→R PRINT
d 100 / LAST ROT * - 'ee' STO #2h 'l' STO
'''END'''
'''END'''
-7 '''STEP'''
DROP output
≫ ≫
'M→π' STO
 
200 M→π
{{out}}
<pre>
1: "π = 3.14159265358979323846264338327950288419716939937510582"
</pre>
=== Faster Rabinowitz & Wagon implementation===
{{trans|Fortran}}
This much faster version favors the stack to local variables.
≪ SWAP →STR
'''IF''' 1 FS?C '''THEN''' "." + '''ELSE WHILE''' DUP2 SIZE > '''REPEAT''' "0" SWAP + '''END''' output SWAP + '''END'''
'output' STO DROP
'WRITE' STO
≪ DUP 50 * 3 / IP → n m
≪ 1 SF 0 {} m + 2 CON 0
1 n '''START'''
DROP 0
m 1 '''FOR''' p
p * OVER p GET 100000 * +
p DUP + 1 - MOD LAST / IP
ROT p 4 ROLL PUT SWAP
-1 '''STEP'''
DUP 100000 MOD LAST / IP
5 ROLL + 5 WRITE
ROT ROT
'''NEXT'''
3 DROPN output
≫ ≫
'N→π' STO
 
100 N→π
{{out}}
<pre>
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011" </pre>
 
=={{header|Ruby}}==
{{trans|Icon}}
<langsyntaxhighlight lang="ruby">pi_digits = Enumerator.new do |y|
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
loop do
Line 4,692 ⟶ 5,467:
 
print pi_digits.next, "."
loop { print pi_digits.next }</langsyntaxhighlight>
 
=={{header|Rust}}==
{{trans|Kotlin}}
<langsyntaxhighlight Rustlang="rust">use num_bigint::BigInt;
 
fn main() {
Line 4,732 ⟶ 5,507:
}
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object Pi {
class PiIterator extends Iterable[BigInt] {
var r: BigInt = 0
Line 4,770 ⟶ 5,545:
}
 
}</langsyntaxhighlight>
Output:
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998
Line 4,778 ⟶ 5,553:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scala">
(import (rnrs))
 
Line 4,809 ⟶ 5,584:
(newline)
(set! i 0))))))
</syntaxhighlight>
</lang>
Output:
<pre>3141592653589793238462643383279502884197
Line 4,830 ⟶ 5,605:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
 
Line 4,868 ⟶ 5,643:
end if;
end while;
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/math.htm#pi_spigot_algorithm]
Line 4,874 ⟶ 5,649:
=={{header|Sidef}}==
===Classical Algorithm===
<langsyntaxhighlight lang="ruby">func pi(callback) {
var (q, r, t, k, n, l) = (1, 0, 1, 1, 3, 3)
loop {
Line 4,899 ⟶ 5,674:
 
STDOUT.autoflush(true)
pi(func(digit){ print digit })</langsyntaxhighlight>
 
===Quicker, Unverified Algorithm===
{{trans|Haskell}}
From the same .pdf mentioned throughout this task, from the last page. The original algorithm was written in Haskell, this is a translation which has also been optimized to avoid redundant multiplications. Same output, but the algorithm is based on one of Gosper’s series that yields more than one digit per term on average, so no test is made partway through the iteration. This is capable of producing approximately 100,000 digits at [https://tio.run/##Hc@9boNAEATg3k@xnXfvNjaXcEkUa7v0KeLU0fkwP7Zl4DgiIYtnJ4A0xTTfSNNV2Tmfpry/e2jQEzzgzwVsOXDkgi9c8pUdn3ggEEDD5j3h1zU2ZZOssfxCG4BbXTez9zgItgqdluc3Am1VoP3eqkgH6KKLlYffecvjdrddGEAQkygstFy02JTUok9znXGAp2GVrZJSy1VLmhwgKilghHHzffz8@jnuXB/r/NZ3JRr6aHB5gxk9mlDdI2QjTdM/ tio.run] in the maximum 60 seconds allowed.
<langsyntaxhighlight lang="ruby">func p(c) { var(q,r,t,g,j,h,k,a,b,y) = (1,180,60,60,54,10,10,15,3)
loop { c(y=(q*(a+=27) +5*r)//5*t); static _ = c('.')
r=10*(g+=j+=54)*(q*(b+=5) +r -y*t); q*=h+=k+=40; t*=g } }
STDOUT.autoflush(1):p(func(d){print d})</langsyntaxhighlight>
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">CLASS BIGNUM;
BEGIN
 
Line 5,218 ⟶ 5,993:
TMOD :- TDIVMOD(A, B).MOD;
 
END BIGNUM;</langsyntaxhighlight><syntaxhighlight lang ="simula">EXTERNAL CLASS BIGNUM;
BIGNUM
BEGIN
Line 5,287 ⟶ 6,062:
 
CALCPI;
END.</langsyntaxhighlight>
Output:
<pre>3141592653589793238462643383279502884197
Line 5,310 ⟶ 6,085:
Used the compact algorithm from [https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf Gibbons paper].
Tailspin will at some point have arbitrary precision integers, currently we have to link into java and use BigInteger. Using java code can be slightly awkward as the argument in Tailspin comes before the method, so divide and subtract read backwards.
<langsyntaxhighlight lang="tailspin">
use 'java:java.math' stand-alone
 
Line 5,349 ⟶ 6,124:
 
1 -> g&{q:$one, r:$zero, t:$one, k:$one, n:$three, l:$three} -> !VOID
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,358 ⟶ 6,133:
Based on the reference in the [[#D|D]] code.
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# http://www.cut-the-knot.org/Curriculum/Algorithms/SpigotForPi.shtml
Line 5,388 ⟶ 6,163:
}
}
}</langsyntaxhighlight>
The pi digit generation requires picking a limit to the number of digits; the bigger the limit, the more digits can be ''safely'' computed. A value of 10k yields values relatively rapidly.
<langsyntaxhighlight lang="tcl">coroutine piDigit piDigitsBySpigot 10000
fconfigure stdout -buffering none
while 1 {
puts -nonewline [piDigit]
}</langsyntaxhighlight>
 
=={{header|TypeScript}}==
<langsyntaxhighlight lang="javascript">type AnyWriteableObject={write:((textToOutput:string)=>any)};
 
function calcPi(pipe:AnyWriteableObject) {
Line 5,421 ⟶ 6,196:
}
 
calcPi(process.stdout);</langsyntaxhighlight>
 
'''Notes:'''
Line 5,431 ⟶ 6,206:
=== Async version ===
 
<langsyntaxhighlight lang="javascript">type AnyWriteableObject = {write:((textToOutput:string)=>Promise<any>)};
 
async function calcPi<T extends AnyWriteableObject>(pipe:T) {
Line 5,470 ⟶ 6,245:
});
 
console.log('.'); //start!</langsyntaxhighlight>
 
Here the calculation does not continue if the consumer does not consume the character.
Line 5,480 ⟶ 6,255:
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<langsyntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Line 5,514 ⟶ 6,289:
End If
Next n
End Sub</langsyntaxhighlight>
{{out}}
<pre>3.
Line 5,541 ⟶ 6,316:
{{trans|C#}}
Don't forget to use the "'''Project'''" tab, "'''Add Reference...'''" for '''''System.Numerics''''' (in case you get compiler errors in the Visual Studio IDE)
<langsyntaxhighlight lang="vbnet">Imports System
Imports System.Numerics
Line 5,564 ⟶ 6,339:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll;white-space:pre-wrap;">3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275900994657640789512694683983525957098258226205224894077267194782684826014769909026401363944374553050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382686838689427741559918559252459539594310499725246808459872736446958486538367362226260991246080512438843904512441365497627807977156914359977001296160894416948685558484063534220722258284886481584560285060168427394522674676788952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288797108931456691368672287489405601015033086179286809208747609178249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428584447952658678210511413547357395231134271661021359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435064302184531910484810053706146806749192781911979399520614196634287544406437451237181921799983910159195618146751426912397489409071864942319615679452080951465502252316038819301420937621378559566389377870830390697920773467221825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539057962685610055081066587969981635747363840525714591028970641401109712062804390397595156771577004203378699360072305587631763594218731251471205329281918261861258673215791984148488291644706095752706957220917567116722910981690915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398315019701651511685171437657618351556508849099898599823873455283316355076479185358932261854896321329330898570642046752590709154814165498594616371802709819943099244889575712828905923233260972997120844335732654893823911932597463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100449293215160842444859637669838952286847831235526582131449576857262433441893039686426243410773226978028073189154411010446823252716201052652272111660396665573092547110557853763466820653109896526918620564769312570586356620185581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318586769751456614068007002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830634328587856983052358089330657574067954571637752542021149557615814002501262285941302164715509792592309907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111790429782856475032031986915140287080859904801094121472213179476477726224142548545403321571853061422881375850430633217518297986622371721591607716692547487389866549494501146540628433663937900397692656721463853067360965712091807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862947265473642523081770367515906735023507283540567040386743513622224771589150495309844489333096340878076932599397805419341447377441842631298608099888687413260472156951623965864573021631598193195167353812974167729478672422924654366800980676928238280689964004824354037014163149658979409243237896907069779422362508221688957383798623001593776471651228935786015881617557829735233446042815126272037343146531977774160319906655418763979293344195215413418994854447345673831624993419131814809277771038638773431772075456545322077709212019051660962804909263601975988281613323166636528619326686336062735676303544776280350450777235547105859548702790814356240145171806246436267945612753181340783303362542327839449753824372058353114771199260638133467768796959703098339130771098704085913374641442822772634659470474587847787201927715280731767907707157213444730605700733492436931138350493163128404251219256517980694113528013147013047816437885185290928545201165839341965621349143415956258658655705526904965209858033850722426482939728584783163057777560688876446248246857926039535277348030480290058760758251047470916439613626760449256274204208320856611906254543372131535958450687724602901618766795240616342522577195429162991930645537799140373404328752628889639958794757291746426357455254079091451357111369410911939325191076020825202618798531887705842972591677813149699009019211697173727847684726860849003377024242916513005005168323364350389517029893922334517220138128069650117844087451960121228599371623130171144484640903890644954440061986907548516026327505298349187407866808818338510228334508504860825039302133219715518430635455007668282949304137765527939751754613953984683393638304746119966538581538420568533862186725233402830871123282789212507712629463229563989898935821167456270102183564622013496715188190973038119800497340723961036854066431939509790190699639552453005450580685501956730229219139339185680344903982059551002263535361920419947455385938102343955449597783779023742161727111
Line 5,570 ⟶ 6,345:
===Quicker, unverified algo===
There seems to be another algorithm in the original reference article (see the [http://www.rosettacode.org/wiki/Pi#Ada Ada] entry), which produces output a bit faster. However, the math behind the algorithm has not been completely proven. It's faster because it doesn't calculate whether each digit is accumulated properly before squirting it out. When using (slow) arbitrary precision libraries, this avoids a lot of computation time.
<langsyntaxhighlight lang="vbnet">Imports System, System.Numerics, System.Text
Module Module1
Line 5,577 ⟶ 6,352:
If msg.Length > 0 Then Console.WriteLine(msg)
Dim first As Boolean = True, stp As Integer = 360,
lim As Integer = stp,
res As StringBuilder = New StringBuilder(),
rc As Integer = -1, y As Byte, et As TimeSpan,
Line 5,584 ⟶ 6,358:
q = 1 : r = 180 : t = 60 : g = 60 : j = 54
h = 10 : k = 10 : a = 15 : b = 3
While rcTrue <' 1000use this to stop after a keypress
' While rc < 20000 ' use this to stop after some fixed point
While true
While res.Length < stp
a += 27 : y = CByte((q * a + 5 * r) / (5 * t))
res.Append(y) : b += 5 : j += 54 : g += j
Line 5,593 ⟶ 6,368:
If first Then res.Insert(1, "."c) : first = False
Console.Write(res.ToString())
rc += res.Length : res.Clear() : lim += stp
If Console.KeyAvailable Then Exit While
End While
Line 5,605 ⟶ 6,380:
If Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</langsyntaxhighlight>
{{out}}The First several thousand digits verified the same as the conventional spigot algorithm, haven't detected any differences yet.
<pre style="height:30ex;overflow:scroll;white-space:pre-wrap;">Press a key to exit...
Line 5,614 ⟶ 6,389:
{{trans|Kotlin}}
{{libheader|Wren-big}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "io" for Stdout
 
Line 5,652 ⟶ 6,427:
}
 
calcPi.call()</langsyntaxhighlight>
 
{{out}}
Line 5,658 ⟶ 6,433:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745...
</pre>
 
=={{header|Yabasic}}==
{{trans|BASIC256}}
<syntaxhighlight lang="yabasic">n = 1000
long = 10 * int(n / 4)
needdecimal = 1 //true
dim a(long)
nines = 0
predigit = 0 // {First predigit is a 0}
 
for j = 1 to long
a(j-1) = 2 // {Start with 2s}
next j
 
for j = 1 to n
q = 0
for i = long to 1 step -1
// {Work backwards}
x = 10*a(i-1) + q*i
a(i-1) = mod(x, (2*i - 1))
q = int(x / (2*i - 1))
next i
a(0) = mod(q, 10)
q = int(q / 10)
if q = 9 then
nines = nines + 1
else
if q = 10 then
d = predigit+1
gosub outputd
if nines > 0 then
for k = 1 to nines
d = 0
gosub outputd
next k
end if
predigit = 0
nines = 0
else
d = predigit
gosub outputd
predigit = q
if nines <> 0 then
for k = 1 to nines
d = 9
gosub outputd
next k
nines = 0
end if
end if
end if
next j
print predigit
end
 
label outputd
if needdecimal then
if d = 0 then return : fi
print d;
print ".";
needdecimal = 0 //false
else
print "", d;
endif
return</syntaxhighlight>
 
=={{header|zkl}}==
Uses the GMP big int library.
Same algorithm as many of the others on this page. Uses in place ops to cut down on big int generation (eg add vs +). Unless GC is given some hints, it will use up 16 gig quickly as it outruns the garbage collector.
<langsyntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"),
one=BN(1), two=BN(2), three=BN(3), four=BN(4), seven=BN(7), ten=BN(10);
 
Line 5,685 ⟶ 6,525:
}
}
}();</langsyntaxhighlight>
Runs until ^C hit, the first 1000 digits match the D output.
{{out}}
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.