Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
geekpradd committed Feb 27, 2015
0 parents commit 254bb58
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
85 changes: 85 additions & 0 deletions PythonPi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Name: pi.py
Purpose: Get the value of Pi to n number of decimal places
Author: Pradipta (geekpradd)
Algorithm: Chudnovsky Algorithm
License: MIT
Module Dependencies:
Math provides fast square rooting
Decimal gives the Decimal data type which is much better than Float
sys is needed to set the depth for recursion.
"""
import math, sys
from decimal import *
getcontext().rounding = ROUND_FLOOR
sys.setrecursionlimit(100000)
if sys.version_info[0] == 2:
input = raw_input

def factorial(n):
"""
Return the Factorial of a number using recursion
Parameters:
n -- Number to get factorial of
"""
if not n:
return 1
return n*factorial(n-1)


def getIteratedValue(k):
"""
Return the Iterations as given in the Chudnovsky Algorithm.
k iterations gives k-1 decimal places.. Since we need k decimal places
make iterations equal to k+1
Parameters:
k -- Number of Decimal Digits to get
"""
k = k+1
getcontext().prec = k
sum=0
for k in range(k):
first = factorial(6*k)*(13591409+545140134*k)
down = factorial(3*k)*(factorial(k))**3*(640320**(3*k))
sum += first/down
return Decimal(sum)

def getValueOfPi(k):
"""
Returns the calculated value of Pi using the iterated value of the loop
and some division as given in the Chudnovsky Algorithm
Parameters:
k -- Number of Decimal Digits upto which the value of Pi should be calculated
"""
iter = getIteratedValue(k)
up = 426880*math.sqrt(10005)
pi = Decimal(up)/iter

return pi

def shell():
"""
Console Function to create the interactive Shell.
Runs only when __name__ == __main__ that is when the script is being called directly
No return value and Parameters
"""
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")

while True:
print (">>> ", end='')
entry = input()
if entry == "quit":
break
if not entry.isdigit():
print ("You did not enter a number. Try again")
else:
print (getValueOfPi(int(entry)))

if __name__=='__main__':
shell()
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
##Value Of Pi in Python

Get the Value of Pi upto n decimal digits using this Python Script. Uses the chudnovsky algorithm implemented using the Pyton Decimal Data Type.

####Installation

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

#####Using pip

```
pip install PythonPi
```

####Usage

#####Console Usage

Just run the file (if not installed using pip) or enter the following command (if installed using pip):

```
pythonpi
```

You can then use the Interactive Shell to do your calculations

#####API Usage

If you for some reason need the value of pi in your program then you can use the module in the following way:

```python
import PythonPi

print(PythonPi.getValueOfPi(12)) #Upto 12 decimal places
```

####About

Created By Pradipta Bora (geekpradd) using the Chudnovsky Algorithm. <a href="http://opensource.org/licenses/MIT">MIT </a> Licensed.
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from setuptools import setup
try:
import pypandoc
description = pypandoc.convert('README.md','rst')
except:
description=''

setup(
name = "PythonPi",
version = '1.0.0',
author = 'Pradipta Bora',
author_email = 'pradd@outlook.com',
description = "Get the Value of Pi upto as many decimal places as needed",
license = "MIT",
keywords = "pi maths",
url = "https://github.com/geekpradd/PythonPi",
py_modules = ['PythonPi'],
entry_points = {
'console_scripts': ['pythonpi = PythonPi:shell']
},
long_description=description,
classifiers=[
"Development Status :: 5 - Production/Stable",
"Topic :: Utilities",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python"
],

)

0 comments on commit 254bb58

Please sign in to comment.