Skip to content

Commit 254bb58

Browse files
author
geekpradd
committed
Initial Release
0 parents  commit 254bb58

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

PythonPi.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Name: pi.py
3+
Purpose: Get the value of Pi to n number of decimal places
4+
Author: Pradipta (geekpradd)
5+
Algorithm: Chudnovsky Algorithm
6+
License: MIT
7+
8+
Module Dependencies:
9+
10+
Math provides fast square rooting
11+
Decimal gives the Decimal data type which is much better than Float
12+
sys is needed to set the depth for recursion.
13+
"""
14+
import math, sys
15+
from decimal import *
16+
getcontext().rounding = ROUND_FLOOR
17+
sys.setrecursionlimit(100000)
18+
if sys.version_info[0] == 2:
19+
input = raw_input
20+
21+
def factorial(n):
22+
"""
23+
Return the Factorial of a number using recursion
24+
25+
Parameters:
26+
n -- Number to get factorial of
27+
"""
28+
if not n:
29+
return 1
30+
return n*factorial(n-1)
31+
32+
33+
def getIteratedValue(k):
34+
"""
35+
Return the Iterations as given in the Chudnovsky Algorithm.
36+
k iterations gives k-1 decimal places.. Since we need k decimal places
37+
make iterations equal to k+1
38+
39+
Parameters:
40+
k -- Number of Decimal Digits to get
41+
"""
42+
k = k+1
43+
getcontext().prec = k
44+
sum=0
45+
for k in range(k):
46+
first = factorial(6*k)*(13591409+545140134*k)
47+
down = factorial(3*k)*(factorial(k))**3*(640320**(3*k))
48+
sum += first/down
49+
return Decimal(sum)
50+
51+
def getValueOfPi(k):
52+
"""
53+
Returns the calculated value of Pi using the iterated value of the loop
54+
and some division as given in the Chudnovsky Algorithm
55+
56+
Parameters:
57+
k -- Number of Decimal Digits upto which the value of Pi should be calculated
58+
"""
59+
iter = getIteratedValue(k)
60+
up = 426880*math.sqrt(10005)
61+
pi = Decimal(up)/iter
62+
63+
return pi
64+
65+
def shell():
66+
"""
67+
Console Function to create the interactive Shell.
68+
Runs only when __name__ == __main__ that is when the script is being called directly
69+
70+
No return value and Parameters
71+
"""
72+
print ("Welcome to Pi Calculator. In the shell below Enter the number of digits upto which the value of Pi should be calculated or enter quit to exit")
73+
74+
while True:
75+
print (">>> ", end='')
76+
entry = input()
77+
if entry == "quit":
78+
break
79+
if not entry.isdigit():
80+
print ("You did not enter a number. Try again")
81+
else:
82+
print (getValueOfPi(int(entry)))
83+
84+
if __name__=='__main__':
85+
shell()

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##Value Of Pi in Python
2+
3+
Get the Value of Pi upto n decimal digits using this Python Script. Uses the chudnovsky algorithm implemented using the Pyton Decimal Data Type.
4+
5+
####Installation
6+
7+
If you want to use this as a module, then you can use pip or just download the script to your computer from <a href="">here</a>.
8+
9+
#####Using pip
10+
11+
```
12+
pip install PythonPi
13+
```
14+
15+
####Usage
16+
17+
#####Console Usage
18+
19+
Just run the file (if not installed using pip) or enter the following command (if installed using pip):
20+
21+
```
22+
pythonpi
23+
```
24+
25+
You can then use the Interactive Shell to do your calculations
26+
27+
#####API Usage
28+
29+
If you for some reason need the value of pi in your program then you can use the module in the following way:
30+
31+
```python
32+
import PythonPi
33+
34+
print(PythonPi.getValueOfPi(12)) #Upto 12 decimal places
35+
```
36+
37+
####About
38+
39+
Created By Pradipta Bora (geekpradd) using the Chudnovsky Algorithm. <a href="http://opensource.org/licenses/MIT">MIT </a> Licensed.

setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from setuptools import setup
2+
try:
3+
import pypandoc
4+
description = pypandoc.convert('README.md','rst')
5+
except:
6+
description=''
7+
8+
setup(
9+
name = "PythonPi",
10+
version = '1.0.0',
11+
author = 'Pradipta Bora',
12+
author_email = '[email protected]',
13+
description = "Get the Value of Pi upto as many decimal places as needed",
14+
license = "MIT",
15+
keywords = "pi maths",
16+
url = "https://github.com/geekpradd/PythonPi",
17+
py_modules = ['PythonPi'],
18+
entry_points = {
19+
'console_scripts': ['pythonpi = PythonPi:shell']
20+
},
21+
long_description=description,
22+
classifiers=[
23+
"Development Status :: 5 - Production/Stable",
24+
"Topic :: Utilities",
25+
"License :: OSI Approved :: MIT License",
26+
"Operating System :: OS Independent",
27+
"Programming Language :: Python"
28+
],
29+
30+
)

0 commit comments

Comments
 (0)