Dado un número N, la tarea es encontrar el siguiente cuadrado perfecto mayor que N.
Ejemplos :
Input: N = 6 Output: 9 9 is a greater number than 6 and is also a perfect square Input: N = 9 Output: 16
Acercarse:
- Encuentre la raíz cuadrada de N dada.
- Calcule su valor mínimo utilizando la función de suelo en C++ .
- Luego súmale 1.
- Imprima el cuadrado de ese número.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <iostream> #include<cmath> using namespace std; // Function to find the next perfect square int nextPerfectSquare(int N) { int nextN = floor(sqrt(N)) + 1; return nextN * nextN; } // Driver Code int main() { int n = 35; cout << nextPerfectSquare(n); return 0; }
Java
// Java implementation of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to find the // next perfect square static int nextPerfectSquare(int N) { int nextN = (int)Math.floor(Math.sqrt(N)) + 1; return nextN * nextN; } // Driver Code public static void main(String args[]) { int n = 35; System.out.println (nextPerfectSquare(n)); } } // This code is contributed by Subhadeep
Python3
# Python3 implementation of above approach import math #Function to find the next perfect square def nextPerfectSquare(N): nextN = math.floor(math.sqrt(N)) + 1 return nextN * nextN if __name__=='__main__': N = 35 print(nextPerfectSquare(N)) # this code is contributed by Surendra_Gangwar
C#
// C# implementation of above approach using System; class GFG { // Function to find the // next perfect square static int nextPerfectSquare(int N) { int nextN = (int)Math.Floor(Math.Sqrt(N)) + 1; return nextN * nextN; } // Driver Code public static void Main() { int n = 35; Console.WriteLine(nextPerfectSquare(n)); } } // This code is contributed // by Shashank
PHP
<?php // PHP implementation // of above approach // Function to find the // next perfect square function nextPerfectSquare($N) { $nextN = floor(sqrt($N)) + 1; return $nextN * $nextN; } // Driver Code $n = 35; echo nextPerfectSquare($n); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of above approach // Function to find the next perfect square function nextPerfectSquare(N) { let nextN = Math.floor(Math.sqrt(N)) + 1; return nextN * nextN; } // Driver Code let n = 35; document.write(nextPerfectSquare(n)); // This code is contributed by souravmahato348. </script>
Producción:
36
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ManasChhabra2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA