# Sum of the series 1, 3, 6, 10… (Triangular Numbers)

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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` ` ` `// This article is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

20

Time complexity : O(n)

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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

20

Time complexity : O(1)

## Recommended Posts:

- Program to print triangular number series till n
- Triangular Numbers
- Check if a number can be represented as a sum of 2 triangular numbers
- Sum of range in a series of first odd then even natural numbers
- Centered triangular number
- Centered triangular number in PL/SQL
- Triangular Matchstick Number
- Squared triangular number (Sum of cubes)
- Smallest triangular number larger than p
- Program for triangular pattern (mirror image around 0)
- Recursive program to print triangular patterns
- Program to find the Volume of a Triangular Prism
- Program to calculate the Surface Area of a Triangular Prism
- Maximum height of triangular arrangement of array values
- First triangular number whose number of divisors exceeds N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.