Skip to content

Python 310 迴圈公式計算

Python TQC

題目說明:

請使用迴圈敘述撰寫一程式,讓使用者輸入正整數n (1 < n),計算以下公式的總和並顯示結果:
\(\frac{1}{\sqrt{1}+\sqrt{2}}+\frac{1}{\sqrt{2}+\sqrt{3}}+\frac{1}{\sqrt{3}+\sqrt{4}}+...+\frac{1}{\sqrt{n-1}+\sqrt{n}}\)

Tip

輸出結果至小數點後四位。

範例輸入
8

範例輸出

1.8284

Solution

1
2
3
4
5
a = eval(input())
SUM = 0
for i in range(1, a):
    SUM += 1/((i**0.5)+(i+1)**0.5)
print("%.4f"%SUM)

Last update : 13 novembre 2024
Created : 13 novembre 2024

Comments

Comments