求数列 12、105、1008、10011、…
的 n 项之和
原文:https://www . geesforgeks . org/find-the-sum-n-terms-of-series-12-105-1008-10011/
给定正整数 n 。求级数第一个 n 项的和
12,105,1008,10011,…..
示例:
输入:n = 4 T3】输出: 11136
输入:n = 7 T3】输出:111111187
进场:
该序列通过使用以下模式形成。对于任何数值 N-
上述解决方案可以通过一系列步骤得出:
给定系列-
12 + 105 + 1008 + 10011 +…….
10 + 2 + 100 + 5 + 1000 + 8 + 10000 + 11 +…… ..
(10 + 100 + 1000 + 10000+……) + (2 + 5 + 8 + 11+……) -(1)
上式中第一项为几何级数,第二项为算术级数。
G.P. = 其中 a 是第一项 a,r 是公比,n 是项数。
A.P. = 其中 a 是第一个术语 a,a 是共同的区别,n 是术语的数量。
所以在代入方程 G.P .和 A.P .中的值,并代入方程(1)中相应的方程后,我们得到,
所以,
插图:
输入: n = 4 输出: 11136 解释:
这就给出了 ans 11136。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// Function to return sum of
// N term of the series
ll findSum(ll n)
{
ll x = 10 * (pow(10, n) - 1) / 9;
ll y = n * (3 * n + 1) / 2;
return x + y;
}
// Driver Code
int main()
{
ll n = 4;
cout << findSum(n);
return 0;
}
Output
11136
时间复杂度: O(1)
辅助空间: O(1)
版权属于:月萌API www.moonapi.com,转载请注明出处