📜  求第N个第17个十六进制数字的总和

📅  最后修改于: 2021-05-04 13:16:49             🧑  作者: Mango

给定数字N,任务是找到前N个七边形数字的总和。

例子:

方法:

  1. 最初,我们需要创建一个函数,该函数将帮助我们计算第N七边形数。
  2. 现在,运行一个从1到N的循环,以找到i七边形数。
  3. 将所有以上计算的七边形数相加。
  4. 最后,显示前N个七边形数字的总和。

下面是上述方法的实现:

C++
// C++ program to find the sum of the
// first N heptadecagonal numbers
#include 
using namespace std;
 
// Function to find the N-th
// heptadecagonal number
int heptadecagonal_num(int n)
{
     
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
 
// Function to find the sum of the
// first N heptadecagonal numbers
int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 5;
     
    cout << sum_heptadecagonal_num(n);
}
 
// This code is contributed by coder001


Java
// Java program to find the sum of the
// first N heptadecagonal numbers
class GFG{
     
// Function to find the N-th
// heptadecagonal number
public static int heptadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
     
// Function to find the sum of the
// first N heptadecagonal numbers
public static int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code    
public static void main(String[] args)
{
    int n = 5;
     
    System.out.println(sum_heptadecagonal_num(n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the sum
# of the first N 
# heptadecagonal numbers
 
# Function to find the
# N-th heptadecagonal
# number
def heptadecagonal_num(n):
 
    # Formula to calculate 
    # nth heptadecagonal
    # number
    return ((15 * n * n) - 13 * n) // 2
     
   
# Function to find the
# sum of the first N
# heptadecagonal numbers
def sum_heptadecagonal_num(n) :
     
    # Variable to store
    # the sum
    summ = 0
     
    # Iterate from 1 to N
    for i in range(1, n + 1):
 
        summ += heptadecagonal_num(i)
     
    return summ
   
# Driver code
if __name__ == '__main__' :
           
    n = 5
     
    print(sum_heptadecagonal_num(n))


C#
// C# program to find the sum of the
// first N heptadecagonal numbers
using System;
 
class GFG{
     
// Function to find the N-th
// heptadecagonal number
public static int heptadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // heptadecagonal number
    return ((15 * n * n) - 13 * n) / 2;
}
     
// Function to find the sum of the
// first N heptadecagonal numbers
public static int sum_heptadecagonal_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 += heptadecagonal_num(i);
    }
    return summ;
}
 
// Driver code
public static void Main()
{
    int n = 5;
     
    Console.WriteLine(sum_heptadecagonal_num(n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
315

时间复杂度: O(N)。