A limit statement says that you can get as close as you like. Here you find how far you must go for a given closeness.
What to build
Given a tolerance, find the smallest n for which (1 + 1/n)ⁿ is within the tolerance of e.
Requirements
- Read the tolerance as a decimal.
- Find the smallest whole n of 1 or more with e − (1 + 1/n)ⁿ less than the tolerance.
- Print that n and the value (1 + 1/n)ⁿ.
Input
One line with the tolerance, a decimal from 0.0002 to 1.
Output
Two lines: n N and value V with V to 6 decimal places.
Constraints
The answer n is at most about 14000.
Example
Input:
0.5
Output:
n 2
value 2.250000
Why it works
The sequence increases toward e, so the gap e − (1 + 1/n)ⁿ shrinks as n grows. The gap is about e ÷ (2n), which tells you roughly how large n must be: the smaller the tolerance, the bigger the n.
Examples
0.5
n 2 value 2.250000
0.1
n 13 value 2.620601
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The values (1 + 1/n)ⁿ increase with n, so the first n that is close enough is the answer.
Hint 2
Use `from math import e` and loop with n = 1, 2, 3, … until e − (1 + 1/n)**n is less than epsilon.
Hint 3
Print n, and then the value with 6 decimal places.
Solution
Show one correct solution (try it yourself first)
from math import e
eps = float(input())
n = 1
while e - (1 + 1 / n) ** n >= eps:
n += 1
print("n", n)
print(f"value {(1 + 1 / n) ** n:.6f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step