Tetrahedral Numbers

A number is termed as a tetrahedral number if it can be represented as a pyramid with a triangular base and three sides, called a tetrahedron. The n^{th} tetrahedral number is the sum of the first n triangular numbers.

The first ten tetrahedral numbers are:

1, 4, 10, 20, 35, 56, 84, 120, 165, 220, …

Formula for n^{th} tetrahedral number:

T_{n}= (n * (n + 1) * (n + 2)) / 6

**Proof:**

The proof uses the fact that the n^{th}tetrahedral number is given by, Tri_{n}= (n * (n + 1)) / 2 It proceeds by induction.Base CaseT_{1}= 1 = 1 * 2 * 3 / 6Inductive StepT_{n+1}= T_{n}+ Tri_{n+1}T_{n+1}= [((n * (n + 1) * (n + 2)) / 6] + [((n + 1) * (n + 2)) / 2] T_{n+1}= (n * (n + 1) * (n + 2)) / 6

Below is the implementation of above idea :

## C++

`// CPP Program to find the` `// nth tetrahedral number` `#include <iostream>` `using` `namespace` `std;` `int` `tetrahedralNumber(` `int` `n)` `{` ` ` `return` `(n * (n + 1) * (n + 2)) / 6;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 5;` ` ` ` ` `cout << tetrahedralNumber(n) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java Program to find the` `// nth tetrahedral number` `class` `GFG {` ` ` `// Function to find Tetrahedral Number` `static` `int` `tetrahedralNumber(` `int` `n)` `{` ` ` `return` `(n * (n + ` `1` `) * (n + ` `2` `)) / ` `6` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `n = ` `5` `;` ` ` ` ` `System.out.println(tetrahedralNumber(n));` `}` `}` `// This code is contributed by Manish Kumar Rai.` |

## Python

`# Python3 Program to find the` `# nth tetrahedral number` `def` `tetrahedralNumber(n):` ` ` ` ` `return` `(n ` `*` `(n ` `+` `1` `) ` `*` `(n ` `+` `2` `)) ` `/` `6` `# Driver Code` `n ` `=` `5` `print` `(tetrahedralNumber(n))` |

## C#

`// C# Program to find the` `// nth tetrahedral number` `using` `System;` `public` `class` `GFG{` ` ` ` ` `// Function to find Tetrahedral Number` ` ` `static` `int` `tetrahedralNumber(` `int` `n)` ` ` `{` ` ` `return` `(n * (n + 1) * (n + 2)) / 6;` ` ` `}` ` ` ` ` `// Driver code` ` ` `static` `public` `void` `Main ()` ` ` `{` ` ` `int` `n = 5;` ` ` ` ` `Console.WriteLine(tetrahedralNumber(n));` ` ` `}` `}` `// This code is contributed by Ajit.` |

## PHP

`<?php` `// PHP Program to find the` `// nth tetrahedral number` `function` `tetrahedralNumber(` `$n` `)` `{` ` ` `return` `(` `$n` `* (` `$n` `+ 1) * (` `$n` `+ 2)) / 6;` `}` `// Driver Code` ` ` `$n` `= 5;` ` ` `echo` `tetrahedralNumber(` `$n` `);` ` ` `// This code is contributed by mits` `?>` |

## Javascript

`<script>` `// JavaScript Program to find the` `// nth tetrahedral number` `// Function to find Tetrahedral Number` `function` `tetrahedralNumber(n)` `{` ` ` `return` `(n * (n + 1) * (n + 2)) / 6;` `}` ` ` `// Driver code` ` ` `let n = 5;` ` ` `document.write(tetrahedralNumber(n));` ` ` ` ` `// This code is contributed by code_hunt.` `</script>` |

Output:

35

**Time Complexity**: O(1).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.