Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +....... - GeeksforGeeks

22 Mar, 2021
Given an infinite series and a value x, the task is to find its sum. Below is the infinite series  
1^2*x^0 + 2^2*x^1 + 3^2*x^2 + 4^2*x^3 +....... upto infinity, where x belongs to (-1, 1)
Examples:  
Input: x = 0.5
Output: 12

Input: x = 0.9
Output: 1900
 
Approach:Though the given series is not an Arithmetico-Geometric series, however, the differences and so on, forms an AP. So, we can use the Method of Differences.Hence, the sum will be (1+x)/(1-x)^3.Below is the implementation of above approach:  
C++
Java
Python
C#
PHP
Javascript
// C++ implementation of above approach#include <iostream>#include <math.h> using namespace std; // Function to calculate sumdouble solve_sum(double x){    // Return sum    return (1 + x) / pow(1 - x, 3);} // Driver codeint 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 Program to find//sum of the given infinite seriesimport java.util.*; class solution{static double calculateSum(double x){     // Returning the final sumreturn (1 + x) / Math.pow(1 - x, 3); } //Driver codepublic static void main(String ar[]){       double x=0.5;  System.out.println((int)calculateSum(x)); }}//This code is contributed by Surendra_Gangwar
# Python implementation of above approach # Function to calculate sumdef solve_sum(x):    # Return sum    return (1 + x)/pow(1-x, 3) # driver code # declaration of value of xx = 0.5 # Function call to calculate the sum when x = 0.5print(int(solve_sum(x)))
// C# Program to find sum of// the given infinite seriesusing System; class GFG{static double calculateSum(double x){     // Returning the final sumreturn (1 + x) / Math.Pow(1 - x, 3); } // Driver codepublic static void Main(){    double x = 0.5;    Console.WriteLine((int)calculateSum(x));}} // This code is contributed// by inder_verma..
<?php// PHP implementation of// above approach // Function to calculate sumfunction 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.5echo solve_sum($x); // This code is contributed// by inder_verma?>
<script>// javascript Program to find//sum of the given infinite series  function calculateSum(x){     // Returning the final sumreturn (1 + x) / Math.pow(1 - x, 3); } //Driver code  var x=0.5;document.write(parseInt(calculateSum(x))); // This code is contributed by 29AjayKumar </script>
12
 
SURENDRA_GANGWAR
inderDuMCA
29AjayKumar
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python