📜  求第一个中心为N的十六进制数字的总和

📅  最后修改于: 2021-04-27 22:47:18             🧑  作者: Mango

给定数字N,任务是找到第一个N个中心十六进制数之和。

例子:

方法:

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

下面是上述方法的实现:

C++
// C++ program to find the sum of the first
// N centred hexadecagonal numbers
#include 
using namespace std;
 
// Centered_Hexadecagonal
// number function
int Centered_Hexadecagonal_num(int n)
{
     
    // Formula to calculate nth
    // Centered_Hexadecagonal
    // number & return it into
    // main function.
    return (8 * n * n - 8 * n + 1);
}
 
// Function to find the sum of the first
// N centered hexadecagonal number
int sum_Centered_Hexadecagonal_num(int n)
{
     
    // Variable to store the sum
    int summ = 0;
     
    // Loop to iterate through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
       // Finding the sum
       summ += Centered_Hexadecagonal_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 5;
     
    // Display first Nth
    // Centered_Hexadecagonal number
    cout << sum_Centered_Hexadecagonal_num(n);
}
 
// This code is contributed by coder001


Java
// Java program to find the sum of the first
// N centred hexadecagonal numbers
 
class GFG{
     
// Centered_Hexadecagonal
// number function
public static int Centered_Hexadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // Centered_Hexadecagonal
    // number & return it into
    // main function.
    return (8 * n * n - 8 * n + 1);
}
     
// Function to find the sum of the first
// N centered hexadecagonal number
public static int sum_Centered_Hexadecagonal_num(int n)
{
         
    // Variable to store the sum
    int summ = 0;
         
    // Loop to iterate through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
         
       // Finding the sum
       summ += Centered_Hexadecagonal_num(i);
    }
    return summ;
}
 
// Driver Code   
public static void main(String[] args)
{
    int n = 5;
     
    // Display first Nth
    // Centered_Hexadecagonal number
    System.out.println(sum_Centered_Hexadecagonal_num(n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the sum of
# the first N centred
# hexadecagonal numbers
 
# Centered_Hexadecagonal
# number function
def Centered_Hexadecagonal_num(n):
    # Formula to calculate 
    # nth Centered_Hexadecagonal
    # number & return it
    # into main function.
    return (8 * n * n -
            8 * n + 1)
     
   
# Function to find the
# sum of the first N
# Centered Hexadecagonal
# number
def sum_Centered_Hexadecagonal_num(n) :
     
    # Variable to store the
    # sum
    summ = 0
     
    # Loop to iterate through the
    # first N numbers
    for i in range(1, n + 1):
 
        # Find the sum
        summ += Centered_Hexadecagonal_num(i)
     
    return summ
   
# Driver Code
if __name__ == '__main__' :
           
    n = 5
     
    # display first Nth
    # Centered_Hexadecagonal number
    print(sum_Centered_Hexadecagonal_num(n))


C#
// C# program to find the sum of the first
// N centred hexadecagonal numbers
using System;
 
class GFG{
     
// Centered_Hexadecagonal
// number function
public static int Centered_Hexadecagonal_num(int n)
{
         
    // Formula to calculate nth
    // Centered_Hexadecagonal
    // number & return it into
    // main function.
    return (8 * n * n - 8 * n + 1);
}
     
// Function to find the sum of the first
// N centered hexadecagonal number
public static int sum_Centered_Hexadecagonal_num(int n)
{
         
    // Variable to store the sum
    int summ = 0;
         
    // Loop to iterate through the
    // first N numbers
    for(int i = 1; i < n + 1; i++)
    {
        
       // Finding the sum
       summ += Centered_Hexadecagonal_num(i);
    }
    return summ;
}
 
// Driver Code
public static void Main()
{
    int n = 5;
     
    // Display first Nth
    // Centered_Hexadecagonal number
    Console.Write(sum_Centered_Hexadecagonal_num(n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
325

时间复杂度: O(N)