Los números que se pueden ordenar para formar un rectángulo se llaman números rectangulares (también conocidos como números pronicos). Los primeros números rectangulares son:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 . . . . . .
Dado un número n, encuentra el n-ésimo número rectangular.
Ejemplos:
Input : 1 Output : 2 Input : 4 Output : 20 Input : 5 Output : 30
El número 2 es un número rectangular porque tiene 1 fila por 2 columnas. El número 6 es un número rectangular porque tiene 2 filas por 3 columnas, y el número 12 es un número rectangular porque tiene 3 filas por 4 columnas.
Si observamos estos números cuidadosamente, podemos notar que el n-ésimo número rectangular es n(n+1) .
C++
// CPP Program to find n-th rectangular number #include <bits/stdc++.h> using namespace std; // Returns n-th rectangular number int findRectNum(int n) { return n * (n + 1); } // Driver code int main() { int n = 6; cout << findRectNum(n); return 0; }
Java
// Java Program to find n-th rectangular number import java.io.*; class GFG { // Returns n-th rectangular number static int findRectNum(int n) { return n * (n + 1); } // Driver code public static void main(String[] args) { int n = 6; System.out.println(findRectNum(n)); } } // This code is contributed by vt_m.
C#
// C# Program to find n-th rectangular number using System; class GFG { // Returns n-th rectangular number static int findRectNum(int n) { return n * (n + 1); } // Driver code public static void Main() { int n = 6; Console.Write(findRectNum(n)); } } // This code is contributed by vt_m.
Python
# Python3 Program to find n-th rectangular number # Returns n-th rectangular number def findRectNum(n): return n*(n + 1) # Driver code n = 6 print (findRectNum(n)) # This code is contributed by Shreyanshi Arun.
PHP
<?php // PHP Program to find n-th // rectangular number // Returns n-th rectangular // number function findRectNum($n) { return $n * ($n + 1); } // Driver Code $n = 6; echo findRectNum($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript Program to find n-th rectangular number // Returns n-th rectangular number function findRectNum(n) { return n * (n + 1); } // Driver code var n = 6; document.write(findRectNum(n)); // This code is contributed by noob2000. </script>
Producción:
42
Complejidad temporal : O(1) desde la realización de operaciones constantes
Complejidad espacial : O (1) ya que se usa espacio constante para variables
Comprobar si un número dado es Pronic | Enfoque eficiente
Este artículo es una contribución de DANISH_RAZA . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA