📜  五角锥数

📅  最后修改于: 2021-04-24 04:22:16             🧑  作者: Mango

给定一个数n,找到第n个五角锥体数。
五边形金字塔形数字属于图形数字类。它是具有五边形底角的金字塔中的对象数。第n个五角锥体数等于前n个五角锥体数之和。
例子:

Input : n = 3 
Output : 18

Input : n = 7
Output : 196

方法1 :(幼稚方法):
这种方法很简单。它说将所有五角形数加到n(通过运行循环)以获得第n个五角形锥数。

以下是此方法的实现:

C++
// CPP Program to get nth Pentagonal
// pyramidal number.
#include 
using namespace std;
 
// function to get nth Pentagonal
// pyramidal number.
int pentagon_pyramidal(int n)
{
    int sum = 0;
 
    // Running loop from 1 to n
    for (int i = 1; i <= n; i++) {
 
        // get nth pentagonal number
        int p = (3 * i * i - i) / 2;
 
        // add to sum
        sum = sum + p;
    }
    return sum;
}
 
// Driver Program
int main()
{
    int n = 4;
    cout << pentagon_pyramidal(n) << endl;
    return 0;
}


Java
// Java Program to get nth
// Pentagonal pyramidal number.
import java.io.*;
 
class GFG
{
 
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    int sum = 0;
 
    // Running loop from 1 to n
    for (int i = 1; i <= n; i++)
    {
 
        // get nth pentagonal number
        int p = (3 * i * i - i) / 2;
 
        // add to sum
        sum = sum + p;
    }
    return sum;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    System.out.println(pentagon_pyramidal(n));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python3 Program to get nth Pentagonal
# pyramidal number.
   
# function to get nth Pentagonal
# pyramidal number.
def pentagon_pyramidal(n):
    sum = 0
 
    # Running loop from 1 to n
    for i in range(1, n + 1):
   
        # get nth pentagonal number
        p = ( 3 * i * i - i ) / 2
 
        # add to sum
        sum = sum + p      
  
    return sum
 
   
# Driver Program
n = 4
print(int(pentagon_pyramidal(n)))


C#
// C# Program to get nth
// Pentagonal pyramidal number.
using System;
 
class GFG
{
     
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    int sum = 0;
 
    // Running loop from 1 to n
    for (int i = 1; i <= n; i++)
    {
 
        // get nth pentagonal number
        int p = (3 * i *
                 i - i) / 2;
 
        // add to sum
        sum = sum + p;
    }
    return sum;
}
 
// Driver Code
static public void Main ()
{
    int n = 4;
    Console.WriteLine(pentagon_pyramidal(n));
}
}
 
// This code is contributed by ajit.


PHP


Javascript


C++
// CPP Program to get nth Pentagonal
// pyramidal number.
#include 
using namespace std;
 
// function to get nth Pentagonal
// pyramidal number.
int pentagon_pyramidal(int n)
{
    return n * n * (n + 1) / 2;
}
 
// Driver Program
int main()
{
    int n = 4;
    cout << pentagon_pyramidal(n) << endl;
    return 0;
}


Java
// Java Program to get nth
// Pentagonal pyramidal number.
import java.io.*;
 
class GFG
{
     
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    return n * n *
          (n + 1) / 2;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    System.out.println(pentagon_pyramidal(n));
}
}
 
// This code is contributed by ajit


Python3
# Python3 Program to get nth Pentagonal
# pyramidal number.
   
# function to get nth Pentagonal
# pyramidal number.
def pentagon_pyramidal(n):    
    return n * n * (n + 1) / 2
 
   
# Driver Program
n = 4
print(int(pentagon_pyramidal(n)))


C#
// C# Program to get nth
// Pentagonal pyramidal number.
using System;
 
class GFG
{
     
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    return n * n *
          (n + 1) / 2;
}
 
// Driver Code
static public void Main ()
{
    int n = 4;
    Console.WriteLine(
            pentagon_pyramidal(n));
}
}
 
// This code is contributed
// by ajit


PHP


Javascript


输出 :

40

时间复杂度: O(n)

方法2 :(有效方法):
在这种方法中,我们使用公式来获得O(1)时间中的第n个五角锥体数。

第n个五角锥金字塔数= n 2 (n + 1)/ 2
以下是此方法的实现:

C++

// CPP Program to get nth Pentagonal
// pyramidal number.
#include 
using namespace std;
 
// function to get nth Pentagonal
// pyramidal number.
int pentagon_pyramidal(int n)
{
    return n * n * (n + 1) / 2;
}
 
// Driver Program
int main()
{
    int n = 4;
    cout << pentagon_pyramidal(n) << endl;
    return 0;
}

Java

// Java Program to get nth
// Pentagonal pyramidal number.
import java.io.*;
 
class GFG
{
     
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    return n * n *
          (n + 1) / 2;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    System.out.println(pentagon_pyramidal(n));
}
}
 
// This code is contributed by ajit

Python3

# Python3 Program to get nth Pentagonal
# pyramidal number.
   
# function to get nth Pentagonal
# pyramidal number.
def pentagon_pyramidal(n):    
    return n * n * (n + 1) / 2
 
   
# Driver Program
n = 4
print(int(pentagon_pyramidal(n)))

C#

// C# Program to get nth
// Pentagonal pyramidal number.
using System;
 
class GFG
{
     
// function to get nth
// Pentagonal pyramidal number.
static int pentagon_pyramidal(int n)
{
    return n * n *
          (n + 1) / 2;
}
 
// Driver Code
static public void Main ()
{
    int n = 4;
    Console.WriteLine(
            pentagon_pyramidal(n));
}
}
 
// This code is contributed
// by ajit

的PHP


Java脚本


输出 :

40

时间复杂度: O(1)