Calculate 500 digits of pi

Write a program to calculate the first 500 digits of pi, meeting the rules below:

• It must be less than 500 characters in length.
• It cannot include "pi", "math.pi" or similar pi constants, nor may it call a library function to calculate pi.
• It may not use the digits "3", "1" and "4" consecutively.
• It must execute in a reasonable time (under 1 minute) on a modern computer.

The shortest program wins.

• To check if your digits are correct: eveandersson.com/pi/digits Feb 4 '11 at 15:07
• Are we allowed to print more than 500 digits with loss of accuracy after first 500? Feb 4 '11 at 15:27
• @Alexandru, I suppose so but I would prefer to see it truncated. Feb 4 '11 at 17:16
• Can we use an HTTP library to download a "digits of pi" website? ;-) Feb 17 '11 at 5:47
• Came here hoping to get something nice and concise for generating arbitrary length approximations of pi in python... unfortunately @Soulman's python solution is apparently tuned for 500 digits; replacing 500 with 1000 gives an incorrect answer. I wonder if there is a good way of phrasing an alternative challenge that would produce a nice short function that is generally useful for generating an arbitrary number of digits? Apr 28 '16 at 8:21

Golfscript - 29 chars

6666,-2%{2+.2/@*\/9)499?2*+}*

I will post analysis later

• Could you explain how this works? Feb 4 '11 at 13:48
• "I will post analysis later". (waits for 3 years).... Feb 26 '14 at 3:40
• "I will post analysis later" *waits for more than 6 years* Apr 24 '17 at 10:42
• "I will post analysis later" (waits for 8 years) Oct 4 '19 at 6:02
• Still waiting... Aug 14 '20 at 15:51

Mathematica (34 chars): (without "cheating" with trig)

N[2Integrate[[1-x^2]^.5,-1,1],500]

So, to explain the magic here:
Integrate[function, lower, upper] gives you the area under the curve "function" from "lower" to "upper". In this case, that function is [1-x^2]^.5, which is a formula that describes the top half of a circle with radius 1. Because the circle has a radius of 1, it does not exist for values of x lower than -1 or higher than 1. Therefore, we are finding the area of half of a circle. When we multiply by 2, then we get the area inside of a circle of radius 1, which is equal to pi.

• Perhaps you should insert, in your answer, an explanation of why this works (for them non-math folks). Feb 26 '14 at 3:31
• wonderful idea. I will see to it presently. I'll give a basic explanation of the math involved. Feb 26 '14 at 3:31
• Maybe you could shorten it: change sqrt[1-x^2] to (1-x^2)^.5) Feb 26 '14 at 3:33
• and I can remove the * after the 2. Mathematica is wonderful. Feb 26 '14 at 3:36

Python (83 chars)

P=0
B=10**500
i=1666
while i:d=2*i+1;P=(P*i%B+(P*i/B+3*i)%d*B)/d;i-=1
print'3.%d'%P

Husk, 2825 24 bytes

i*!500İ⁰ΣG*2mṠ/o!İ1→ḣ□70

Try it online!

Calculates the value of pi as a rational number using the first 5000 terms of the infinite series 2 + 1/3*(2 + 2/5*(2 + 3/7*(2 + 4/9*(2 + ...)))), and then extracts the first 500 digits.

The code to calculate the value of pi from a specified number of terms is only 13 bytes (ΣG*2mṠ/o!İ1→ḣ):

ΣG*2mṠ/o!İ1→ḣ
Σ                       # the sum of
G*2                    # the cumulative product, starting at 2, of
m                   # mapping the following function to all terms of
ḣ           # series from 1 to ... (whatever number is specified)
Ṡ/                 # divide by x
o!  →            # element at index -1
İ1             # of series of odd numbers

Unfortunately, we then need to waste 3 bytes specifying the number of terms to use:

□70                     # 70^2 = 4900

And then 8 more bytes converting the rational number (expressed as a fraction) into its digits in decimal form:

i*!500İ⁰
i                       # integer value of
*                      # multiplying by
!500                  # 500th element of
İ⁰                # series of powers of 10

PARI/GP, 14

\p500
acos(-1)

You can avoid trig by replacing the second line with

gamma(.5)^2

or

(6*zeta(2))^.5

or

psi(3/4)-psi(1/4)

or

4*intnum(x=0,1,(1-x^2)^.5)

or

sumalt(k=2,(-1)^k/(2*k-3))*4

Python3 136

from decimal import *
D=Decimal
getcontext().prec=600
p=D(3).sqrt()*sum(D(2-k%2*4)/3**k/(2*k+1)for k in range(1100))
print(str(p)[:502])

Python3 164

Uses this formula.

from decimal import *
D=Decimal
getcontext().prec=600
p=sum(D(1)/16**k*(D(4)/(8*k+1)-D(2)/(8*k+4)-D(1)/(8*k+5)-D(1)/(8*k+6))for k in range(411))
print(str(p)[:502])

Mathematica (17 bytes)

N[ArcCos[-1],500]

Pyth, 21

u+/*GHhyHy^T500r^3T1Z

Uses this algorithm: pi = 2 + 1/3*(2 + 2/5*(2 + 3/7*(2 + 4/9*(2 + ...)))) found in the comments of the Golfscript answer.

• This doesn't deserve a downvote... Oct 26 '14 at 11:21
• This answer is incorrect, it generates 34247779... which, to my knowledge, is not pi.
– orlp
Mar 26 '15 at 19:11
• @orlp The r operation was recently changed in a way which broke this answer. Change the 1 to a 0, and it will work in current Pyth. Mar 27 '15 at 3:13

JavaScript, 60 bytes

i=1n;p=x=3n*(10n**520n);while(x=x*i/(i*4n+4n)){i+=2n;p+=x/i}

Try it online!

• 62 bytes: for(i=1n,p=x=3n*(10n**520n);x>0;x=x*i/(i*4n+4n),p+=x/(i+=2n)); Nov 15 '20 at 16:13

Java 10, 208207206193 176 bytes

-14 bytes thanks to @ceilingcat.
-17 bytes thanks to @jeJe.

Try it online.

Or as full program (228 bytes):

interface M{static void main(String[]a){var t=java.math.BigInteger.TEN;var p=t=t.pow(503).shiftLeft(1);for(int i=1;i<2e3;p=p.add(t))t=t.multiply(t.valueOf(i)).divide(t.valueOf(i+++i));System.out.print((p+"").substring(0,500));}}

Try it online.

• 176 Oct 13 '21 at 20:32
• @jeje Thanks. :) Oct 14 '21 at 7:29

bc -l (22 = 5 command line + 17 program)

scale=500
4*a(1)
• The rules say "nor may it call a library function to calculate pi." Feb 4 '11 at 20:09
• @Peter The problem I guess, is that "library function" is not always a well defined term, and it only get worse when you say "to calculate Pi", as you may use it to calculate intermediate results, for example Sqrt() in Alexandru's answer. Feb 4 '11 at 21:52
• I think this is cheating because atan calculates 1/4 pi but it is an interesting solution nonetheless. Feb 5 '11 at 10:55
• @Thomas O: if this is cheating, where's the limit?
– J B
Mar 17 '11 at 6:53
• trig functions should have been prohibited because of answers like this. the idea is to calculate pi with an algorithm, not a built-in function. sqrt is a bit different as it's not a trig function. Nov 18 '20 at 10:36

Mathematica - 50

½ = 1/2; 2/Times @@ FixedPointList[(½ + ½ #)^½~N~500 &, ½^½]

Axiom, 80 bytes

digits(503);v:=1./sqrt(3);6*reduce(+,[(-1)^k*v^(2*k+1)/(2*k+1)for k in 0..2000])

for reference https://tuts4you.com/download.php?view.452; it would be an approssimation to 6*arctg(1/sqrt(3))=%pi and it would use serie expansion for arctg

3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 592307816
4 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 505822317
2 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 442881097
5 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 454326648
2 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 917153643
6 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 575959195
3 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 891227938
1 8301194913 01

05AB1E, 20 bytes

₄°·D.ΓN>*N·3+÷}O+₄;£

Port of my Java answer (with the 503 replaced with 1000 - anything $$\\geq503\$$ is fine to output the first 500 digits accurately with this approach).

Explanation:

₄°              # Push 10**1000
·             # Double it to 2e1000
D            # Duplicate it
.Γ          # Loop until the result no longer changes,
# collecting all intermediate results
# (excluding the initial value unfortunately)
N>        #  Push the 0-based loop-index, and increase it by 1 to make it 1-based
*       #  Multiply this 1-based index to the current value
N·     #  Push the 0-based index again, and double it
3+   #  Add 3 to it
÷  #  Integer-divide the (index+1)*value by this (2*index+3)
}O         # After the cumulative fixed-point loop: sum all values in the list
+        # Add the 2e1000 we've duplicated, which wasn't included in the list
₄;      # Push 1000, and halve it to 500
£     # Leave the first 500 digits of what we've calculated
# (after which it is output implicitly as result)

Fortran, 154 bytes

Mangled the rosetta code solution. Saved lots of bytes using implicit integers i j k l m n, print instead of write, and shuffling things around

Try it Online...

integer,dimension(3350)::v=2;x=1E5;j=0
do n=1,101;do l=3350,1,-1
m=x*v(l)+i*l;i=m/(2*l-1);v(l)=m-i*(2*l-1);enddo
k=i/x;print'(I5.5)',j+k;j=i-k*x;enddo
end

APL (NARS2000), 20 bytes

{2+⍵×⍺÷1+⍨2×⍺}/⍳7e3x

I haven't been able to test this, but here's a version in Dyalog APL. The only difference between them is the suffix "x", which is used for rational numbers in NARS2000 but is not available in Dyalog (or other variants available online, as far as I know).

It's based on the pi = 2 + 1/3*(2 + 2/5*(2 + 3/7*(2 + 4/9*(2 + ...)))) formula in the comments under the accepted Golfscript answer.