[英]closest output to euler python
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第一个 = e = (1+1/n)^n
first = 2.7048138294215285第一个 = 2.7048138294215285
second = 1/e = (1 - 1/n)^n秒 = 1/e = (1 - 1/n)^n
second = 2.7319990264290284第二 = 2.7319990264290284
so first is closer to Euler but my output always is give me second why?所以第一个更接近欧拉,但我的 output 总是给我第二个为什么?
forgot to mention n is 100忘了说n是100
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
. second
是0.3660323412732292
和1/e
是0.36787944117144233
,所以second < 1/e
实际上是True
,但这并不意味着second
比first
更接近e
。 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.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.