Dado un número n , la tarea es encontrar el n -ésimo número heptagonal centrado.
El número heptagonal centrado es un número de figura centrado que representa un heptágono con un punto en el centro y todos los demás puntos que lo rodean en forma heptagonal. El N número heptagonal centrado puede calcularse usando la fórmula (7n 2 – 7n + 2) / 2 .
Ejemplos:
Input : n = 2 Output : 8 Input : n = 7 Output : 148
Consulte este diagrama para ver la representación pictórica.
A continuación se muestra la implementación:
C++
// CPP program to find n-th // Centered heptagonal number #include <bits/stdc++.h> using namespace std; // Function to find Centered // heptagonal number int centered_heptagonal_num(long int n) { // Formula to calculate nth // Centered heptagonal number return (7 * n * n - 7 * n + 2) / 2; } // Driver Code int main() { long int n = 5; cout << n << "th Centered heptagonal number : "; cout << centered_heptagonal_num(n); return 0; }
C
// C program to find n-th // Centered heptagonal number #include <stdio.h> // Function to find Centered // heptagonal number int centered_heptagonal_num(long int n) { // Formula to calculate nth // Centered heptagonal number return (7 * n * n - 7 * n + 2) / 2; } // Driver Code int main() { long int n = 5; printf("%ldth Centered heptagonal number : ",n); printf("%d",centered_heptagonal_num(n)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to find n-th Centered // heptagonal number import java.io.*; class GFG { // Function to find Centered heptagonal // number static long centered_heptagonal_num(long n) { // Formula to calculate nth // Centered heptagonal number return (7 * n * n - 7 * n + 2) / 2; } // Driver Code public static void main (String[] args) { long n = 5; System.out.println( n + "th Centered " + "heptagonal number : " + centered_heptagonal_num(n)); } } // This code is contributed by anuj_67.
Python3
# Python program to find nth # Centered heptagonal number # Function to find Centered # heptagonal number def centered_heptagonal_num(n): # Formula to calculate nth # Centered heptagonal number return (7 * n * n - 7 * n + 2) // 2 # Driver Code n = 5 print("%sth Centered heptagonal number : " %n, centered_heptagonal_num(n))
C#
//C# program to find n-th Centered // heptagonal number using System; class GFG { // Function to find Centered heptagonal // number static long centered_heptagonal_num(long n) { // Formula to calculate nth // Centered heptagonal number return (7 * n * n - 7 * n + 2) / 2; } // Driver Code public static void Main () { long n = 5; Console.WriteLine( n + "th Centered " + "heptagonal number : " + centered_heptagonal_num(n)); } } // This code is contributed by anuj_67.
PHP
<?php // PHP program to find n-th // Centered heptagonal number // Function to find Centered // heptagonal number function centered_heptagonal_num($n) { // Formula to calculate nth // Centered heptagonal number return (7 * $n * $n - 7 * $n + 2) / 2; } // Driver Code $n = 5; echo $n ,"th Centered heptagonal number : "; echo centered_heptagonal_num($n); // This code is contributed by m_kit ?>
Javascript
<script> // Javascript program to find n-th // Centered heptagonal number // Function to find Centered // heptagonal number function centered_heptagonal_num(n) { // Formula to calculate nth // Centered heptagonal number return parseInt((7 * n * n - 7 * n + 2) / 2); } // Driver Code let n = 5; document.write(n + "th Centered heptagonal number : "); document.write(centered_heptagonal_num(n)); // This code is contributed by rishavmahato348. </script>
Producción :
5th Centered heptagonal number : 71
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)