给定一个数N ,任务是找到第一个N个居中的三边形数的总和。
A Centered tridecagonal number represents a dot at the center and other dots surrounding the center dot in the successive tridecagonal(13 sided polygon) layer. The first few Centered tridecagonal numbers are 1, 14, 40, 79 …
例子:
Input: N = 3
Output: 55
Explanation:
1, 14 and 40 are the first three Centered tridecagonal number.
1 + 14 + 40 = 55.
Input: N = 5
Output: 265
方法:
- 最初,我们需要创建一个函数,该函数将帮助我们计算第N个居中的十三边形数。
- 现在,运行一个从1到N的循环,并在此范围内找到居中的三边形数字。
- 将所有以上计算的居中三边形数字相加。
- 最后,显示前N个居中的三边形数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of
// the first Nth centered
// tridecagonal number
#include
using namespace std;
// Function to calculate the
// N-th centered tridecagonal
// number
int Centered_tridecagonal_num(int n)
{
// Formula to calculate
// Nth centered tridecagonal
// number & return it
return (13 * n * (n - 1) + 2) / 2;
}
// Function to find the sum
// of the first N centered
// tridecagonal numbers
int sum_Centered_tridecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Loop to iterate and find the
// sum of first N centered
// tridecagonal numbers
for(int i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ ;
}
// Driver code
int main()
{
int n = 5;
cout << sum_Centered_tridecagonal_num(n)
<< endl;
return 0;
}
// This code is contributed by rutvik_56
Java
// Java program to find the sum of
// the first Nth centered
// tridecagonal number
class GFG{
// Function to calculate the
// N-th centered tridecagonal
// number
public static int Centered_tridecagonal_num(int n)
{
// Formula to calculate
// Nth centered tridecagonal
// number & return it
return (13 * n * (n - 1) + 2) / 2;
}
// Function to find the sum
// of the first N centered
// tridecagonal numbers
public static int sum_Centered_tridecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Loop to iterate and find the
// sum of first N centered
// tridecagonal numbers
for(int i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ ;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.println(sum_Centered_tridecagonal_num(n));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Program to find the sum of
# the first Nth
# Centered_tridecagonal number
# Function to calculate the
# N-th Centered tridecagonal
# number
def Centered_tridecagonal_num(n):
# Formula to calculate
# Nth Centered tridecagonal
# number & return it
return (13 * n *
(n - 1) + 2) // 2
# Function to find the sum
# of the first N
# Centered tridecagonal
# numbers
def sum_Centered_tridecagonal_num(n) :
# Variable to store
# the sum
summ = 0
# Loop to iterate and find the
# sum of first N Centered
# tridecagonal numbers
for i in range(1, n + 1):
summ += Centered_tridecagonal_num(i)
return summ
# Driver Code
if __name__ == '__main__' :
n = 5
print(sum_Centered_tridecagonal_num(n))
C#
// C# program to find the sum of
// the first Nth centered
// tridecagonal number
using System;
class GFG{
// Function to calculate the
// N-th centered tridecagonal
// number
public static int Centered_tridecagonal_num(int n)
{
// Formula to calculate
// Nth centered tridecagonal
// number & return it
return (13 * n * (n - 1) + 2) / 2;
}
// Function to find the sum
// of the first N centered
// tridecagonal numbers
public static int sum_Centered_tridecagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Loop to iterate and find the
// sum of first N centered
// tridecagonal numbers
for(int i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ;
}
// Driver code
public static void Main()
{
int n = 5;
Console.WriteLine(sum_Centered_tridecagonal_num(n));
}
}
// This code is contributed by Code_Mech
Javascript
输出:
265
时间复杂度: O(N)。