Sum of the series 1, 3, 6, 10... (Triangular Numbers)
Last Updated :
16 Aug, 2022
Given n, no of elements in the series, find the summation of the series 1, 3, 6, 10....n. The series mainly represents triangular numbers.
Examples:
Input: 2
Output: 4
Explanation: 1 + 3 = 4
Input: 4
Output: 20
Explanation: 1 + 3 + 6 + 10 = 20
A simple solution is to one by one add triangular numbers.
C++
/* 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 series
int seriesSum(int n)
{
int sum = 0;
for (int i=1; i<=n; i++)
sum += i*(i+1)/2;
return sum;
}
// Driver code
int main()
{
int n = 4;
cout << seriesSum(n);
return 0;
}
Java
// 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
# Python3 program to find sum
# series 1, 3, 6, 10, 15, 21...
# and then find its sum.
# Function to find the sum of series
def seriessum(n):
sum = 0
for i in range(1, n + 1):
sum += i * (i + 1) / 2
return sum
# Driver code
n = 4
print(seriessum(n))
# This code is Contributed by Azkia Anam.
C#
// 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));
}
}
//
PHP
<?php
// PHP 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)
{
$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.
?>
JavaScript
<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)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum += i * ((i + 1) / 2);
return sum;
}
// Driver code
let n = 4;
document.write(seriesSum(n)) ;
// This code is contributed by aashish1995
</script>
Output:
20
Time complexity : O(n)
Auxiliary Space: O(1) since using constant variables
An efficient solution is to use direct formula n(n+1)(n+2)/6
Let g(i) be i-th triangular number.
g(1) = 1
g(2) = 3
g(3) = 6
g(n) = n(n+1)/2
Let f(n) be the sum of the triangular
numbers 1 through n.
f(n) = g(1) + g(2) + ... + g(n)
Then:
f(n) = n(n+1)(n+2)/6
How can we prove this? We can prove it by induction. That is, prove two things :
- It's true for some n (n = 1, in this case).
- If it's true for n, then it's true for n+1.
This allows us to conclude that it's true for all n >= 1.
Now 1) is easy. We know that f(1) = g(1)
= 1. So it's true for n = 1.
Now for 2). Suppose it's true for n.
Consider f(n+1). We have:
f(n+1) = g(1) + g(2) + ... + g(n) + g(n+1)
= f(n) + g(n+1)
Using our assumption f(n) = n(n+1)(n+2)/6
and g(n+1) = (n+1)(n+2)/2, we have:
f(n+1) = n(n+1)(n+2)/6 + (n+1)(n+2)/2
= n(n+1)(n+2)/6 + 3(n+1)(n+2)/6
= (n+1)(n+2)(n+3)/6
Therefore, f(n) = n(n+1)(n+2)/6
Below is the implementation of the above approach:
C++
/* 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 series
int seriesSum(int n)
{
return (n * (n + 1) * (n + 2)) / 6;
}
// Driver code
int main()
{
int n = 4;
cout << seriesSum(n);
return 0;
}
Java
// 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)
{
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
Python3
# Python 3 program to find sum
# series 1, 3, 6, 10, 15, 21...
# and then find its sum*/
# Function to find the sum of series
def seriesSum(n):
return int((n * (n + 1) * (n + 2)) / 6)
# Driver code
n = 4
print(seriesSum(n))
# This code is contributed by Smitha.
C#
// 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)
{
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
// PHP 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)
{
return ($n * ($n + 1) *
($n + 2)) / 6;
}
// Driver code
$n = 4;
echo(seriesSum($n));
// This code is contributed by Ajit.
?>
JavaScript
<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)
{
return (n * (n + 1) * (n + 2)) / 6;
}
// Driver code
let n = 4;
document.write(seriesSum(n));
// This code is contributed by todaysgaurav
</script>
Output:
20
Time complexity : O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Squared triangular number (Sum of cubes) Given a number s (1 <= s <= 1000000000). If s is sum of the cubes of the first n natural numbers then print n, otherwise print -1.First few Squared triangular number are 1, 9, 36, 100, 225, 441, 784, 1296, 2025, 3025, ...Examples : Input : 9 Output : 2 Explanation : The given number is sum of
15+ min read
Sum of the series 3, 20, 63, 144, ...... Find the sum of first n terms of the given series: 3, 20, 63, 144, ..... Examples: Input : n = 2 Output : 23 Input : n =4 Output : 230 Approach: First, we have to find the general term (Tn) of the given series. series can we written in the following way also: (3 * 1^2), (5 * 2^2), (7 * 3^2), (9 * 4^
4 min read
Find the sequence number of a triangular number Given an integer N print the sequence number of the given Triangular Number. If the number is not a triangular number then print -1. A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each
6 min read
Find the sum of series 3, 7, 13, 21, 31.... Given a number N. The task is to find the sum of below series upto nth term. 3, 7, 13, 21, 31, .... Examples: Input : N = 3 Output : 23 Input : N = 25 Output : 5875 Approach:Let $$S=0+3+7+13+21+31+.......+a_{n-1}+a_n$$ $$S=3+7+13+21+31+...+a_{n-2}+a_{n-1}+a_n$$ Subtracting the above two equations, w
3 min read
Find sum of the series 1-2+3-4+5-6+7....... Given a number N. The task is to find the sum of the below series up to nth term. 1- 2 + 3 - 4 + 5 - 6 +.... Examples: Input : N = 8 Output : -4 Input : N = 10001 Output : 5001 Approach: If we observe carefully, we can see that the sum of the above series follows a pattern of alternating positive an
3 min read
Find sum of the series 1! - 2! + 3! - 4! + 5! . . . till Nth term Given a positive integer N, the task is to find the sum of series 1! - 2! + 3! - 4! + 5!... till Nth term. Examples: Input: N = 6Output: -619Explanation: The sum of the series upto 5th term can be calculated as1! - 2! + 3! - 4! + 5! -6! = 1 -2 +6 -24 +120 -720 = -619 Input: N = 5Output: 101 Native A
10 min read
Sum of all the numbers present at given level in Pascal's triangle Given a level L. The task is to find the sum of all the integers present at the given level in Pascal's triangle . A Pascal triangle with 6 levels is shown below: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Examples: Input: L = 3 Output: 4 1 + 2 + 1 = 4 Input: L = 2 Output:2 Approach: If we observe
2 min read
Sum of all the numbers in the Nth row of the given triangle Given a positive integer N, the task is to find the sum of all the numbers in the Nth row of the below triangle. 1 3 2 6 2 3 10 2 3 4 15 2 3 4 5 ... ... ... Examples: Input: N = 2 Output: 5 3 + 2 = 5Input: N = 3 Output: 11 6 + 2 + 3 = 11 Approach: Taking a closer look at the pattern, it can be obser
5 min read
Sum of the first N terms of the series 2,10, 30, 68,.... Given a number N, the task is to find the sum of first N terms of the below series: Sn = 2 + 10 + 30 + 68 + ⦠upto n terms Examples: Input: N = 2 Output: 12 2 + 10 = 12 Input: N = 4 Output: 40 2 + 10 + 30 + 68 = 110 Approach: Let, the nth term be denoted by tn. This problem can easily be solved by s
4 min read
Sum of the first N terms of the series 2, 6, 12, 20, 30.... Given a number N, the task is to find the sum of the first N terms of the below series: Sn = 2 + 6 + 12 + 20 + 30 ⦠upto n terms Examples: Input: N = 2 Output: 8 Explanation: 2 + 6 = 8 Input: N = 4 Output: 40 Explanation: 2 + 6+ 12 + 20 = 40 Approach: Let, the nth term be denoted by Sn. This problem
4 min read