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 nth 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 nth tetrahedral number:
Tn = (n * (n + 1) * (n + 2)) / 6
The proof uses the fact that the nth tetrahedral number is given by, Trin = (n * (n + 1)) / 2 It proceeds by induction. Base Case T1 = 1 = 1 * 2 * 3 / 6 Inductive Step Tn+1 = Tn + Trin+1 Tn+1 = [((n * (n + 1) * (n + 2)) / 6] + [((n + 1) * (n + 2)) / 2] Tn+1 = (n * (n + 1) * (n + 2)) / 6
Below is the implementation of above idea :
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.