|
|
import decimal |
|
|
from decimal import Decimal, getcontext |
|
|
|
|
|
|
|
|
getcontext().prec = 110 |
|
|
|
|
|
|
|
|
c = 299792458 |
|
|
h = Decimal('6.62607015e-34') |
|
|
k_B = Decimal('1.38e-23') |
|
|
TSR = Decimal(c**2) / k_B |
|
|
Q = Decimal('2') ** (Decimal('1') / Decimal('12')) |
|
|
|
|
|
def chudnovsky_pi(n_terms): |
|
|
C = Decimal(426880) * Decimal(10005).sqrt() |
|
|
K = Decimal(6) |
|
|
M = Decimal(1) |
|
|
X = Decimal(1) |
|
|
L = Decimal(13591409) |
|
|
S = Decimal(0) |
|
|
for i in range(1, n_terms + 1): |
|
|
if i == 1: |
|
|
M = Decimal(1) |
|
|
else: |
|
|
divisor = Decimal((i-1)**3) |
|
|
if divisor == Decimal(0): |
|
|
divisor = Decimal(1) |
|
|
M = (K**3 - Decimal(16)*K) * M // divisor |
|
|
L += Decimal(545140134) |
|
|
X *= Decimal(-262537412640768000) |
|
|
term = Decimal(M * L) / X |
|
|
|
|
|
term *= Q ** (Decimal(i) / Decimal(n_terms)) |
|
|
|
|
|
if term < Decimal('1e-15'): |
|
|
TSR_rel = TSR |
|
|
ΔTSR_q = Decimal('0') |
|
|
else: |
|
|
TSR_rel = TSR / Decimal((1 - (term**2 / Decimal(c**2))).sqrt()) |
|
|
ΔTSR_q = h * term |
|
|
S += term * TSR_rel + ΔTSR_q |
|
|
K += Decimal(12) |
|
|
pi = C / S |
|
|
return +pi |
|
|
|
|
|
|
|
|
pi = chudnovsky_pi(100) |
|
|
print(f"π ≈ {pi}") |
|
|
|
|
|
|
|
|
import json |
|
|
with open('pi_simulation_results.json', 'w') as f: |
|
|
json.dump({"pi_approx": str(pi)}, f) |
|
|
|