How can I find the first integer n for which the sum of the numbers below is greater than 2.5?

2 ビュー (過去 30 日間)
gogogo
gogogo 2022 年 2 月 15 日
回答済み: DGM 2022 年 2 月 15 日
1+1/2+1/3+1/4+...+1/n>2.5

回答 (1 件)

DGM
DGM 2022 年 2 月 15 日
Here's one simple way:
kmax = 10;
n = 1:kmax;
s = cumsum(1./n);
[n' s']
ans = 10×2
1.0000 1.0000 2.0000 1.5000 3.0000 1.8333 4.0000 2.0833 5.0000 2.2833 6.0000 2.4500 7.0000 2.5929 8.0000 2.7179 9.0000 2.8290 10.0000 2.9290
kest = n(find(s>2.5,1,'first'))
kest = 7

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by