Open In App

Find the sum of N terms of the series 1^2, (1^2+2^2), (1^2+2^2+3^2), …..

Last Updated : 20 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer, N. Find the sum of the first N term of the series-

12, (12+22), (12+22+32),….,till N terms

Examples:

Input: N = 3

Output: 20

Input: N = 1

Output: 1

 

Approach: The sequence is formed by using the following pattern. For any value N-

Given 1^2, (1^2+2^2), (1^2+2^2+3^2),….,till N terms

\sum_{i=1}^{i=n} i = \frac{n(n+1)}{2} \\ \sum_{i=1}^{i=n} i^{2}= \frac{n(n+1)(2n+1)}{6} \\ \sum_{i=1}^{i=n} i^{3}= \frac{n^{2}(n+1)^{2}}{4} \\ 1^{2}+(1^{2}+2^{2}....)+(1^{2}+2^{2}....+n^{2})\\ = \sum_{i=1}^{i=n} (1^{2}+2^{2}...+i^{2})\\ =\sum_{i=1}^{i=n}\sum_{i=1}^{i=i}i^{2}\\ =\sum_{i=1}^{i=n}  \frac{i(i+1)(2i+1)}{6} \\ =\sum_{i=1}^{i=n}  \frac{2i^{3}+3i^{2}+i}{6} \\ =\frac{1}{3}\sum_{i=1}^{i=n} i^{3}+\frac{1}{2}\sum_{i=1}^{i=n} i^{2}+\frac{1}{6}\sum_{i=1}^{i=n} i\\ \text{use above summation formula}\\ =\frac{1}{3}[\frac{n^{2}(n+1)^{2}}{4}]+\frac{1}{2}[\frac{n(n+1)(2n+1)}{6}]+\frac{1}{6}[\frac{n(n+1)}{2}]\\ =\frac{n(n+1)^{2}(n+2)}{12}

SN = N * (N+1)2 * (N+2) / 12

Below is the implementation of the above approach:

C++

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N){
  
  return N*(N+1)*(N+1)*(N+2)/12;
   
}
 
// Driver Code
 
int main()
{
    int N = 3;
     
    cout << findSum(N);
}

                    

Java

// Java program to implement
// the above approach
 
class GFG {
 
    // Function to return sum of
    // N term of the series
 
    static int findSum(int N) {
 
        return N * (N + 1) * (N + 1) * (N + 2) / 12;
 
    }
 
    // Driver Code
 
    public static void main(String args[]) {
        int N = 3;
 
        System.out.print(findSum(N));
    }
}
 
// This code is contributed by Saurabh Jaiswal

                    

Python3

# Python 3 program for the above approach
 
# Function to return sum of
# N term of the series
 
def findSum(N):
   
  return N*(N+1)*(N+1)*(N+2)//12
 
 
# Driver Code
if __name__ == "__main__":
   
    # Value of N
    N = 3   
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.

                    

C#

// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to return sum of
// N term of the series
 
static int findSum(int N){
  
  return N*(N+1)*(N+1)*(N+2)/12;
   
}
 
// Driver Code
 
public static void Main()
{
    int N = 3;
     
    Console.Write(findSum(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.

                    

Javascript

<script>
// Javascript program to implement
// the above approach
 
// Function to return sum of
// N term of the series
 
function findSum(N){
  
  return N*(N+1)*(N+1)*(N+2)/12;
   
}
 
// Driver Code
 
let N = 3;
document.write(findSum(N));
 
// This code is contributed by Samim Hossain Mondal.
</script>

                    

Output
20

Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.


 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads