Skip to content
Related Articles
Sum of sum-series of first N Natural numbers
• Last Updated : 26 Mar, 2021

Given a natural number n, find the sum of sum-series of first N natural number.

Sum-Series: is sum of first N natural numbers, i.e, sum-series of 5 is 15 ( 1 + 2 + 3 + 4 + 5 ).

Example:

Input: N = 5
Output: 35
Explanation:
Sum of sum-series of {1, 2, 3, 4, 5} i.e. {1 + 3 + 6 + 10 + 15} is 35.
Input: N = 2
Output:
Explanation:
Sum of sum-series of {1, 2} i.e. {1 + 3} is 4.

Simple approach:
Find sum-series for every value from 1 to N and then add it.

• Create a variable Total_sum to store the required sum series.
• Iterate over number from 1 to N
• Find sum-series of every value by using the formulae sum = (N*(N + 1)) / 2
• Add the value to Total_sum

At the end, print the value stored in Total_sum.

## C++

 // C++ program to implement// the above approach#includeusing namespace std; // Function to find the sumstatic long sumOfSumSeries(int N){    long sum = 0L;     // Calculate sum-series    // for every natural number    // and add them    for (int i = 1; i <= N; i++)    {        sum = sum + (i * (i + 1)) / 2;    }     return sum;} // Driver codeint main(){    int N = 5;    cout << sumOfSumSeries(N);} // This code is contributed by Code_Mech

## Java

 // Java program to implement// the above approach class GFG {     // Function to find the sum    static long sumOfSumSeries(int N)    {         long sum = 0L;         // Calculate sum-series        // for every natural number        // and add them        for (int i = 1; i <= N; i++) {            sum = sum + (i * (i + 1)) / 2;        }         return sum;    }     // Driver code    public static void main(String[] args)    {        int N = 5;        System.out.println(sumOfSumSeries(N));    }}

## Python3

 # Python3 program to implement# the above approach # Function to find the sumdef sumOfSumSeries(N):     _sum = 0     # Calculate sum-series    # for every natural number    # and add them    for i in range(N + 1):        _sum = _sum + (i * (i + 1)) // 2     return _sum # Driver codeN = 5 print(sumOfSumSeries(N))     # This code is contributed by divyamohan123

## C#

 // C# program to implement// the above approachusing System;class GFG{ // Function to find the sumstatic long sumOfSumSeries(int N){    long sum = 0L;     // Calculate sum-series    // for every natural number    // and add them    for(int i = 1; i <= N; i++)    {       sum = sum + (i * (i + 1)) / 2;    }         return sum;} // Driver codepublic static void Main(){    int N = 5;         Console.Write(sumOfSumSeries(N));}} // This code is contributed by Nidhi_Biet

## Javascript

 
Output:
35

Time complexity: O(N)
Efficient approach:
Total_sum of above series can be calculated directly by using below formulae: where N is the natural number

Proof of above formula:
Lets assume N = 5

1. Then sum is sum of all below elements in table, lets call this “result”

lets populate the empty cells with same value in other columns, lets call this “totalSum

1.

As sum of N numbers is repeated N times
totalSum = N * [(N*(N + 1))/2]
populated data = (1 times * 2) + (2 times * 3) + (3 times * 4) + (4 times * 5)
= 1*2 + 2*3 + 3*4 ……… +(N-1)*N
=[(N-1) * (N) * (N+1)]/3

1. Since,

result = totalSum – populatedData
= N * [(N*(N+1))/2] – [(N-1) * (N) * (N+1)]/3
= (N*(N+1)*(N+2))/6

1.
2. Therefore Below is the implementation of the above approach:

## C++

 // C++ program to implement// the above approach#include #include using namespace std; // Function to find the sumlong sumOfSumSeries(int n){    return (n * (n + 1) * (n + 2)) / 6;} // Driver codeint main (){    int N = 5;    cout << sumOfSumSeries(N);    return 0;} // This code is contributed// by shivanisinghss2110

## Java

 // Java program to implement// the above approach class GFG {     // Function to find the sum    static long sumOfSumSeries(int n)    {        return (n * (n + 1) * (n + 2)) / 6;    }     // Driver code    public static void main(String[] args)    {        int N = 5;        System.out.println(sumOfSumSeries(N));    }}

## Python3

 # Python3 program to implement# the above approach # Function to find the sumdef sumOfSumSeries(n):         return (n * (n + 1) * (n + 2)) // 6 # Driver codeN = 5 print(sumOfSumSeries(N)) # This code is contributed by divyamohan123

## C#

 // C# program to implement the// above approachusing System;class GFG{ // Function to find the sumstatic long sumOfSumSeries(int n){    return (n * (n + 1) * (n + 2)) / 6;} // Driver codepublic static void Main(String[] args){    int N = 5;         Console.Write(sumOfSumSeries(N));}} // This code is contributed by Ritik Bansal

## Javascript

 
Output:
35

Time complexity O(1) considering multiplication, addition & division take constant time.

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