给定整数n,我们需要找到第n个居中的四面体数。
说明:中心四面体数是代表四面体的中心比喻数。
四面体数:如果一个数字可以表示为具有三角形底面和三边的金字塔,则称为四面体,称为四面体。第n个四面体数是前n个三角数之和。
前几个居中的四面体数系列是:
1,5,15,35,69,121,195,295,425,589…………………………。
中心四面体数的数学n项:
例子 :
Input : n = 3
Output : 35
Input : n = 9
Output : 589
下面是上述公式的实现。
C++
// C++ Program to find nth
// Centered tetrahedral number
#include
using namespace std;
// Function to find centered
// Centered tetrahedral number
int centeredTetrahedralNumber(int n)
{
// Formula to calculate nth
// Centered tetrahedral number
// and return it into main function.
return (2 * n + 1) * (n * n + n + 3) / 3;
}
// Driver Code
int main()
{
int n = 6;
cout << centeredTetrahedralNumber(n);
return 0;
}
Java
// Java Program to find nth Centered
// tetrahedral number
import java.io.*;
class GFG {
// Function to find centered
// Centered tetrahedral number
static int centeredTetrahedralNumber(int n)
{
// Formula to calculate nth
// Centered tetrahedral number
// and return it into main function.
return (2 * n + 1) * (n * n + n + 3) / 3;
}
// Driver Code
public static void main (String[] args)
{
int n = 6;
System.out.println(
centeredTetrahedralNumber(n));
}
}
// This code is contributed by anuj_67.
Python3
# Python program to find nth
# Centered tetrahedral number
# Function to calculate
# Centered tetrahedral number
def centeredTetrahedralNumber(n):
# Formula to calculate nth
# Centered tetrahedral number
# and return it into main function
return (2 * n + 1) * (n * n + n + 3) // 3
# Driver Code
n = 6
print(centeredTetrahedralNumber(n))
# This code is contributed by ajit
C#
// C# Program to find nth Centered
// tetrahedral number
using System;
class GFG {
// Function to find centered
// Centered tetrahedral number
static int centeredTetrahedralNumber(int n)
{
// Formula to calculate nth
// Centered tetrahedral number
// and return it into main function.
return (2 * n + 1) * (n * n + n + 3) / 3;
}
// Driver Code
public static void Main ()
{
int n = 6;
Console.WriteLine(
centeredTetrahedralNumber(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出 :
195