I have made a python program to calculate e and it worked up until a while ago but now all of the sudden it doesn't work anymore and is behaving very oddly. Here is the source code:
from decimal import *
import time
dig = int(input('Enter number of digits: '))
getcontext().prec = dig +5
def factorial(n):
num = Decimal(1)
while Decimal(n) >= Decimal(1):
num = Decimal(num)*Decimal(n)
n = Decimal(n)-Decimal(1)
return Decimal(num)
def calce(n):
def calce(n):
e = 0
for i in range(0, n+1):
e = e + Decimal((Decimal(2)*Decimal(i)+Decimal(2)))/Decimal(factorial(Decimal(2)*Decimal(i)+1))
return Decimal(e)
n = int(input('How many iterations for e?: '))
t0= time.clock()
numb = str(calce(n))
numb = numb[:-4]
f = open("edigits.txt", 'w')
f.write(str(dig))
f.write(" digits.")
f.write("\n")
f.write(str(n))
f.write("!\n\n")
f.write(numb)
t= str(time.clock() - t0)
f.seek(0, 0)
f.write(t)
f.write(" seconds.\n\n")
f.close()
It even works when I don't write it to a file but when I do this it only gives the time elapsed and doesn't give anything else and sometimes it might give this random number too but that is it... Any help?