Dado un número entero N , la tarea es verificar si el número dado es un cuadrado perfecto con todos sus dígitos como un cuadrado perfecto o no. Si se encuentra que es cierto, escriba “ Sí” . De lo contrario, escriba “ No” .
Ejemplos:
Entrada: N = 144
Salida: Sí
Explicación:
El número 144 es un cuadrado perfecto y también los dígitos del número {1(= 1 2 , 4(= 2 2 } también son cuadrados perfectos.
Entrada: N = 81
Salida: No
Enfoque: La idea es verificar si el número N dado es un cuadrado perfecto o no. Si es cierto, verifica si todos sus dígitos son 0 , 1 , 4 o 9 . Si es cierto, escriba » Sí» . De lo contrario, escriba “ No” .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if digits of // N is a perfect square or not bool check_digits(long N) { // Iterate over the digits while (N > 0) { // Extract the digit int n = N % 10; // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return 0; } // Divide N by 10 N = N / 10; } // Return true return 1; } // Function to check if N is // a perfect square or not bool is_perfect(long N) { long double n = sqrt(N); // If floor and ceil of n // is not same if (floor(n) != ceil(n)) { return 0; } return 1; } // Function to check if N satisfies // the required conditions or not void isFullSquare(long N) { // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { cout << "Yes"; } else { cout << "No"; } } // Driver Code int main() { long N = 144; // Function Call isFullSquare(N); return 0; }
Java
// Java program for // the above approach import java.util.*; class GFG{ // Function to check if digits of // N is a perfect square or not static boolean check_digits(long N) { // Iterate over the digits while (N > 0) { // Extract the digit int n = (int) (N % 10); // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return false; } // Divide N by 10 N = N / 10; } // Return true return true; } // Function to check if N is // a perfect square or not static boolean is_perfect(long N) { double n = Math.sqrt(N); // If floor and ceil of n // is not same if (Math.floor(n) != Math.ceil(n)) { return false; } return true; } // Function to check if N satisfies // the required conditions or not static void isFullSquare(long N) { // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { System.out.print("Yes"); } else { System.out.print("No"); } } // Driver Code public static void main(String[] args) { long N = 144; // Function Call isFullSquare(N); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach import math # Function to check if digits of # N is a perfect square or not def check_digits(N): # Iterate over the digits while (N > 0): # Extract the digit n = N % 10 # Check if digit is a # perfect square or not if ((n != 0) and (n != 1) and (n != 4) and (n != 9)): return 0 # Divide N by 10 N = N // 10 # Return true return 1 # Function to check if N is # a perfect square or not def is_perfect(N): n = math.sqrt(N) # If floor and ceil of n # is not same if (math.floor(n) != math.ceil(n)): return 0 return 1 # Function to check if N satisfies # the required conditions or not def isFullSquare(N): # If both the conditions # are satisfied if (is_perfect(N) and check_digits(N)): print("Yes") else: print("No") # Driver Code N = 144 # Function call isFullSquare(N) # This code is contributed by sanjoy_62
C#
// C# program for // the above approach using System; class GFG{ // Function to check if digits of // N is a perfect square or not static bool check_digits(long N) { // Iterate over the digits while (N > 0) { // Extract the digit int n = (int) (N % 10); // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return false; } // Divide N by 10 N = N / 10; } // Return true return true; } // Function to check if N is // a perfect square or not static bool is_perfect(long N) { double n = Math.Sqrt(N); // If floor and ceil of n // is not same if (Math.Floor(n) != Math.Ceiling(n)) { return false; } return true; } // Function to check if N satisfies // the required conditions or not static void isFullSquare(long N) { // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { Console.Write("Yes"); } else { Console.Write("No"); } } // Driver Code public static void Main() { long N = 144; // Function Call isFullSquare(N); } } // This code is contributed by Chitranayal
Javascript
<script> // JavaScript program for the above approach // Function to check if digits of // N is a perfect square or not function check_digits(N) { // Iterate over the digits while (N > 0) { // Extract the digit let n = N % 10; // Check if digit is a // perfect square or not if ((n != 0) && (n != 1) && (n != 4) && (n != 9)) { return 0; } // Divide N by 10 N = Math.floor(N / 10); } // Return true return 1; } // Function to check if N is // a perfect square or not function is_perfect(N) { let n = Math.sqrt(N); // If floor and ceil of n // is not same if (Math.floor(n) != Math.ceil(n)) { return 0; } return 1; } // Function to check if N satisfies // the required conditions or not function isFullSquare(N) { // If both the conditions // are satisfied if (is_perfect(N) && check_digits(N)) { document.write("Yes"); } else { document.write("No"); } } // Driver Code let N = 144; // Function Call isFullSquare(N); // This code is contributed by Surbhi Tyagi. </script>
Yes
Complejidad de tiempo: O(log 10 N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA