Calculate 500 digits of pi

25

4

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.

Thomas O

Posted 2011-02-04T12:38:20.667

Reputation: 3 044

1Can we use an HTTP library to download a "digits of pi" website? ;-) – dan04 – 2011-02-17T05:47:11.917

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? – Don Hatch – 2016-04-28T08:21:33.813

I think this question would have been much better off restricting the programming languages accepted. – Tyzoid – 2014-01-04T05:32:44.797

Related mathematics post: https://math.stackexchange.com/q/1189820/272831

– Simply Beautiful Art – 2017-09-18T18:42:05.163

It may not use the digits "3", "1" and "4" consecutively I assume that you can do "3"+".14" or reverse(<500 digits of pi in reverse>). – Naruyoko – 2019-10-03T17:19:20.663

To check if your digits are correct: http://www.eveandersson.com/pi/digits/

– Nellius – 2011-02-04T15:07:35.437

Are we allowed to print more than 500 digits with loss of accuracy after first 500? – Alexandru – 2011-02-04T15:27:05.093

@Alexandru, I suppose so but I would prefer to see it truncated. – Thomas O – 2011-02-04T17:16:38.933

@Joey no library functions TO CALCULATE PI - I would assume you can use anything from the libraries except the PI constant / function. – Aurel Bílý – 2011-02-05T21:27:38.970

Answers

11

Golfscript - 29 chars

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

I will post analysis later

gnibbler

Posted 2011-02-04T12:38:20.667

Reputation: 14 170

@gnibbler. Nice. I tried continued fraction formula, but it converged too slow. I might have done something wrong, though. – Alexandru – 2011-02-06T14:50:21.000

@hallvabo that's not a continued fraction, that's the same pi = 2 + 1/3*(2 + 2/5*(2 + 3/7*(2 + 4/9*(2 + ...)))) that everyone uses. Also, only 500*log2(10) = 1661 (*2 = 3322) iterations should be necessary for 500 digits. I imagine the 6666 is a left over from the 1000 digits problem. – primo – 2012-11-30T04:57:10.447

@primo: I must have hallucinated... I have used this formula in my own Python codegolf solutions several times. – hallvabo – 2012-12-22T13:16:20.723

15"I will post analysis later" waits for more than 6 years\ – Erik the Outgolfer – 2017-04-24T10:42:57.433

1@EriktheOutgolfer I was going to post that. :P – Christopher – 2017-04-25T00:53:38.993

@Justin Tears... – Simply Beautiful Art – 2017-09-18T18:40:55.393

65"I will post analysis later". (waits for 3 years).... – Justin – 2014-02-26T03:40:26.787

1"I will post analysis later" (waits for 8 years) – Lyxal – 2019-10-04T06:02:52.443

"I will post analysis later" (waits for 9 years) – Srivaths – 2020-02-18T15:11:59.260

5Could you explain how this works? – Thomas O – 2011-02-04T13:48:12.577

Shouldn't last digit be 2 instead of 1? – Alexandru – 2011-02-04T17:34:38.193

@Alexandu Starting at position 499, the following digits are 91298, so I guess the difference is about starting with the leading "3.14" or with ".14" – Dr. belisarius – 2011-02-04T21:43:27.473

A 29 bytes version can be found at the GolfScript homepage.

– hallvabo – 2011-02-04T23:09:59.387

8

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.

Stack Tracer

Posted 2011-02-04T12:38:20.667

Reputation: 259

Perhaps you should insert, in your answer, an explanation of why this works (for them non-math folks). – Justin – 2014-02-26T03:31:25.210

wonderful idea. I will see to it presently. I'll give a basic explanation of the math involved. – Stack Tracer – 2014-02-26T03:31:48.950

Maybe you could shorten it: change sqrt[1-x^2] to (1-x^2)^.5) – Justin – 2014-02-26T03:33:38.813

and I can remove the * after the 2. Mathematica is wonderful. – Stack Tracer – 2014-02-26T03:36:51.670

4

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

Soulman

Posted 2011-02-04T12:38:20.667

Reputation: 141

3

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

Charles

Posted 2011-02-04T12:38:20.667

Reputation: 2 435

2

Mathematica (17 bytes)

N[ArcCos[-1],500]

Proof of validity.

Quixotic

Posted 2011-02-04T12:38:20.667

Reputation: 2 199

2

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

scale=500
4*a(1)

Alexandru

Posted 2011-02-04T12:38:20.667

Reputation: 5 485

1@Thomas O: if this is cheating, where's the limit? – J B – 2011-03-17T06:53:33.157

5The rules say "nor may it call a library function to calculate pi." – Peter Taylor – 2011-02-04T20:09:37.483

@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. – Dr. belisarius – 2011-02-04T21:52:02.987

I think this is cheating because atan calculates 1/4 pi but it is an interesting solution nonetheless. – Thomas O – 2011-02-05T10:55:12.563

1

Mathematica - 50

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

swish

Posted 2011-02-04T12:38:20.667

Reputation: 7 484

1

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.

isaacg

Posted 2011-02-04T12:38:20.667

Reputation: 39 268

This answer is incorrect, it generates 34247779... which, to my knowledge, is not pi. – orlp – 2015-03-26T19:11:22.227

@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. – isaacg – 2015-03-27T03:13:04.527

This doesn't deserve a downvote... – Beta Decay – 2014-10-26T11:21:37.963

1

Python3 136

Uses Madhava's formula.

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])

Alexandru

Posted 2011-02-04T12:38:20.667

Reputation: 5 485

0

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

RosLuP

Posted 2011-02-04T12:38:20.667

Reputation: 3 036

0

JavaScript, 68 bytes

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

Try it online!

Yehuda Schwartz

Posted 2011-02-04T12:38:20.667

Reputation: 101