如何找到哪個公式更接近歐拉
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")
無需編程
第一個 = e = (1 1/n)^n
第一個 = 2.7048138294215285
秒 = 1/e = (1 - 1/n)^n
第二 = 2.7319990264290284
所以第一個更接近歐拉,但我的輸出總是給我第二個為什么?
忘了說 n 是 100
uj5u.com熱心網友回復:
那是因為你的支票不對。secondis0.3660323412732292和1/eis 0.36787944117144233,所以second < 1/e實際上是,True 但這并不意味著second更接近ethan first。相反,您應該比較實際距離:
dist_first = (first - e) ** 2
dist_second = (1 / second - e) ** 2
然后比較它們:
if dist_first < dist_second:
print("First is closer.")
elif dist_second < dist_first:
print("Second is closer.")
else:
print("Draw.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382600.html
