Dado un número n, encuentra el n-ésimo Número Hexagonal Centrado.
Un número hexadecagonal centrado representa un punto en el centro y otros puntos a su alrededor en sucesivas capas hexadecagonales (polígono de 16 lados).
Los primeros números hexadecagonales centrados son:
1, 17, 49, 97, 161, 241, 337, 449, 577, 721, 881………………….
Ejemplos:
Input : 3 Output : 49 Input : 10 Output : 721
En matemáticas, el número hexagonal centrado para el término n-ésimo viene dado por:
A continuación se muestra la implementación básica de la idea anterior:
C++
// C++ Program to find // nth centered hexadecagonal // number #include <bits/stdc++.h> using namespace std; // centered hexadecagonal function int center_hexadecagonal_num(long int n) { // Formula to calculate nth // centered hexadecagonal number return 8 * n * n - 8 * n + 1; } // Driver Code int main() { long int n = 2; cout << n << "th centered hexadecagonal number : " << center_hexadecagonal_num(n); cout << endl; n = 12; cout << n << "th centered hexadecagonal number : " << center_hexadecagonal_num(n); return 0; }
C
// C Program to find // nth centered hexadecagonal // number #include <stdio.h> // centered hexadecagonal function int center_hexadecagonal_num(long int n) { // Formula to calculate nth // centered hexadecagonal number return 8 * n * n - 8 * n + 1; } // Driver Code int main() { long int n = 2; printf("%ldth centered hexadecagonal number : %d\n",n,center_hexadecagonal_num(n)); n = 12; printf("%ldth centered hexadecagonal number : %d\n",n,center_hexadecagonal_num(n)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java Program to find nth // centered hexadecagonal number import java.io.*; class GFG { // centered hexadecagonal function static int center_hexadecagonal_num(int n) { // Formula to calculate nth // centered hexadecagonal number return 8 * n * n - 8 * n + 1; } // Driver Code public static void main(String args[]) { int n = 2; System.out.print(n + "th centered " + "hexadecagonal number: "); System.out.println(center_hexadecagonal_num(n)); n = 12; System.out.print(n + "th centered " + "hexadecagonal number: "); System.out.println(center_hexadecagonal_num(n)); } } // This code is contributed by ajit.
Python3
# Program to find nth # centered hexadecagonal # number # centered hexadecagonal # function def center_hexadecagonal_num(n): # Formula to calculate # nth centered hexadecagonal # number return 8 * n * n - 8 * n + 1 # Driver Code if __name__ == '__main__' : n = 2 print(n,"nd centered hexadecagonal " + "number : ", center_hexadecagonal_num(n)) n = 12 print(n,"th centered hexadecagonal " + "number : ", center_hexadecagonal_num(n)) # This code is contributed # by akt_mit
C#
// C# Program to find nth // centered hexadecagonal number using System; class GFG { // centered hexadecagonal // function static int center_hexadecagonal_num(int n) { // Formula to calculate nth // centered hexadecagonal number return 8 * n * n - 8 * n + 1; } // Driver Code static public void Main () { int n = 2; Console.Write(n + "th centered " + "hexadecagonal number: "); Console.WriteLine(center_hexadecagonal_num(n)); n = 12; Console.Write(n + "th centered " + "hexadecagonal number: "); Console.WriteLine(center_hexadecagonal_num(n)); } } // This code is contributed by m_kit
PHP
<?php // PHP Program to find // nth centered hexadecagonal // number // centered hexadecagonal function function center_hexadecagonal_num($n) { // Formula to calculate nth // centered hexadecagonal number return 8 * $n * $n - 8 * $n + 1; } // Driver Code $n = 2; echo $n , "th centered hexadecagonal number : ", center_hexadecagonal_num($n); echo "\n"; $n = 12; echo $n , "th centered hexadecagonal numbe : ", center_hexadecagonal_num($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript Program to find nth // centered hexadecagonal number // Centered hexadecagonal function function center_hexadecagonal_num(n) { // Formula to calculate nth // centered hexadecagonal number return 8 * n * n - 8 * n + 1; } // Driver code var n = 2; document.write(n + "th centered " + "hexadecagonal number: "); document.write(center_hexadecagonal_num(n) + "<br>"); n = 12; document.write(n + "th centered " + "hexadecagonal number: "); document.write(center_hexadecagonal_num(n)); // This code is contributed by Ankita saini </script>
Producción :
2th centered hexadecagonal number : 17 12th centered hexadecagonal number : 1057
Complejidad temporal: O(1)
Espacio auxiliar: O(1)
Referencias:
http://oeis.org/A069129