Dado un número n, encuentre el n-ésimo número decagonal centrado.
Un número decagonal centrado es un número figurativo centrado que representa un decágono con un punto en el centro y todos los demás puntos que lo rodean en forma decagonal sucesiva. Fuente [Wiki] .
Los primeros números decagonales centrados son:
1, 11, 31, 61, 101, 151, 211, 281, 361, 451, 551, 661…………
Ejemplos:
Input : 3 Output : 31 Input : 6 Output : 151
En matemáticas, el número decagonal centrado para el término n viene dado por:
A continuación se muestra la implementación básica de la idea anterior.
C++
// Program to find nth // centered decagonal // number #include <bits/stdc++.h> using namespace std; // Centered decagonal // number function int centereddecagonalnum(int n) { // Formula to calculate nth // centered decagonal number & // return it into main function. return (5 * n * n + 5 * n + 1); } // Driver Code int main() { int n = 5; cout << n << "th centered decagonal" << "number: "; cout << centereddecagonalnum(n); cout << endl; n = 9; cout << n << "th centered decagonal" << "number: "; cout << centereddecagonalnum(n); return 0; }
C
// C Program to find nth // centered decagonal // number #include <stdio.h> // Centered decagonal // number function int centereddecagonalnum(int n) { // Formula to calculate nth // centered decagonal number & // return it into main function. return (5 * n * n + 5 * n + 1); } // Driver Code int main() { int n = 5; printf("%dth centered decagonal number: ",n); printf("%d\n",centereddecagonalnum(n)); n = 9; printf("%dth centered decagonal number: ",n); printf("%d\n",centereddecagonalnum(n)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java Program to find nth // centered decagonal number import java.io.*; class GFG { // Centered decagonal // number function static int centereddecagonalnum(int n) { // Formula to calculate nth // centered decagonal number & // return it into main function. return (5 * n * n + 5 * n + 1); } // Driver Code public static void main (String[] args) { int n = 5; System.out.print(n + "th centered " + "decagonal number: "); System.out.println(centereddecagonalnum(n)); n = 9; System.out.print(n + "th centered " + "decagonal number: "); System.out.println(centereddecagonalnum(n)); } } // This code is contributed by m_kit
Python3
# Program to find nth # centered decagonal number # Centered decagonal # number function def centereddecagonalnum(n) : # Formula to calculate # nth centered decagonal # number & return it # into main function. return (5 * n * n + 5 * n + 1) # Driver Code if __name__ == '__main__' : n = 5 print(n,"th centered decagonal " + "number : ", centereddecagonalnum(n)) n = 9 print(n,"th centered decagonal " + "number : ", centereddecagonalnum(n)) # This code is contributed by m_kit
C#
// Program to find nth // centered decagonal // number using System; class GFG { // Centered decagonal // number function static int centereddecagonalnum(int n) { // Formula to calculate nth // centered decagonal number & // return it into main function. return (5 * n * n + 5 * n + 1); } // Driver Code static public void Main () { int n = 5; Console.Write(n + "th centered decagonal"+ "number: "); Console.WriteLine(centereddecagonalnum(n)); n = 9; Console.Write(n + "th centered decagonal"+ "number: "); Console.WriteLine(centereddecagonalnum(n)); } } // This code is contributed by aj_36
PHP
<?php // Program to find nth // centered decagonal number // Centered decagonal // number function function centereddecagonalnum($n) { // Formula to calculate // nth centered decagonal // number & return it // into main function. return (5 * $n * $n + 5 * $n + 1); } // Driver Code $n = 5; echo $n , "th centered decagonal", "number: "; echo centereddecagonalnum($n); echo "\n"; $n = 9; echo $n , "th centered decagonal", "number: "; echo centereddecagonalnum($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript Program to find nth // centered decagonal number // Centered decagonal // number function function centereddecagonalnum(n) { // Formula to calculate nth // centered decagonal number & // return it into main function. return (5 * n * n + 5 * n + 1); } // Driver Code var n = 5; document.write(n + "th centered " + "decagonal number: "); document.write(centereddecagonalnum(n) + "<br>"); n = 9; document.write(n + "th centered " + "decagonal number: "); document.write(centereddecagonalnum(n)); // This code is contributed by Kirti </script>
Producción
5th centered decagonalnumber: 151 9th centered decagonalnumber: 451
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)