给定数字N,任务是找到前N个中心十进制数的总和。
The first few Centered decagonal numbers are 1, 11, 31, 61, 101, 151 …
例子:
Input: N = 3
Output: 43
Explanation:
1, 11 and 31 are the first three Centered decagonal numbers.
Input: N = 5
Output: 205
方法:
- 最初,我们需要创建一个函数,该函数将帮助我们计算第N个中心十进制数。
- 现在,运行一个从1到N的循环,以找到第i个居中的十进制数。
- 将所有以上计算的居中十进制数相加。
- 最后,显示第一个N个居中十进制数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// first N centred decagonal number
#include
using namespace std;
// Function to find the N-th
// centred decagonal number
int Centered_decagonal_num(int n)
{
// Formula to calculate nth
// centered_decagonal number
// & return it into main function.
return (5 * n * n - 5 * n + 1);
}
// Function to find the sum of
// the first N centered decagonal
// numbers
int sum_Centered_decagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
for(int i = 1; i < n + 1; i++)
{
summ += Centered_decagonal_num(i);
}
return summ;
}
// Driver code
int main()
{
int n = 5;
// Display first Nth
// centered_decagonal number
cout << (sum_Centered_decagonal_num(n));
return 0;
}
// This code is contributed by PrinciRaj1992
Java
// Java program to find the sum of the
// first N centred decagonal number
class GFG {
// Function to find the N-th
// centred decagonal number
static int Centered_decagonal_num(int n)
{
// Formula to calculate nth
// centered_decagonal number
// & return it into main function.
return (5 * n * n - 5 * n + 1);
}
// Function to find the sum of
// the first N centered decagonal
// numbers
static int sum_Centered_decagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
for(int i = 1; i < n + 1; i++)
{
summ += Centered_decagonal_num(i);
}
return summ;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
// Display first Nth
// centered_decagonal number
System.out.println(sum_Centered_decagonal_num(n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find the sum of
# the first N centred
# decagonal number
# Function to find the N-th
# centred decagonal number
def Centered_decagonal_num(n):
# Formula to calculate
# nth Centered_decagonal
# number & return it
# into main function.
return (5 * n * n -
5 * n + 1)
# Function to find the
# sum of the first N
# Centered decagonal
# numbers
def sum_Centered_decagonal_num(n) :
# Variable to store
# the sum
summ = 0
# Iterating through the range
for i in range(1, n + 1):
summ += Centered_decagonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 5
# display first Nth
# Centered_decagonal number
print(sum_Centered_decagonal_num(n))
C#
// C# program to find the sum of the
// first N centred decagonal number
using System;
class GFG {
// Function to find the N-th
// centred decagonal number
static int Centered_decagonal_num(int n)
{
// Formula to calculate nth
// centered_decagonal number
// & return it into main function.
return (5 * n * n - 5 * n + 1);
}
// Function to find the sum of
// the first N centered decagonal
// numbers
static int sum_Centered_decagonal_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
for(int i = 1; i < n + 1; i++)
{
summ += Centered_decagonal_num(i);
}
return summ;
}
// Driver code
public static void Main(String[] args)
{
int n = 5;
// Display first Nth
// centered_decagonal number
Console.WriteLine(sum_Centered_decagonal_num(n));
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
205
时间复杂度: O(N)。