📌  相关文章
📜  找出无穷级数的总和1 ^ 2.x ^ 0 + 2 ^ 2.x ^ 1 + 3 ^ 2.x ^ 2 + 4 ^ 2.x ^ 3 +……。

📅  最后修改于: 2021-04-26 06:40:05             🧑  作者: Mango

给定一个无穷级数和一个值x,任务是求和。以下是无限级数

例子:

Input: x = 0.5
Output: 12

Input: x = 0.9
Output: 1900

方法:
尽管给定的级数不是算术几何级数,但是区别在于(2^2-1^2), (3^2-2^2), ...  等等,形成一个AP。因此,我们可以使用差异方法。
Let\: S = 1 + 4x + 9x^2 + 16x^3 + ...\infty \\\\ Multiply\: both\: sides\: with\: common\: ratio\: x\: of\: the\: GP(geometric progression).\\ S_x = x + 4x^2 + 9x^3 + ...\infty \\ \\ Now, \: subtract\: the\: two\: equations.\\ => (1-x)S = 1 + 3x + 5x^2 + 7x^3 + ...\infty \null\hfill (1)\\\\ Now, \: let\: R = 1 + 3x + 5x^2 + 7x^3 + ...\infty, \: which\: is\: an \:Arithmetico-Geometric\: series\: with \:a=1, \: d=2 \:and \:r=x.\\ For an A.G.P., $Sum \:R \:= \frac{a}{1-r} + \frac{rd}{(1-r)^2} \\ Substituting\: the \:values, \:we\: get\: R = \frac{1+x}{(1-x)^2} \\ Substitute\: R\: in\: (1), \:we\: get, (1-x)S=\frac{1+x}{(1-x)^2} \\ => S= \frac{1+x}{(1-x)^3}$
因此,总和为(1 + x)/(1-x)^ 3。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
#include 
 
using namespace std;
 
// Function to calculate sum
double solve_sum(double x)
{
    // Return sum
    return (1 + x) / pow(1 - x, 3);
}
 
// Driver code
int main()
{
    // declaration of value of x
    double x = 0.5;
 
    // Function call to calculate
    // the sum when x=0.5
    cout << solve_sum(x);
 
    return 0;
}


Java
// Java Program to find
//sum of the given infinite series
import java.util.*;
 
class solution
{
static double calculateSum(double x)
{
     
// Returning the final sum
return (1 + x) / Math.pow(1 - x, 3);
 
}
 
//Driver code
public static void main(String ar[])
{
     
  double x=0.5;
  System.out.println((int)calculateSum(x));
 
}
}
//This code is contributed by Surendra_Gangwar


Python
# Python implementation of above approach
 
# Function to calculate sum
def solve_sum(x):
    # Return sum
    return (1 + x)/pow(1-x, 3)
 
# driver code
 
# declaration of value of x
x = 0.5
 
# Function call to calculate the sum when x = 0.5
print(int(solve_sum(x)))


C#
// C# Program to find sum of
// the given infinite series
using System;
 
class GFG
{
static double calculateSum(double x)
{
     
// Returning the final sum
return (1 + x) / Math.Pow(1 - x, 3);
 
}
 
// Driver code
public static void Main()
{
    double x = 0.5;
    Console.WriteLine((int)calculateSum(x));
}
}
 
// This code is contributed
// by inder_verma..


PHP


Javascript


输出:
12