Created
November 14, 2015 21:21
-
-
Save Jonarzz/31dc509430c3ebee22a2 to your computer and use it in GitHub Desktop.
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
# 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