Created
April 14, 2015 16:09
-
-
Save dylnmc/b0f9407014563db46416 to your computer and use it in GitHub Desktop.
Pi py - simple pi calculation in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
__author__ = "dylnmc" | |
def main(): | |
from decimal import Decimal, getcontext | |
from os import nice | |
getcontext().prec = 100 | |
two = Decimal("2.0") | |
p = two # product | |
n = two # numerator | |
d = Decimal("3.0") # denominator | |
count = 0 | |
try: | |
while True: | |
p *= n / d | |
if count % 2 == 0: | |
n += two | |
else: | |
d += two | |
count += 1 | |
if count % 1000 == 0: | |
print "\x1b[0;0Hpi: ", p * two | |
except (KeyboardInterrupt, SystemExit, EOFError): | |
print "\nThat's enough pi for now!" | |
print "loops:", count | |
raise SystemExit | |
if __name__ == "__main__": | |
print "\x1bc\x1b[?25l" | |
try: | |
main() | |
except SystemExit: | |
pass | |
finally: | |
print "\x1b[?25h" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment