Open In App

Find sum of series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+… till 3N terms

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the sum of the below series till 3N terms.

1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+… till 3N terms

Examples:

Input: N = 2
Output: 17

Input: N = 3
Output: 56

 

Naive Approach: 

If we observe clearly then we can divide it into a grouping of 3 terms having N no. of groups.

1 to 3 term = 1^3 +1^2 +1 = 3

4 to 6 term = 2^3+2^2+2 = 14

7 to 9 term = 3^3+3^2+ 3 = 39

.

.

(3N-2) to 3N term = N^3+N^2+ N

Below steps can be used to solve the problem-

  • For each iterative i, calculate (i^3+i^2+i).
  • And add the calculated value to sum (Initially the sum will be 0).
  • Return the final sum.

Below is the implementation of the above approach:

C++

// C++ program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the sum
// upto 3Nth term of the series
int seriesSum(int N)
{
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
        // Adding current calculated value
        // to sum
        sum += (pow(i, 3) + pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << seriesSum(N);
    return 0;
}

                    

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
     
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
       
      // Adding current calculated value
      // to sum
      sum += (Math.pow(i, 3) + Math.pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int N = 5;
    System.out.print(seriesSum(N));
  }
}
 
// This code is contributed by hrithikgarg03188

                    

Python3

# Python code for the above approach
 
# Function to return the sum
# upto 3Nth term of the series
def seriesSum(N):
   
    # Initial value of the sum
    sum = 0;
 
    # Loop to iterate from 1 to N
    for i in range(1, N + 1):
        # Adding current calculated value
        # to sum
        sum += (i ** 3) + (i ** 2) + i;
 
    # Return the sum upto 3Nth term
    return sum;
 
# Driver Code
 
# Get the value of N
N = 5;
print(seriesSum(N));
 
# This code is contributed by Saurabh Jaiswal

                    

C#

// C# program for the above approach
using System;
class GFG {
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
     
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
       
      // Adding current calculated value
      // to sum
      sum += ((int)Math.Pow(i, 3) + (int)Math.Pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
  }
 
  // Driver Code
  public static void Main ()
  {
    int N = 5;
    Console.Write(seriesSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

                    

Javascript

<script>
      // JavaScript code for the above approach
 
      // Function to return the sum
      // upto 3Nth term of the series
      function seriesSum(N) {
          // Initial value of the sum
          let sum = 0;
 
          // Loop to iterate from 1 to N
          for (let i = 1; i <= N; i++) {
              // Adding current calculated value
              // to sum
              sum += (Math.pow(i, 3) + Math.pow(i, 2) + i);
          }
 
          // Return the sum upto 3Nth term
          return sum;
      }
 
      // Driver Code
 
      // Get the value of N
      let N = 5;
      document.write(seriesSum(N));
 
       // This code is contributed by Potta Lokesh
  </script>

                    

Output
295

Time Complexity: O(N)

Auxiliary Space: O(1), since no extra space has been taken.


Efficient Approach:

From the given series, find the formula for the 3Nth term:

The given Series

1^{3}+1^{2}+1+2^{3}+2^{2}+2+3^{3}+3^{2}+3+....+till 3N terms

This can be written as-

(1^{3}+2^{3}+3^{3}+....+N^{3})+(1^{2}+2^{2}+3^{2}+....+N^{2})+(1+2+3+....+N)                                -(1)

The above three equations are in A.P., hence can be written as-

(\frac{N*(N+1)}{2})^{2}+\frac{(N*(N+1)*(2*N+1))}{6}+\frac{(N*(N+1))}{2}

\frac{(3*N^{2}*(N+1)^{2})+(2*N*(N+1)*(2*N+1))+(6*N*(N+1))}{12}

\frac{(N*(N+1)*(3*N^{2}+7*N+8))}{12}

= N*(N+1)*(3*N^2+7*N+8)/12

So, the sum of the series till 3Nth term can be generalized as:

\frac{(N*(N+1)*(3*N^{2}+7*N+8))}{12}

C++

// C++ program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the sum
// upto 3Nth term of the series
int seriesSum(int N)
{
    return N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << seriesSum(N);
    return 0;
}

                    

Java

// Java program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
import java.util.*;
public class GFG
{
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
    return N * (N + 1) * (3 * (int)Math.pow(N, 2) + 7 * N + 8) / 12;
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Get the value of N
    int N = 5;
    System.out.print(seriesSum(N));
  }
}
 
// This code is contributed by Samim Hosdsain Mondal.

                    

Python

# Python program to find the sum of the
# series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
# till 3N terms
import math
 
# Function to return the sum
# upto 3Nth term of the series
def seriesSum(N):
 
    return math.floor(N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12)
 
# Driver Code
 
# Get the value of N
N = 5
print(seriesSum(N))
 
# This code is contributed by Samim Hossain Mondal

                    

C#

// C# program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
using System;
class GFG
{
   
// Function to return the sum
// upto 3Nth term of the series
static int seriesSum(int N)
{
    return N * (N + 1) * (3 * (int)Math.Pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
public static void Main()
{
   
    // Get the value of N
    int N = 5;
    Console.Write(seriesSum(N));
}
}
 
// This code is contributed by Samim Hosdsain Mondal.

                    

Javascript

<script>
    // JavaScript program to find the sum of the
    // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
    // till 3N terms
 
    // Function to return the sum
    // upto 3Nth term of the series
    const seriesSum = (N) => parseInt(N * (N + 1) * (3 * Math.pow(N, 2) + 7 * N + 8) / 12)
 
    // Driver Code
    // Get the value of N
    let N = 5;
    document.write(seriesSum(N));
 
// This code is contributed by rakeshsahni
 
</script>

                    

Output
295

Time Complexity: O(1)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads