给定数字N ,任务是找到第一个N个居中的八边形数字的总和。
例子:
Input: N = 3
Output: 75
Explanation:
1, 19 and 55 are the first three centered octadecagonal numbers.
Input: N = 10
Output: 298
方法:
- 最初,我们需要创建一个函数,该函数将帮助我们计算第N个居中的八边形数。
- 现在,运行一个从1到N的循环,以找到第i个居中的八边形数。
- 将所有上述计算的居中八边形数相加。
- 最后,显示前N个居中的八边形数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// first N centered octadecagonal numbers
#include
using namespace std;
// Function to find the N-th centered
// octadecagonal number
int center_octadecagon_num(int n)
{
// Formula to calculate
// nth centered octadecagonal
// number
return (9 * n * n - 9 * n + 1);
}
// Function to find the sum of the first
// N centered octadecagonal numbers
int sum_center_octadecagon_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += center_octadecagon_num(i);
}
return summ;
}
// Driver Code
int main()
{
int n = 3;
cout << (sum_center_octadecagon_num(n));
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find the sum of the
// first N centered octadecagonal numbers
class GFG {
// Function to find the N-th centered
// octadecagonal number
static int center_octadecagon_num(int n)
{
// Formula to calculate
// nth centered octadecagonal
// number
return (9 * n * n - 9 * n + 1);
}
// Function to find the sum of the first
// N centered octadecagonal numbers
static int sum_center_octadecagon_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += center_octadecagon_num(i);
}
return summ;
}
// Driver Code
public static void main(String[] args)
{
int n = 3;
System.out.println(sum_center_octadecagon_num(n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find the sum of
# the first N Centered Octadecagonal
# numbers
# Function to find the N-th
# Centered octadecagonal
# number
def center_octadecagon_num(n):
# Formula to calculate
# nth centered octadecagonal
# number
return (9 * n * n - 9 * n + 1)
# Function to find the sum of
# the first N Centered
# octadecagonal numbers
def sum_center_octadecagon_num(n) :
# Variable to store
# the sum
summ = 0
# Iterating through the range
# 1 to N
for i in range(1, n + 1):
summ += center_octadecagon_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 3
print(sum_center_octadecagon_num(n))
C#
// C# program to find the sum of the
// first N centered octadecagonal numbers
using System;
class GFG {
// Function to find the N-th centered
// octadecagonal number
static int center_octadecagon_num(int n)
{
// Formula to calculate
// nth centered octadecagonal
// number
return (9 * n * n - 9 * n + 1);
}
// Function to find the sum of the first
// N centered octadecagonal numbers
static int sum_center_octadecagon_num(int n)
{
// Variable to store
// the sum
int summ = 0;
// Iterating through the range
// 1 to N
for(int i = 1; i < n + 1; i++)
{
summ += center_octadecagon_num(i);
}
return summ;
}
// Driver Code
public static void Main(String[] args)
{
int n = 3;
Console.WriteLine(sum_center_octadecagon_num(n));
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
75
时间复杂度: O(N)。