17
\$\begingroup\$

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.

\$\endgroup\$
  • 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

1 2
3
0
\$\begingroup\$

C++ 26

main(){cout<<6*asin(0.5);}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "It must be able to calculate pi to at least 5 decimal places." \$\endgroup\$ – mbomb007 Jan 14 '15 at 15:06
0
\$\begingroup\$

Ruby 8

-0.0.arg 

Output takes 2 more chars:

p -0.0.arg  # => 3.141592653589793
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

p,n,s=3,2,1
while s<99:
 n,s,z=n+2,s+1,4/(n*(n+1)*(n+2))
 if s%2<1:p+=z
 else:p-=z
print(p)

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

Output

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

| improve this answer | |
\$\endgroup\$
-1
\$\begingroup\$

Java, 55

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

Hooray for static blocks ^_^

| improve this answer | |
\$\endgroup\$
  • 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
-1
\$\begingroup\$

bc, 8 characters

Solution :

3+.14159

Usage :

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

Python, 27 chars

print "%.5f"%(355.0/113.0)
| improve this answer | |
\$\endgroup\$
-2
\$\begingroup\$

Matlab or Octave

0+pi

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

octave:5> 0+pi
ans =  3.14159265358979
| improve this answer | |
\$\endgroup\$
1 2
3

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