├── .gitignore ├── PythonPi.py ├── README.md └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | *.egg-info/ -------------------------------------------------------------------------------- /PythonPi.py: -------------------------------------------------------------------------------- 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 | from __future__ import print_function 15 | import math, sys 16 | from decimal import * 17 | getcontext().rounding = ROUND_FLOOR 18 | sys.setrecursionlimit(100000) 19 | 20 | python2 = sys.version_info[0] == 2 21 | if python2: 22 | input = raw_input 23 | 24 | def factorial(n): 25 | """ 26 | Return the Factorial of a number using recursion 27 | 28 | Parameters: 29 | n -- Number to get factorial of 30 | """ 31 | if not n: 32 | return 1 33 | return n*factorial(n-1) 34 | 35 | 36 | def getIteratedValue(k): 37 | """ 38 | Return the Iterations as given in the Chudnovsky Algorithm. 39 | k iterations gives k-1 decimal places.. Since we need k decimal places 40 | make iterations equal to k+1 41 | 42 | Parameters: 43 | k -- Number of Decimal Digits to get 44 | """ 45 | k = k+1 46 | getcontext().prec = k 47 | sum=0 48 | for k in range(k): 49 | first = factorial(6*k)*(13591409+545140134*k) 50 | down = factorial(3*k)*(factorial(k))**3*(640320**(3*k)) 51 | sum += first/down 52 | return Decimal(sum) 53 | 54 | def getValueOfPi(k): 55 | """ 56 | Returns the calculated value of Pi using the iterated value of the loop 57 | and some division as given in the Chudnovsky Algorithm 58 | 59 | Parameters: 60 | k -- Number of Decimal Digits upto which the value of Pi should be calculated 61 | """ 62 | iter = getIteratedValue(k) 63 | up = 426880*math.sqrt(10005) 64 | pi = Decimal(up)/iter 65 | 66 | return pi 67 | 68 | def shell(): 69 | """ 70 | Console Function to create the interactive Shell. 71 | Runs only when __name__ == __main__ that is when the script is being called directly 72 | 73 | No return value and Parameters 74 | """ 75 | 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") 76 | 77 | while True: 78 | print (">>> ", end='') 79 | entry = input() 80 | if entry == "quit": 81 | break 82 | if not entry.isdigit(): 83 | print ("You did not enter a number. Try again") 84 | else: 85 | print (getValueOfPi(int(entry))) 86 | 87 | if __name__=='__main__': 88 | shell() 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##PythonPi 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 here. 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. MIT Licensed. 40 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 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.2', 11 | author = 'Pradipta Bora', 12 | author_email = 'pradd@outlook.com', 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 | ) --------------------------------------------------------------------------------