给定数字N ,任务是找到前N个二十进制正弦数的总和。
The first few Icosidigonal numbers are 1, 22, 63, 124, 205, 306 …
例子:
Input: N = 3
Output: 86
Explanation:
1, 22 and 63 are the first three Icosidigonal numbers.
Input: N = 6
Output: 721
方法:
- 最初,我们需要创建一个函数来帮助我们计算第N个正弦数。
- 现在,运行开始从1到N循环,找到第i个icosidigonal号码。
- 将所有以上计算出的二十边形数字相加。
- 最后,显示前N个二十进制对角线数字的总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// first N icosidigonal numbers
#include
using namespace std;
// Function to find the
// N-th icosidigonal number
int Icosidigonal_num(int n)
{
// Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
}
// Function to find the sum of the
// first N icosidigonal number
int sum_Icosidigonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating in the range 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
}
// Driver code
int main()
{
int n = 6;
// Display first Nth
// icosidigonal numbers
cout << sum_Icosidigonal_num(n);
}
// This code is contributed by coder001
Java
// Java program to find the sum of the
// first N icosidigonal numbers
class GFG{
// Function to find the
// N-th icosidigonal number
public static int Icosidigonal_num(int n)
{
// Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
}
// Function to find the sum of the
// first N icosidigonal number
public static int sum_Icosidigonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating in the range 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
}
// Driver code
public static void main(String[] args)
{
int n = 6;
// Display first Nth
// icosidigonal numbers
System.out.println(sum_Icosidigonal_num(n));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to find the
# sum of the first N
# Icosidigonal numbers
# Function to find the
# N-th Icosidigonal
# number
def Icosidigonal_num(n):
# Formula to calculate
# nth Icosidigonal
# number
return (20 * n * n -
18 * n) // 2
# Function to find the
# sum of the first N
# Icosidigonal number
def sum_Icosidigonal_num(n) :
# Variable to store
# the sum
summ = 0
# Iterating in the range
# 1 to N
for i in range(1, n + 1):
summ += Icosidigonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 6
print(sum_Icosidigonal_num(n))
C#
// C# program to find the sum of the
// first N icosidigonal numbers
using System;
class GFG{
// Function to find the
// N-th icosidigonal number
static int Icosidigonal_num(int n)
{
// Formula to calculate
// nth icosidigonal number
return (20 * n * n - 18 * n) / 2;
}
// Function to find the sum of the
// first N icosidigonal number
static int sum_Icosidigonal_num(int n)
{
// Variable to store the sum
int summ = 0;
// Iterating in the range 1 to N
for(int i = 1; i < n + 1; i++)
{
// Finding the sum
summ += Icosidigonal_num(i);
}
return summ;
}
// Driver code
public static void Main(string[] args)
{
int n = 6;
// Display first Nth
// icosidigonal numbers
Console.WriteLine(sum_Icosidigonal_num(n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
721
时间复杂度: O(N)。