在 C++ 中求和級數 0.7、0.77、0.777……至 n 項


在此問題中,我們給定了一個數字的 n 項。此級數為 0.7、0.77、0.777……我們的任務是建立一個程式來求和級數 0.7、0.77、0.777 …至 n 項。

讓我們舉個例子來了解此問題:

輸入  4

輸出  

說明:0.7 + 0.77 + 0.777 + 0.7777 = 3.0247

為了解決此問題,我們將匯出級數求和公式。讓我們找到它的通式:

sum = 0.7 + 0.77 + 0.777 + ... upto n terms
sum = 7 (0.1 + 0.11 + 0.111 + … upto n terms)
sum = 7 (9/9)(0.1 + 0.11 + 0.111 + … upto n terms)
sum = 7/9(0.9 + 0.99 + 0.999 + … upto n terms)
sum = 7/9 ( (1 - 0.1) + (1 - 0.01) + (1 - 0.001) + … upto n terms )
sum = 7/9 ( (1+ 1 + 1 + … + upto n) - (0.1 + 0.01 + 0.001 + … upto n terms)
)
sum = 7/9 ( (n) - (1/10 + 1/100 + 1/1000 + … upto n terms) )
sum = 7/9 ( n - 0.1 * ((1 - (0.1)n)/ (1 - 0.1)) )
sum = 7/9 ( n - 0.1 * ((1 - (0.1)n)/ (0.9)) )
sum = 7/9 ( n - ((1 - (1/10n) )/9) )
sum = 7/81 ( 9n - (1 - (1/10n) ) )
sum = 7/81 (9n - 1 + 10-n)

此公式給出了級數求和(至 n 項)的通式。

示例

程式,演示我們的解決方案的工作原理:

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
float calcSeriesSum(int n) {
   return ( (.08641) * (9*n - 1) + pow(10, (-1) * n) );
}
int main() {
   int n = 5;
   cout<<"The sum of series 0.7, 0.77, 0.777, ... upto n terms is "<<calcSeriesSum(n);
   return 0;
}

輸出

The sum of series 0.7, 0.77, 0.777, ... upto n terms is 3.80205

更新於:2020 年 8 月 14 日

171 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.