Advertisement
zeromega64twenty

steve pi solver to any number

Dec 18th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | Software | 0 0
  1. import threading
  2. import time
  3. import decimal
  4. from decimal import Decimal, getcontext
  5.  
  6. def pi_chudnovsky(digits):
  7.     getcontext().prec = digits + 1
  8.     K = Decimal(6)
  9.     M = Decimal(1)
  10.     L = Decimal(13591409)
  11.     X = Decimal(1)
  12.     S = Decimal(13591409)
  13.  
  14.     def worker(K, M, L, X, S):
  15.         M *= 2 * (k * 2 - 1) * (k * 2 - 1)
  16.         L += 545140134
  17.         X *= -262537412640768000
  18.         S += M * L / X
  19.  
  20.     threads = []
  21.     for k in range(1, digits // 14 + 1):
  22.         thread = threading.Thread(target=worker, args=(k, M, L, X, S))
  23.         thread.start()
  24.         threads.append(thread)
  25.  
  26.     for thread in threads:
  27.         thread.join()
  28.  
  29.     pi = 426880 * decimal.Decimal(10005).sqrt() / S
  30.     return str(pi)[:digits + 1]
  31.  
  32. print("Calculating pi to 1,000,000 decimal places using the Chudnovsky algorithm...")
  33. print("This will take a while")
  34. print("---------------------------------------------------------------------")
  35.  
  36. start_time = time.time()
  37. result = pi_chudnovsky(1000000)
  38. end_time = time.time()
  39.  
  40. print("---------------------------------------------------------------------")
  41. print(f"The first 1,000,000 digits of pi are: {result}")
  42. print(f"It took {end_time - start_time} seconds to calculate.")
  43. print("Hooray for math")
Tags: math pi Solver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement