Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ … + x^n(x^n+y^n)

Given a series x(x+y)+x^{2}(x^{2}+y^{2})+...+x^{n}(x^{n}+y^{n}) where x, y and n take integral values. The task is to Find the sum till nth term of the given series.

Examples:

Input: x = 2, y = 2, n = 2
Output: 40

Input: x = 2, y = 4, n = 2
Output: 92

Approach: Given series is :

S=x(x+y)+x^{2}(x^{2}+y^{2})+x^{3}(x^{3}+y^{3})+...+x^{n}(x^{n}+y^{n}).
=(x^{2}+xy)+(x^{4}+x^{2}y^{2})++(x^{6}+x^{3}y^{3})+...++(x^{2n}+x^{n}y^{n})
=(x^{2}+x^{4}+x^{6}+...+x^{2n})+(xy+x^{2}y^{2}+x^{3}y^{3}+...+x^{n}y^{n})
=(\frac{x^{2}((x^{2})^{n}-1)}{x^{2}-1})+(\frac{xy(x^{n}y^{n}-1)}{xy-1})
Thus our problem reduces to finding sum of two GP series.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the sum of series
#include <bits/stdc++.h>
using namespace std;
  
// Function to return required sum
int sum(int x, int y, int n)
{
  
    // sum of first series
    int sum1 = (pow(x, 2) * (pow(x, 2 * n) - 1))
               / (pow(x, 2) - 1);
  
    // sum of second series
    int sum2 = (x * y * (pow(x, n) * pow(y, n) - 1))
               / (x * y - 1);
  
    return sum1 + sum2;
}
  
// Driver Code
int main()
{
    int x = 2, y = 2, n = 2;
  
    // function call to print sum
    cout << sum(x, y, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum of series 
  
public class GFG {
      
    // Function to return required sum 
    static int sum(int x, int y, int n) 
    
      
        // sum of first series 
        int sum1 = (int) (( Math.pow(x, 2) * (Math.pow(x, 2 * n) - 1)) 
                   / (Math.pow(x, 2) - 1)); 
      
        // sum of second series 
        int sum2 = (int) ((x * y * (Math.pow(x, n) * Math.pow(y, n) - 1))
                    / (x * y - 1)); 
      
        return sum1 + sum2; 
    
      
    // Driver code 
    public static void main (String args[]){
        int x = 2, y = 2, n = 2
          
        // function call to print sum 
        System.out.println(sum(x, y, n)); 
    }
  
// This code is contributed by ANKITRAI1
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the sum of series 
# Function to return required sum 
  
def sum(x,y,n):
      
    # sum of first series 
    sum1 = ((x**2)*(x**(2*n)-1))//(x**2 - 1)
      
    # sum of second series
    sum2 = (x*y*(x**n*y**n-1))//(x*y-1)
    return (sum1+sum2)
      
# Driver Code 
if __name__=='__main__':
    x = 2
    y = 2
    n = 2
# function call to print sum 
    print(sum(x, y, n))
      
# this code is contributed by sahilshelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the sum of series 
using System;
  
class GFG 
{
  
// Function to return required sum 
static int sum(int x, int y, int n) 
  
    // sum of first series 
    int sum1 = (int) ((Math.Pow(x, 2) * 
                      (Math.Pow(x, 2 * n) - 1)) /
                      (Math.Pow(x, 2) - 1)); 
  
    // sum of second series 
    int sum2 = (int) ((x * y * (Math.Pow(x, n) *
                Math.Pow(y, n) - 1)) / (x * y - 1)); 
  
    return sum1 + sum2; 
  
// Driver code 
public static void Main ()
{
    int x = 2, y = 2, n = 2; 
      
    // function call to print sum 
    Console.Write(sum(x, y, n)); 
}
}
  
// This code is contributed by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the 
// sum of series
  
// Function to return required sum
function sum($x, $y, $n)
{
    //sum of first series
    $sum1 = (pow($x, 2) * 
            (pow($x, 2 * $n) - 1)) / 
            (pow($x, 2) - 1);
  
    // sum of second series
    $sum2 = ($x * $y * (pow($x, $n) * 
                        pow($y, $n) - 1)) / 
                           ($x * $y - 1);
  
    return $sum1 + $sum2;
}
  
// Driver code
$x = 2;
$y = 2;
$n = 2;
  
// function call to print sum
echo sum($x, $y, $n);
  
// This code is contributed 
// by Shashank_Sharma
?>

chevron_right


Output:

40

Time Complexity: O(log(n))



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.