I was trying to solve a problem which stated:
Calculate the first 10 digit prime found in consecutive digits of e.
I was able to solve the problem but I did it by using some 10k digits of e available online. So I tried to write a program which calculates digits of e. The problem is that it simply gives the incorrect answer.
The code and the formula I used are:
$$e = \sum\limits_{n=0}^{\infty}\frac{1}{n!} = \frac{1}{1} + \frac{1}{1} + \frac{1}{1 \cdot 2} + \frac{1}{1\cdot 2 \cdot 3} + \cdots$$
import math
e=0
x=int(input()) #larger this number, more will be the digits of e
for i in range(x):
e+=(1/(math.factorial(i)))
print(e)
When the user inputs 10, the digits returned are 2.7182815255731922 which is not correct.
Can someone explain why my code does not produce the correct result?
(1+9^-(4^6*7))^3^2^85
which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot. \$\endgroup\$[floating-point]
and[numerical-methods]
. The existing answer would still fit the question on SO with those tags. \$\endgroup\$