Open In App

Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)

Last Updated : 25 Jul, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

Given a positive integer n, find the sum of the following series, where the i-th term is the sum of the first i natural numbers:

(1) + (1+2) + (1+2+3) + ... + (1+2+3+...+n)

Examples

Input  : n = 5   
Output : 35
Explanation :
(1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35

Input : n = 10
Output : 220
Explanation :
(1) + (1+2) + (1+2+3) + .... +(1+2+3+4+.....+10) = 220

Naive Approach : 

Below is the implementation of the above series:  

C++
// CPP program to find sum of given series
#include <bits/stdc++.h>
using namespace std;

// Function to find sum of given series
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1 ; i <= n ; i++)
        for (int j = 1 ; j <= i ; j++)
            sum += j;
    return sum;
}

// Driver Function
int main()
{
    int n = 10;
    cout << sumOfSeries(n); 
    return 0;
}
Java
// JAVA Code For Sum of the series
import java.util.*;

class GFG {
    
    // Function to find sum of given series
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1 ; i <= n ; i++)
            for (int j = 1 ; j <= i ; j++)
                sum += j;
        return sum;
    }
    
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
         int n = 10;
         System.out.println(sumOfSeries(n)); 
        
    }
}

// This code is contributed by Arnav Kr. Mandal.
Python
# Python3 program to find sum of given series 

# Function to find sum of series
def sumOfSeries(n):
    return sum([i*(i+1)/2 for i in range(1, n + 1)])

# Driver Code 
if __name__ == "__main__":
    n = 10
    print(sumOfSeries(n))
C#
// C# Code For Sum of the series
using System;

class GFG {

    // Function to find sum of given series
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                sum += j;
        return sum;
    }

    /* Driver program to test above function */
    public static void Main()
    {
        int n = 10;
        
        Console.Write(sumOfSeries(n));
    }
}

// This code is contributed by vt_m.
JavaScript
<script>

// JavaScript Program for Sum of the series

 // Function to find sum of given series
    function sumOfSeries(n)
    {
        let sum = 0;
        for (let i = 1 ; i <= n ; i++)
            for (let j = 1 ; j <= i ; j++)
                sum += j;
        return sum;
    }

// Driver code    
         
        let n = 10;
        document.write(sumOfSeries(n));
            
</script>
PHP
<?php
// PHP program to find 
// sum of given series

// Function to find 
// sum of given series
function sumOfSeries($n)
{
    $sum = 0;
    for ($i = 1 ; $i <= $n ; $i++)
        for ($j = 1 ; $j <= $i ; $j++)
            $sum += $j;
    return $sum;
}

// Driver Code
$n = 10;
echo(sumOfSeries($n)); 

// This code is contributed by Ajit.
?>


Output : 

220

Time Complexity: O(n^2)
Space Complexity: O(1)


Efficient Approach :

Let n^{th}      term of the series 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4)...(1 + 2 + 3 +..n) be denoted as an 
 

an = Σn1 i = \frac{n (n + 1)}{2} = \frac{(n^2 + n)}{2}
Sum of n-terms of series
Σn1 an = Σn1 \frac{(n^2 + n)}{2}
= \frac{1}{2} Σ [ n^2 ] + Σ [ n ]
= \frac{1}{2} * \frac{n(n + 1)(2n + 1)}{6} + \frac{1}{2} * \frac{n(n+1)}{2}
= \frac{n(n+1)(2n+4)}{12}

Below is implementation of above approach: 

C++
// CPP program to find sum of given series
#include <bits/stdc++.h>
using namespace std;

// Function to find sum of given series
int sumOfSeries(int n)
{
    return (n * (n + 1) * (2 * n + 4)) / 12;
}

// Driver Function
int main()
{
    int n = 10;
    cout << sumOfSeries(n); 
}
Java
// JAVA Code For Sum of the series
import java.util.*;

class GFG {
    
    // Function to find sum of given series
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * 
                (2 * n + 4)) / 12;
    }
    
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
         int n = 10;
         System.out.println(sumOfSeries(n)); 
        
    }
}

// This code is contributed by Arnav Kr. Mandal.
Python
# Python program to find sum of given series

# Function to find sum of given series
def sumOfSeries(n):
    return (n * (n + 1) * (2 * n + 4)) / 12;
    
# Driver function
if __name__ == '__main__':
    n = 10
    print(sumOfSeries(n))
C#
// C# Code For Sum of the series
using System;

class GFG {

    // Function to find sum of given series
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 4)) / 12;
    }

    /* Driver program to test above function */
    public static void Main()
    {
        int n = 10;
        
        Console.Write(sumOfSeries(n));
    }
}

// This code is contributed by vt_m.
JavaScript
<script>

// JavaScript program For Sum of the series

// Function to find sum of given series
    function sumOfSeries(n)
    {
        return (n * (n + 1) *
                (2 * n + 4)) / 12;
    }
     

// Driver code
        
        let n = 10;
        document.write(sumOfSeries(n));
                  
</script>
PHP
<?php
// PHP program to find
// sum of given series

// Function to find 
// sum of given series
function sumOfSeries($n)
{
    return ($n * ($n + 1) * 
           (2 * $n + 4)) / 12;
}

// Driver Code
$n = 10;
echo(sumOfSeries($n)); 

// This code is contributed by Ajit.
?>

Output : 

220

Time Complexity: O(1)

Auxiliary Space: O(1) 


Explore