Dada una serie infinita y un valor x, la tarea es encontrar su suma. A continuación se muestra la serie infinita.
1^2*x^0 + 2^2*x^1 + 3^2*x^2 + 4^2*x^3 +……. hasta el infinito, donde x pertenece a (-1, 1)
Ejemplos:
Input: x = 0.5 Output: 12 Input: x = 0.9 Output: 1900
Enfoque:
aunque la serie dada no es una serie aritmético-geométrica, sin embargo, las diferencias , etc., forman un AP. Entonces, podemos usar el Método de las Diferencias.
Por lo tanto, la suma será (1+x)/(1-x)^3.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <iostream> #include <math.h> 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
<?php // PHP implementation of // above approach // Function to calculate sum function 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 echo solve_sum($x); // This code is contributed // by inder_verma ?>
Javascript
<script> // javascript Program to find //sum of the given infinite series function calculateSum(x) { // Returning the final sum return (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>
Producción:
12