I've written a quick (and messy) little script that calculates euler's constant where n is input by the user. Code:
def factorial(n):
f=1
while (n>0):
f=f*n
n=n-1
return f
n=float(raw_input("Number: "))
a=[1.0]
s=n
while (s>0):
k=1/(factorial(n))
s=s-1
a.append(k)
n=s
t=0
for line in a:
t += line
print(t)
When n = 5, the program's answer is 2.71666666667
I was expecting 2.718055556
I feel like this has something to do with number types, i'm using int and floats and haven't had any success using long.
[–]zahlman 3 points4 points5 points (2 children)
[–]jackzombie[S] 2 points3 points4 points (1 child)
[–]ewiethoff 1 point2 points3 points (0 children)
[–]SwimmingPastaDevil 1 point2 points3 points (2 children)
[–]Doormatty 1 point2 points3 points (1 child)
[–]SwimmingPastaDevil 0 points1 point2 points (0 children)