Last active
February 17, 2020 12:09
-
-
Save Feuermurmel/40f93d369f2b1c69d7df82c6a0f4f81d to your computer and use it in GitHub Desktop.
I wrote this for fun. Slow as hell. Maybe broken. Don't use.
This file contains 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
from math import floor | |
from fractions import Fraction as F | |
def pi_approx(bits): | |
def term(k): | |
return F(1, 16 ** k) * (F(4, 8 * k + 1) - F(2, 8 * k + 4) - F(1, 8 * k + 5) - F(1, 8 * k + 6)) | |
return sum(term(k) for k in range(bits // 4 + 1)) | |
def pi_decimal_digits(n): | |
denom = 10 ** n | |
p = pi_approx(denom.bit_length()) | |
int_part = floor(p) | |
fract_part = p - int_part | |
return str(int_part) + '.' + str(floor(fract_part * denom)).ljust(n, '0') | |
print(pi_decimal_digits(1)) | |
print(pi_decimal_digits(2)) | |
print(pi_decimal_digits(5)) | |
print(pi_decimal_digits(1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment