Skip to content

Instantly share code, notes, and snippets.

@Premx
Created December 24, 2015 11:18
Show Gist options
  • Save Premx/d0f718cc5bbb9c87cceb to your computer and use it in GitHub Desktop.
Save Premx/d0f718cc5bbb9c87cceb to your computer and use it in GitHub Desktop.
#Nur Tabs
from time import *
from _thread import start_new_thread
t1 = clock()
t2 = clock()
def exact(precision):
t1 = clock()
terms = 1
if(precision == 3-1):
terms = 2000
if(precision == 4-1):
terms = 135000
if(precision == 5-1):
terms = 376500
if(precision >= 6-1 ):
terms = 1000000000
unverseit = 0
last = 0.0
print("Terms/Start>>>" + str(terms))
while(unverseit < 4):
tmpi = leibniz(terms)
tmp1 = int(tmpi * (10 ** (precision + 1)))
tmp_last1 = int(last * (10 ** (precision + 1)))
if(tmp1 == tmp_last1):
unverseit += 1
print("\n+1 " + "\n---\n" + str(tmpi) + "\n" + str(tmp1) + "\n---\n")
else:
unverseit=0
if(terms % 1000 == 0):
print(str(terms) + "\n" + str(tmp1) + " != " + str(tmp_last1))
terms += 1
last = tmpi
print("\n!-!-!-!\nFinish\n!-!-!-!\n")
print("\nTerms > " + str(terms) + "\nPi > " + str(tmpi))
t2 = clock()
t = t2 - t1
print("%.2fs" % (t))
def leibniz(terms):
result = 0.0
sign = 1.0
for n in range(terms):
result += sign/(2.0*n+1.0)
sign = -sign
return 4 * result
while True:
precision = int(input("Precision: "))
exact(precision - 1)
@Premx
Copy link
Author

Premx commented Dec 24, 2015

It uses the Leibniz algorithm (https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80) to calculate pi. You can enter the precision of pi so for example 3 -> It will calculate to the third place after the comma exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment