Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import time
- import decimal
- from decimal import Decimal, getcontext
- def pi_chudnovsky(digits):
- getcontext().prec = digits + 1
- K = Decimal(6)
- M = Decimal(1)
- L = Decimal(13591409)
- X = Decimal(1)
- S = Decimal(13591409)
- def worker(K, M, L, X, S):
- M *= 2 * (k * 2 - 1) * (k * 2 - 1)
- L += 545140134
- X *= -262537412640768000
- S += M * L / X
- threads = []
- for k in range(1, digits // 14 + 1):
- thread = threading.Thread(target=worker, args=(k, M, L, X, S))
- thread.start()
- threads.append(thread)
- for thread in threads:
- thread.join()
- pi = 426880 * decimal.Decimal(10005).sqrt() / S
- return str(pi)[:digits + 1]
- print("Calculating pi to 1,000,000 decimal places using the Chudnovsky algorithm...")
- print("This will take a while")
- print("---------------------------------------------------------------------")
- start_time = time.time()
- result = pi_chudnovsky(1000000)
- end_time = time.time()
- print("---------------------------------------------------------------------")
- print(f"The first 1,000,000 digits of pi are: {result}")
- print(f"It took {end_time - start_time} seconds to calculate.")
- print("Hooray for math")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement