The Wayback Machine - https://web.archive.org/web/20240627065803/https://www.geeksforgeeks.org/tetrahedral-numbers/
Open In App

Tetrahedral Numbers

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Proof: 
 

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 : 
 

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).

Space complexity: O(1) since using constant variables
 



Similar Reads

Program to print tetrahedral numbers upto Nth term
Prerequisites: Triangular numbersTetrahedral numbers Given a value n, and the task is to print tetrahedral number series up to nth term.Examples: Input: 5 Output: 1 4 10 20 35 Input: 10 Output: 1 4 10 20 35 56 84 120 165 220 Method 1: Using Triangular Number series: This problem can be easily solved with the fact that Nth Tetrahedral Number is equa
9 min read
Centered tetrahedral number
We are given integer n, we need to find n-th centered tetrahedral number.Description: The centered tetrahedral number is a centered figurate number that represents a tetrahedron.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 nth
4 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Permutation of numbers such that sum of two consecutive numbers is a perfect square
Prerequisite: Hamiltonian Cycle Given an integer n(&gt;=2), find a permutation of numbers from 1 to n such that the sum of two consecutive numbers of that permutation is a perfect square. If that kind of permutation is not possible to print "No Solution". Examples: Input : 17 Output : [16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17] Expl
15+ min read
Numbers within a range that can be expressed as power of two numbers
Given two integers L and R. Find the number of perfect powers in the given range [L, R]. A number x is said to be perfect power if there exists some integers a &gt; 0, p &gt; 1 such that x = ap. Examples : Input : 1 4 Output : 2 Explanation : Suitable numbers are 1 and 4 where 1 can be expressed as 1 = 12 and 4 can be expressed as 4 = 22 Input : 12
12 min read
Print all distinct integers that can be formed by K numbers from a given array of N numbers
Given an array of N elements and an integer K, print all the distinct integers which can be formed by choosing K numbers from the given N numbers. A number from an array can be chosen any number of times. Examples: Input: k = 2, a[] = {3, 8, 17, 5} Output: The 10 distinct integers are: 6 8 10 11 13 16 20 22 25 34 The 2 elements chosen are: 3+3 = 6,
7 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbers cannot be a perfect square.Examples: Input : N
8 min read
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
Given N and K, the task is to print N lines where each line contains 4 numbers such that every among those 4 numbers has a GCD K and the maximum number used in N*4 should be minimized.Note: In case of multiple outputs, print any one. Examples: Input: N = 1, K = 1 Output: 1 2 3 5 Every pair among 1, 2, 3 and 5 gives a GCD K and the largest number am
5 min read
three90RightbarBannerImg