Dado un número n, encuentra el n-ésimo número piramidal pentagonal.
Un número piramidal pentagonal pertenece a la clase de números figurados. Es el número de objetos en una pirámide de base pentagonal. El enésimo número piramidal pentagonal es igual a la suma de los primeros n números pentagonales .
Ejemplos:
Input : n = 3 Output : 18 Input : n = 7 Output : 196
Método 1: (Enfoque ingenuo):
este enfoque es simple. Dice que hay que sumar todos los números pentagonales hasta n (ejecutando un bucle) para obtener el número piramidal pentagonal n-ésimo.
A continuación se muestra la implementación de este enfoque:
C++
// CPP Program to get nth Pentagonal // pyramidal number. #include <bits/stdc++.h> 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
<?php // PHP Program to get nth // Pentagonal pyramidal number. // function to get nth // Pentagonal pyramidal number. function pentagon_pyramidal($n) { $sum = 0; // Running loop from 1 to n for ($i = 1; $i <= $n; $i++) { // get nth pentagonal number $p = (3 * $i * $i - $i) / 2; // add to sum $sum = $sum + $p; } return $sum; } // Driver Code $n = 4; echo pentagon_pyramidal($n); // This code is contributed by m_kit ?>
Javascript
<script> // javascript Program to get nth // Pentagonal pyramidal number. // function to get nth // Pentagonal pyramidal number. function pentagon_pyramidal(n) { var sum = 0; // Running loop from 1 to n for (i = 1; i <= n; i++) { // get nth pentagonal number var p = (3 * i * i - i) / 2; // add to sum sum = sum + p; } return sum; } // Driver Code var n = 4; document.write(pentagon_pyramidal(n)); // This code is contributed by Amit Katiyar </script>
Producción :
40
Complejidad de tiempo : O(n)
Método 2: (Enfoque eficiente):
en este enfoque, usamos la fórmula para obtener el enésimo número piramidal pentagonal en tiempo O(1).
Enésimo número piramidal pentagonal = n 2 (n + 1) / 2
A continuación se muestra la implementación de este enfoque:
C++
// CPP Program to get nth Pentagonal // pyramidal number. #include <bits/stdc++.h> 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
<?php // PHP Program to get // nth Pentagonal // pyramidal number. // function to get // nth Pentagonal // pyramidal number. function pentagon_pyramidal($n) { return $n * $n * ($n + 1) / 2; } // Driver Code $n = 4; echo pentagon_pyramidal($n); // This code is contributed // by akt_mit ?>
Javascript
<script> // javascript Program to get nth // Pentagonal pyramidal number. // function to get nth // Pentagonal pyramidal number. function pentagon_pyramidal(n) { return n * n * (n + 1) / 2; } // Driver Code var n = 4; document.write(pentagon_pyramidal(n)); // This code is contributed by Princi Singh </script>
Producción :
40
Complejidad de tiempo : O(1)
Publicación traducida automáticamente
Artículo escrito por Prasad_Kshirsagar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA