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 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”.
Below is the implementation of the above approach:
# 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
Output:
Yes
Time Complexity: O(log10N)
Auxiliary Space: O(1)