Skip to content

Commit 9c708ec

Browse files
committed
finding e to nth digit
1 parent 31d5aeb commit 9c708ec

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

n_digit_e.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# find e to nth digit by brothers' formulae: http://www.intmath.com/exponential-logarithmic-functions/calculating-e.php
2+
import decimal
3+
4+
5+
def factorial(n):
6+
factorials = [1]
7+
for i in range(1, n + 1):
8+
factorials.append(factorials[i - 1] * i)
9+
return factorials
10+
11+
12+
def compute_e(n):
13+
decimal.getcontext().prec = n + 1
14+
e = 2
15+
factorials = factorial(2 * n + 1)
16+
for i in range(1, n + 1):
17+
counter = 2 * i + 2
18+
denominator = factorials[2 * i + 1]
19+
e += decimal.Decimal(counter / denominator)
20+
return e
21+
22+
23+
while True:
24+
n = int(input("Please type number between 0-1000"))
25+
if n >= 0 and n <= 1000:
26+
break
27+
28+
print(str(compute_e(n))[:n + 1])

0 commit comments

Comments
 (0)