Dado un número N , la tarea es encontrar la suma de los primeros N números pentagonales centrados.
Los primeros números pentagonales centrados son 1, 6, 16, 31, 51, 76, 106…
Ejemplos:
Entrada: N = 3
Salida: 23
Explicación:
1, 6 y 16 son los tres primeros
números pentagonales centrados.
Entrada: N = 5
Salida: 105
Enfoque: La idea es crear primero una función que nos ayude a encontrar el número pentagonal centrado en un tiempo constante. La implementación de esta función ya se ha discutido en este artículo . Los siguientes pasos se siguen después de crear esta función:
- Ejecute un ciclo comenzando desde 1 hasta N, para encontrar el i -ésimo número pentagonal centrado .
- Sume todos los números pentagonales centrados calculados anteriormente .
- Luego, muestre la suma de N números pentagonales centrados .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the sum of the // first N centered pentagonal numbers #include<bits/stdc++.h> using namespace std; // Function to find the // Centered_Pentagonal number int Centered_Pentagonal_num(int n) { // Formula to calculate // nth Centered_Pentagonal // number & return it // into main function. return (5 * n * n - 5 * n + 2) / 2; } // Function to find the sum of the first // N Centered_Pentagonal numbers int sum_Centered_Pentagonal_num(int n) { // To get the sum int summ = 0; // Iterating through the range // 1 to N for(int i = 1; i < n + 1; i++) { summ += Centered_Pentagonal_num(i); } return summ; } // Driver Code int main() { int n = 5; // Display first Nth // Centered_Pentagonal number cout << (sum_Centered_Pentagonal_num(n)); return 0; } // This code is contributed by PratikBasu
Java
// Java program to find the sum of the // first N centered pentagonal numbers class GFG{ // Function to find the // Centered_Pentagonal number static int Centered_Pentagonal_num(int n) { // Formula to calculate // nth Centered_Pentagonal // number & return it // into main function. return (5 * n * n - 5 * n + 2) / 2; } // Function to find the sum of the first // N Centered_Pentagonal numbers static int sum_Centered_Pentagonal_num(int n) { // To get the sum int summ = 0; // Iterating through the range // 1 to N for(int i = 1; i < n + 1; i++) { summ += Centered_Pentagonal_num(i); } return summ; } // Driver Code public static void main(String[] args) { int n = 5; // Display first Nth // Centered_Pentagonal number System.out.print((sum_Centered_Pentagonal_num(n))); } } // This code is contributed by sapnasingh4991
Python3
# Python3 program to find the sum of # the first N Centered # Pentagonal number # Function to find the # Centered_Pentagonal number def Centered_Pentagonal_num(n): # Formula to calculate # nth Centered_Pentagonal # number & return it # into main function. return (5 * n * n - 5 * n + 2) // 2 # Function to find the # sum of the first N # Centered_Pentagonal # numbers def sum_Centered_Pentagonal_num(n) : # To get the sum summ = 0 for i in range(1, n + 1): # Function to get the # Centered_Pentagonal_num summ += Centered_Pentagonal_num(i) return summ # Driver Code if __name__ == '__main__' : n = 5 # display first Nth # Centered_Pentagonal number print(sum_Centered_Pentagonal_num(n))
C#
// C# program to find the sum of the // first N centered pentagonal numbers using System; class GFG{ // Function to find the // Centered_Pentagonal number static int Centered_Pentagonal_num(int n) { // Formula to calculate // nth Centered_Pentagonal // number & return it // into main function. return (5 * n * n - 5 * n + 2) / 2; } // Function to find the sum of the first // N Centered_Pentagonal numbers static int sum_Centered_Pentagonal_num(int n) { // To get the sum int summ = 0; // Iterating through the range // 1 to N for(int i = 1; i < n + 1; i++) { summ += Centered_Pentagonal_num(i); } return summ; } // Driver code public static void Main(String[] args) { int n = 5; // Display first Nth // Centered_Pentagonal number Console.Write((sum_Centered_Pentagonal_num(n))); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript program to find the sum of the // first N centered pentagonal numbers // Function to find the // Centered_Pentagonal number function Centered_Pentagonal_num(n) { // Formula to calculate // nth Centered_Pentagonal // number & return it // into main function. return (5 * n * n - 5 * n + 2) / 2; } // Function to find the sum of the first // N Centered_Pentagonal numbers function sum_Centered_Pentagonal_num(n) { // To get the sum let summ = 0; // Iterating through the range // 1 to N for(let i = 1; i < n + 1; i++) { summ += Centered_Pentagonal_num(i); } return summ; } let n = 5; // Display first Nth // Centered_Pentagonal number document.write(sum_Centered_Pentagonal_num(n)); </script>
105
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA