how to find which formula is closer to euler
import math
n = abs(int(input("enter a number: ")))
e = math.e
first = (1 1/n)**n
second = (1- 1/n)**n
if second == 1/e or second < 1/e:
print("second is closest to e")
elif first == e or first < e:
print("first is closest to e")
else:
print("tie")
without programming
first = e = (1 1/n)^n
first = 2.7048138294215285
second = 1/e = (1 - 1/n)^n
second = 2.7319990264290284
so first is closer to Euler but my output always is give me second why?
forgot to mention n is 100
CodePudding user response:
That is because your checks are not right. second
is 0.3660323412732292
and 1/e
is 0.36787944117144233
, so second < 1/e
is in fact True
but that doesn't mean that second
is closer to e
than first
. Instead, you should compare actual distances:
dist_first = (first - e) ** 2
dist_second = (1 / second - e) ** 2
and then compare them:
if dist_first < dist_second:
print("First is closer.")
elif dist_second < dist_first:
print("Second is closer.")
else:
print("Draw.")