在C++中求级数√3 + √12 + ... 的前N项和
在这个问题中,我们给定一个整数N。我们的任务是 *求级数√3 + √12 + ... 的前n项和*。
这个级数是 √3+√12+√27+√48+...
即,这是一个平方根的级数。
让我们通过一个例子来理解这个问题:
Input : N = 3 Output : 10.3922
**解释** −
√3+√12+√27=1.7320+3.4641+5.1961=10.3922
解题方法
解决这个问题的一个简单方法是找到级数的通项,然后求出前n项的和。使用公式计算和可以将时间复杂度降低到O(1)。
级数为:
√3+√12+√27+√48+...
这里,所有项都有 √3 。将其作为公因式提出,我们得到:
⇒√3(√1+√4+√9+√16+⋯)
⇒√3(1+2+3+4+⋯)
因此,通项为:
Tn=n∗√3
利用这个公式,我们可以求出级数的前n项和:
Sum=∑n∗√3
Sum=√3∗∑n
Sum=√3∗n∗(n+1)2
示例
程序演示了解决方案的工作原理
#include<iostream> #include<math.h> using namespace std; float calcSumNTerms(float n) { return ((sqrt(3)) * ((n*(n+1))/2)); } int main() { float n = 25; cout<<"The sum of series upto n terms is "<<calcSumNTerms(n); return 0; }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
The sum of series upto n terms is 562.917
广告