Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created March 11, 2015 01:36
Show Gist options
  • Save 1995eaton/f35b50ab6dcff9630a3a to your computer and use it in GitHub Desktop.
Save 1995eaton/f35b50ab6dcff9630a3a to your computer and use it in GitHub Desktop.
E calculator
from gmpy2 import mpz, get_context
# 19 loops maxes out 64-bit floating point number
# ~~ 2.718281828459045 == math.e ~~
def E(prec=19):
num, den = 0, 1
for i in range(1, prec):
# add fraction keeping GCD
# allows for greater precision
num = (num + 1) * i
den *= i
return num / den
def gmp_E(prec=19):
num, den = mpz(0), mpz(1)
for i in range(1, prec):
num = (num + 1) * i
den *= i
return num / den
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment