Sum of first n natural numbers

06 Apr, 2021
Given a positive integer n. The task is to find the sum of the sum of first n natural number.Examples:  
Input : n = 3
Output : 10
Sum of first natural number: 1
Sum of first and second natural number: 1 + 2 = 3
Sum of first, second and third natural number = 1 + 2 + 3 = 6
Sum of sum of first three natural number = 1 + 3 + 6 = 10

Input : n = 2
Output : 4
 
A simple solution is to one by one add triangular numbers.  
C++
Java
Python3
C#
PHP
Javascript
/* CPP program to find sum series 1, 3, 6, 10, 15, 21...and then find its sum*/#include <iostream>using namespace std; // Function to find the sum of seriesint seriesSum(int n){    int sum = 0;    for (int i=1; i<=n; i++)       sum += i*(i+1)/2;    return sum;} // Driver codeint main(){    int n = 4;    cout << seriesSum(n);    return 0;}
// Java program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum*/import java.io.*; class GFG {             // Function to find the sum of series    static int seriesSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)        sum += i * (i + 1) / 2;        return sum;    }     // Driver code    public static void main (String[] args)    {        int n = 4;        System.out.println(seriesSum(n));             }} // This article is contributed by vt_m
# Python3 program to find sum# series 1, 3, 6, 10, 15, 21...# and then find its sum. # Function to find the sum of seriesdef seriessum(n):         sum = 0    for i in range(1, n + 1):        sum += i * (i + 1) / 2    return sum     # Driver coden = 4print(seriessum(n)) # This code is Contributed by Azkia Anam.
// C# program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum*/using System; class GFG {     // Function to find the sum of series    static int seriesSum(int n)    {        int sum = 0;                 for (int i = 1; i <= n; i++)            sum += i * (i + 1) / 2;                     return sum;    }     // Driver code    public static void Main()    {        int n = 4;                 Console.WriteLine(seriesSum(n));    }} // This article is contributed by vt_m.
<?php// PHP program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum // Function to find// the sum of seriesfunction seriesSum($n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += $i * ($i + 1) / 2;    return $sum;} // Driver code$n = 4;echo(seriesSum($n)); // This code is contributed by Ajit.?>
<script> // javascript program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum*/     // Function to find the sum of series    function seriesSum(n) {        var sum = 0;        for (i = 1; i <= n; i++)            sum += i * ((i + 1) / 2);        return sum;    }     // Driver code             var n = 4;        document.write(seriesSum(n));  // This code contributed by Rajput-Ji </script>
Output: 
20
Time complexity : O(n)An efficient solution is to use direct formula n(n+1)(n+2)/6Mathematically, we need to find, Σ ((i * (i + 1))/2), where 1 <= i <= n So, lets solve this summation,  
Sum = Σ ((i * (i + 1))/2), where 1 <= i <= n
    = (1/2) * Σ (i * (i + 1))
    = (1/2) * Σ (i2 + i)
    = (1/2) * (Σ i2 + Σ i)

We know Σ i2 = n * (n + 1) * (2*n + 1) / 6 and 
Σ i = n * ( n + 1) / 2.
Substituting the value, we get,
Sum = (1/2) * ((n * (n + 1) * (2*n + 1) / 6) + (n * ( n + 1) / 2))  
    = n * (n + 1)/2 [(2n + 1)/6 + 1/2]
    = n * (n + 1) * (n + 2) / 6
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
/* CPP program to find sum series 1, 3, 6, 10, 15, 21...and then find its sum*/#include <iostream>using namespace std; // Function to find the sum of seriesint seriesSum(int n){    return (n * (n + 1) * (n + 2)) / 6;} // Driver codeint main(){    int n = 4;    cout << seriesSum(n);    return 0;}
// java program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sumimport java.io.*; class GFG{    // Function to find the sum of series    static int seriesSum(int n)    {        return (n * (n + 1) * (n + 2)) / 6;    }    // Driver code    public static void main (String[] args) {                 int n = 4;        System.out.println( seriesSum(n));             }} // This article is contributed by vt_m
# Python 3 program to find sum# series 1, 3, 6, 10, 15, 21...# and then find its sum*/ # Function to find the sum of seriesdef seriesSum(n):     return int((n * (n + 1) * (n + 2)) / 6)  # Driver coden = 4print(seriesSum(n)) # This code is contributed by Smitha.
// C# program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sumusing System; class GFG {         // Function to find the sum of series    static int seriesSum(int n)    {        return (n * (n + 1) * (n + 2)) / 6;    }     // Driver code    public static void Main()    {         int n = 4;                 Console.WriteLine(seriesSum(n));    }} // This code is contributed by vt_m.
<?php// PHP program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum // Function to find// the sum of seriesfunction seriesSum($n){    return ($n * ($n + 1) *           ($n + 2)) / 6;} // Driver code$n = 4;echo(seriesSum($n)); // This code is contributed by Ajit.?>
<script>// javascript program to find sum// series 1, 3, 6, 10, 15, 21...// and then find its sum // Function to find the sum of seriesfunction seriesSum(n){    return (n * (n + 1) * (n + 2)) / 6;} // Driver codevar n = 4;document.write( seriesSum(n)); // This code is contributed by shikhasingrajput</script>
Output:  
20
Time complexity : O(1) 
Rajput-Ji
shikhasingrajput
series
series-sum
triangular-number
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Count ways to reach the n'th stair
Fizz Buzz Implementation
Median of two sorted arrays of same size
Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
Product of Array except itself