Skip to content

Instantly share code, notes, and snippets.

@Cvar1984
Created November 19, 2024 10:57
Show Gist options
  • Save Cvar1984/7069b62bf3de13d3c0312511fb5508d3 to your computer and use it in GitHub Desktop.
Save Cvar1984/7069b62bf3de13d3c0312511fb5508d3 to your computer and use it in GitHub Desktop.
import decimal
def binary_split(a, b):
if b == a + 1:
Pab = -(6*a - 5)*(2*a - 1)*(6*a - 1)
Qab = 10939058860032000 * a**3
Rab = Pab * (545140134*a + 13591409)
else:
m = (a + b) // 2
Pam, Qam, Ram = binary_split(a, m)
Pmb, Qmb, Rmb = binary_split(m, b)
Pab = Pam * Pmb
Qab = Qam * Qmb
Rab = Qmb * Ram + Pam * Rmb
return Pab, Qab, Rab
def chudnovsky(n):
"""Chudnovsky algorithm."""
P1n, Q1n, R1n = binary_split(1, n)
return (426880 * decimal.Decimal(10005).sqrt() * Q1n) / (13591409*Q1n + R1n)
decimal.getcontext().prec = 20 # number of digits of decimal precision
print(f"{chudnovsky(2)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment