Check if a number is a perfect square having all its digits as a perfect square
Given an integer N, the task is to check if the given number is a perfect square having all its digits as a perfect square or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 144
Output: Yes
Explanation: The number 144 is a perfect square and also the digits of the number {1(= 12, 4(= 22} is also a perfect squares.Input: N = 81
Output: No
Approach:
The first step in the approach is to check if N is a perfect square. We can use the built-in sqrt() function to compute the square root of N, and then check if the square of this value is equal to N. If N is not a perfect square, we can immediately return “No” and exit the function.
If N is a perfect square, we can proceed to the next step, which is to check if each digit of N is a perfect square. To do this, we can convert N to a string using the to_string() function, which returns a string representation of N. We can then iterate over each character of the string using a for loop, and convert each character to an integer using the expression c – ‘0’. This expression subtracts the ASCII value of the character ‘0’ from the ASCII value of the current character c, resulting in an integer value corresponding to the current digit.
For each digit, we can use the isPerfectSquare() function to check if it is a perfect square. If the digit is not a perfect square, we can immediately return “No” and exit the function. If all digits are perfect squares, we can return “Yes” and exit the function.
C++
#include <bits/stdc++.h> using namespace std; bool isPerfectSquare( int n) { int root = sqrt (n); return root * root == n; } bool isFullSquare( long N) { // Check if N is a perfect square if (!isPerfectSquare(N)) { cout << "No" ; return false ; } // Convert N to a string and check if each character is a perfect square string str = to_string(N); for ( char c : str) { int digit = c - '0' ; if (!isPerfectSquare(digit)) { cout << "No" ; return false ; } } // All digits are perfect squares, so N is a full square cout << "Yes" ; return true ; } int main() { long N = 144; isFullSquare(N); return 0; } |
Java
import java.io.*; import java.lang.*; import java.util.*; class Main { public static boolean isPerfectSquare( int n) { int root = ( int )Math.sqrt(n); return root * root == n; } public static boolean isFullSquare( long N) { // Check if N is a perfect square if (!isPerfectSquare(( int )N)) { System.out.println( "No" ); return false ; } // Convert N to a string and check if each character // is a perfect square String str = Long.toString(N); for ( char c : str.toCharArray()) { int digit = c - '0' ; if (!isPerfectSquare(digit)) { System.out.println( "No" ); return false ; } } // All digits are perfect squares, so N is a full // square System.out.println( "Yes" ); return true ; } public static void main(String[] args) throws java.lang.Exception { long N = 144 ; isFullSquare(N); } } |
Python3
import math # Function to check if a number is a perfect square def isPerfectSquare(n): root = math.sqrt(n) return root * root = = n # Function to check if a number is a full square def isFullSquare(N): # Check if N is a perfect square if not isPerfectSquare(N): print ( "No" ) return False # Convert N to a string and check if each character is a perfect square strN = str (N) for c in strN: digit = int (c) if not isPerfectSquare(digit): print ( "No" ) return False # All digits are perfect squares, so N is a full square print ( "Yes" ) return True # Test the code with an example value of N N = 144 isFullSquare(N) |
C#
using System; class Program { static bool IsPerfectSquare( int n) { int root = ( int )Math.Sqrt(n); return root * root == n; } static bool IsFullSquare( long N) { // Check if N is a perfect square if (!IsPerfectSquare(( int )N)) { Console.WriteLine( "No" ); return false ; } // Convert N to a string and check if each character // is a perfect square string str = N.ToString(); foreach ( char c in str) { int digit = c - '0' ; if (!IsPerfectSquare(digit)) { Console.WriteLine( "No" ); return false ; } } // All digits are perfect squares, so N is a full // square Console.WriteLine( "Yes" ); return true ; } static void Main( string [] args) { long N = 144; IsFullSquare(N); } } |
Javascript
// Function to check if a number is a perfect square function isPerfectSquare(n) { let root = Math.sqrt(n); return root * root == n; } // Function to check if a number is a full square function isFullSquare(N) { // Check if N is a perfect square if (!isPerfectSquare(N)) { console.log( "No" ); return false ; } // Convert N to a string and check if each character is a perfect square let str = N.toString(); for (let c of str) { let digit = c - '0' ; if (!isPerfectSquare(digit)) { console.log( "No" ); return false ; } } // All digits are perfect squares, so N is a full square console.log( "Yes" ); return true ; } // Test the code with an example value of N let N = 144; isFullSquare(N); |
Output: Yes
Time Complexity: O(d), where d is the number of digits in the input number N.
Space Complexity: O(1)
Approach: The idea is to check if the given number N is a perfect square or not. If found to be true, check if all its digits are either 0, 1, 4, or 9. If found to be true, print “Yes”. Otherwise, print “No”.
Algorithm:
- Create a static method “check_digits” with a boolean return type that takes the long value “N” as the input parameter.
- Start a while loop with the condition “N” greater than 0.
- Extract the last digit of “N” and store it in “n”.
- If “n” is not equal to 0, 1, 4, or 9, return false.
- Divide “N” by 10 and store the value in “N”.
- If came out from the while loop then return true.
- Create a static method “is_perfect” with a boolean return type that takes the long value “N” as input.
- Now store the square root of “N” in variable “n” of double type.
- If the floor and ceiling of “n” are not equal, return false.
- Otherwise, return true.
- Create a static method named “isFullSquare” with void return type which takes long as an input variable named as “N”.
- If both “is_perfect” and “check_digits” methods return true for “N”, print “Yes”.
- Otherwise, print “No”.
Below is the implementation of the above approach:
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
Time Complexity: O(log10N), where N is the input variable
Auxiliary Space: O(1)