# Pi Calculation Code Golf [closed]

## 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.

• @hvd Why do you think it should be disqualified? It fits the specs ... – Dr. belisarius Feb 24 '14 at 21:53
• @hvd acos(-1). I win! – Level River St Feb 24 '14 at 23:47
• 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. – Nicolas Barbulesco Feb 25 '14 at 20:07
• 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. – Cruncher Feb 25 '14 at 21:54
• To make this question interesting it needs a scoring function that rewards digits of pi per byte of code. – Ben Jackson Feb 26 '14 at 5:16

# Python 3.x - 123

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

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)

• "It must be able to calculate pi to at least 5 decimal places." – mbomb007 Jan 14 '15 at 15:06

# Ruby 8

-0.0.arg


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.

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.

# Java, 55

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


Hooray for static blocks ^_^

• -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. – Justin Feb 25 '14 at 1:06
• Also, that will never compile without a main method. – Isiah Meadows Feb 25 '14 at 4:20
• @Quincunx That is why I linked it. – Navin Feb 25 '14 at 14:21
• @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. – Navin Feb 25 '14 at 14:23
• 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. – David Conrad Mar 4 '14 at 18:27

bc, 8 characters

Solution :

3+.14159


Usage :

$bc <<< "3+.14159" 3.14159$

• +1 amazing technique! HOW DOES THIS EVEN WORK?? – Jason C Feb 26 '14 at 4:25

## Python, 27 chars

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


# Windows Calculator, 1 click

0+pi

octave:5> 0+pi