📜  四面体数

📅  最后修改于: 2021-04-27 09:16:57             🧑  作者: Mango

如果数字可以表示为具有三角形底边和三个边的金字塔,则称为四面体数,称为四面体。第n四面体数是前n个三角数之和。
前十个四面体数字为:
1,4,10,20,35,56,84,120,165,220,…

第n四面体数的公式:

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

下面是上述想法的实现:

C++
// CPP Program to find the
// nth tetrahedral number
#include 
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


Javascript


输出:

35

时间复杂度:O(1)。