Created
April 1, 2024 05:42
-
-
Save x1unix/065c1451c97821d12042760bebecdc84 to your computer and use it in GitHub Desktop.
y-cruncher
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
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) | |
print(chudnovsky(2)) # 3.141592653589793238462643384 | |
decimal.getcontext().prec = 100 | |
for n in range(2,10): | |
print(f"{n=} {chudnovsky(n)}") # 3.14159265358979323846264338... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment