给定数N,任务是找到第一个N中心十二边形数的总和。
The first few Centered Dodecagonal Numbers are 1, 13, 37, 73, 121, 181 …
例子:
Input: N = 3
Output: 51
Explanation:
1, 13 and 37 are the first three centered Dodecagonal number.
Input: N = 5
Output: 245
方法:
- 首先,创建一个函数,该函数将帮助我们计算第N个中心十二边形数。
- 运行从1到N的循环,以找到第i个居中的十二边形数。
- 将所有以上计算的居中十二边形数相加。
- 最后,显示前N个居中的十二边形数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum
// of the first N Centred
// Dodecagonal number
#include
using namespace std;
// Function to find the N-th
// Centered Dodecagonal number
int Centered_Dodecagonal_num(int n)
{
// Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Function to find the sum of the first
// N Centered_Dodecagonal number
int sum_Centered_Dodecagonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating from 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
}
// Driver code
int main()
{
int n = 5;
cout << sum_Centered_Dodecagonal_num(n);
}
// This code is contributed by coder001
Java
// Java program to find the sum of the
// first N centred dodecagonal number
class GFG {
// Function to find the N-th
// centered dodecagonal number
static int Centered_Dodecagonal_num(int n)
{
// Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Function to find the sum of the first
// N Centered_Dodecagonal number
static int sum_Centered_Dodecagonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating from 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
}
// Driver code
public static void main (String[] args)
{
int n = 5;
System.out.print(sum_Centered_Dodecagonal_num(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find the sum
# of the first N centred
# Dodecagonal number
# Function to find the
# N-th Centered Dodecagonal
# number
def Centered_Dodecagonal_num(n):
# Formula to calculate
# nth Centered_Dodecagonal
# number
return 6 * n * (n - 1) + 1
# Function to find the
# sum of the first N
# Centered_Dodecagonal
# number
def sum_Centered_Dodecagonal_num(n) :
# Variable to store the
# sum
summ = 0
# Iterating from 1 to N
for i in range(1, n + 1):
# Finding the sum
summ += Centered_Dodecagonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 5
print(sum_Centered_Dodecagonal_num(n))
C#
// C# program to find the sum of the
// first N centred dodecagonal number
using System;
class GFG{
// Function to find the N-th
// centered dodecagonal number
static int Centered_Dodecagonal_num(int n)
{
// Formula to calculate nth
// Centered_Dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Function to find the sum of the first
// N Centered_Dodecagonal number
static int sum_Centered_Dodecagonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating from 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Centered_Dodecagonal_num(i);
}
return summ;
}
// Driver code
public static void Main()
{
int n = 5;
Console.Write(sum_Centered_Dodecagonal_num(n));
}
}
// This code is contributed by Code_Mech
Javascript
输出:
245
时间复杂度: O(N)。