Related Articles

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

• Difficulty Level : Easy
• Last Updated : 09 Sep, 2021

Given the value of n, we need to find the sum of the series where i-th term is sum of first i natural numbers.
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 implementation of above series :

## C++

 // CPP program to find sum of given series#include using namespace std; // Function to find sum of given seriesint 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 Functionint main(){    int n = 10;    cout << sumOfSeries(n);    return 0;}

## Java

 // JAVA Code For Sum of the seriesimport 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 seriesdef sumOfSeries(n):    return sum([i*(i+1)/2 for i in range(1, n + 1)]) # Driver Codeif __name__ == "__main__":    n = 10    print(sumOfSeries(n))

## C#

 // C# Code For Sum of the seriesusing 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.

## PHP

 

## Javascript

 

Output :

220

Efficient Approach :
Let term of the series 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4)…(1 + 2 + 3 +..n) be denoted as an

an = Σn1  =  =
Sum of n-terms of series
Σn1 an = Σn1
=  Σ    + Σ
=  *  +  *
= 

Below is implementation of above approach :

## C++

 // CPP program to find sum of given series#include using namespace std; // Function to find sum of given seriesint sumOfSeries(int n){    return (n * (n + 1) * (2 * n + 4)) / 12;} // Driver Functionint main(){    int n = 10;    cout << sumOfSeries(n);}

## Java

 // JAVA Code For Sum of the seriesimport 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 seriesdef sumOfSeries(n):    return (n * (n + 1) * (2 * n + 4)) / 12;     # Driver functionif __name__ == '__main__':    n = 10    print(sumOfSeries(n))

## C#

 // C# Code For Sum of the seriesusing 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.

## PHP

 

## Javascript

 

Output :

220

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up