Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Bailey-Borwein-Plouffe formula for calculating Pi
N = 15
def calculate_pi():
pi = 0
for k in range(0, N):
dividend = dividend_for_k(k)
divider = pow(16, k)
pi += dividend/divider
return pi
def dividend_for_k(k):
k = float(k)
dividend = 4/(8*k + 1)
dividend -= 2/(8*k + 4)
dividend -= 1/(8*k + 5)
dividend -= 1/(8*k + 6)
return dividend
def main():
print("The program will return Pi with given precision (rounded):", end=" ")
precision = input()
while int(precision) < 1 or int(precision) > 15:
print("Only [1, 15] numbers are acceptable.")
print("The program will return Pi with given precision (rounded):", end=" ")
precision = input()
format_string = '%.' + str(precision) + 'f'
print(format_string % calculate_pi())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.