The Challenge

You must calculate pi in the shortest length you can. Any language is welcome to join and you can use any formula to calculate pi. It must be able to calculate pi to at least 5 decimal places. Shortest, would be measured in characters. Competition lasts for 48 hours. Begin.

Note: This similar question states that PI must be calculated using the series 4 * (1 – 1/3 + 1/5 – 1/7 + …). This question does not have this restriction, and in fact a lot of answers here (including the most likely to win) would be invalid in that other question. So, this is not a duplicate.


closed as unclear what you're asking by Jason C, mbomb007, Blue, Rɪᴋᴇʀ, clismique Feb 17 '17 at 23:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ @hvd Why do you think it should be disqualified? It fits the specs ... \$\endgroup\$ – Dr. belisarius Feb 24 '14 at 21:53
  • 5
    \$\begingroup\$ @hvd acos(-1). I win! \$\endgroup\$ – Level River St Feb 24 '14 at 23:47
  • 4
    \$\begingroup\$ This looks weird, inconsistent. Calculating π has to be dividing a circle by its diameter, or some other operation giving π. If we accept doing 355/113 — which has nothing to do with π except luck —, like @ace, then logically we should accept doing 3.14159. \$\endgroup\$ – Nicolas Barbulesco Feb 25 '14 at 20:07
  • 7
    \$\begingroup\$ I don't get why people like this question. This is one of the most ill-defined and uninteresting questions I've seen on here. The only difference between this and hello world, is that this has something to do with Pi. \$\endgroup\$ – Cruncher Feb 25 '14 at 21:54
  • 8
    \$\begingroup\$ To make this question interesting it needs a scoring function that rewards digits of pi per byte of code. \$\endgroup\$ – Ben Jackson Feb 26 '14 at 5:16

68 Answers 68


Python 3.x - 123

Sadly this has very little precision... giving only 1-2 decimal places correct.

Anyway this is a fun method of calculating pi.

import random
R = lambda: random.uniform(-1,1)
print(sum(1 for _ in range(10**6) if abs(R() + R()*1j) < 1)*4/10**6)
  • \$\begingroup\$ "It must be able to calculate pi to at least 5 decimal places." \$\endgroup\$ – mbomb007 Jan 14 '15 at 15:06

Ruby 8


Output takes 2 more chars:

p -0.0.arg  # => 3.141592653589793

Python 3, 91 (with display), 82 (just calculation)

This knows that Pi is 3, then it uses the Nilakantha method to calculate Pi accurately to 6 decimal places.

while s<99:
 if s%2<1:p+=z

It starts with the humorous 3,2,1, but also with the rather distasteful p,n,s...


3.141592 395970107
The space is to separate the correct output from the incorrect.


Java, 55

class P{static{System.out.print(Math.toRadians(180));}}

Hooray for static blocks ^_^

  • 2
    \$\begingroup\$ -1. This is a direct copy of codegolf.stackexchange.com/a/22025/9498 . You thought of using the static block, but since this is identical to the other answer, you should have posted this idea in a comment. \$\endgroup\$ – Justin Feb 25 '14 at 1:06
  • \$\begingroup\$ Also, that will never compile without a main method. \$\endgroup\$ – Isiah Meadows Feb 25 '14 at 4:20
  • \$\begingroup\$ @Quincunx That is why I linked it. \$\endgroup\$ – Navin Feb 25 '14 at 14:21
  • \$\begingroup\$ @impinball IIRC, main() is only needed at runtime in Java (unlike most other languages which need an entry point to compile). The static block will run before it crashes. \$\endgroup\$ – Navin Feb 25 '14 at 14:23
  • \$\begingroup\$ It doesn't, though. When Java sees it doesn't have a main() method, it doesn't even load the class and the static block never runs. At least, that's the result I get with the latest Java 8 build. \$\endgroup\$ – David Conrad Mar 4 '14 at 18:27

bc, 8 characters

Solution :


Usage :

$ bc <<< "3+.14159"
  • 4
    \$\begingroup\$ +1 amazing technique! HOW DOES THIS EVEN WORK?? \$\endgroup\$ – Jason C Feb 26 '14 at 4:25

Python, 27 chars

print "%.5f"%(355.0/113.0)

Windows Calculator, 1 click

Inspired by This Answer

enter image description here

  • 1
    \$\begingroup\$ This answer is a blatant advertisement for your other answer. There is no relation whatsoever. \$\endgroup\$ – Justin Jun 29 '14 at 1:55

Matlab or Octave


So short stackexchange won't let me post it without this. The output:

octave:5> 0+pi
ans =  3.14159265358979

Not the answer you're looking for? Browse other questions tagged or ask your own question.