Check if a number is a perfect square having all its digits as a perfect square
Last Updated :
06 Apr, 2023
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)
{
if (!isPerfectSquare(N))
{
cout << "No" ;
return false ;
}
string str = to_string(N);
for ( char c : str)
{
int digit = c - '0' ;
if (!isPerfectSquare(digit))
{
cout << "No" ;
return false ;
}
}
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)
{
if (!isPerfectSquare(( int )N)) {
System.out.println( "No" );
return false ;
}
String str = Long.toString(N);
for ( char c : str.toCharArray()) {
int digit = c - '0' ;
if (!isPerfectSquare(digit)) {
System.out.println( "No" );
return false ;
}
}
System.out.println( "Yes" );
return true ;
}
public static void main(String[] args)
throws java.lang.Exception
{
long N = 144 ;
isFullSquare(N);
}
}
|
Python3
import math
def isPerfectSquare(n):
root = math.sqrt(n)
return root * root = = n
def isFullSquare(N):
if not isPerfectSquare(N):
print ( "No" )
return False
strN = str (N)
for c in strN:
digit = int (c)
if not isPerfectSquare(digit):
print ( "No" )
return False
print ( "Yes" )
return True
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)
{
if (!IsPerfectSquare(( int )N)) {
Console.WriteLine( "No" );
return false ;
}
string str = N.ToString();
foreach ( char c in str)
{
int digit = c - '0' ;
if (!IsPerfectSquare(digit)) {
Console.WriteLine( "No" );
return false ;
}
}
Console.WriteLine( "Yes" );
return true ;
}
static void Main( string [] args)
{
long N = 144;
IsFullSquare(N);
}
}
|
Javascript
function isPerfectSquare(n) {
let root = Math.sqrt(n);
return root * root == n;
}
function isFullSquare(N) {
if (!isPerfectSquare(N)) {
console.log( "No" );
return false ;
}
let str = N.toString();
for (let c of str) {
let digit = c - '0' ;
if (!isPerfectSquare(digit)) {
console.log( "No" );
return false ;
}
}
console.log( "Yes" );
return true ;
}
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++
#include <bits/stdc++.h>
using namespace std;
bool check_digits( long N)
{
while (N > 0) {
int n = N % 10;
if ((n != 0) && (n != 1)
&& (n != 4) && (n != 9)) {
return 0;
}
N = N / 10;
}
return 1;
}
bool is_perfect( long N)
{
long double n = sqrt (N);
if ( floor (n) != ceil (n)) {
return 0;
}
return 1;
}
void isFullSquare( long N)
{
if (is_perfect(N)
&& check_digits(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
long N = 144;
isFullSquare(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean check_digits( long N)
{
while (N > 0 )
{
int n = ( int ) (N % 10 );
if ((n != 0 ) && (n != 1 ) &&
(n != 4 ) && (n != 9 ))
{
return false ;
}
N = N / 10 ;
}
return true ;
}
static boolean is_perfect( long N)
{
double n = Math.sqrt(N);
if (Math.floor(n) != Math.ceil(n))
{
return false ;
}
return true ;
}
static void isFullSquare( long N)
{
if (is_perfect(N) &&
check_digits(N))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
public static void main(String[] args)
{
long N = 144 ;
isFullSquare(N);
}
}
|
Python3
import math
def check_digits(N):
while (N > 0 ):
n = N % 10
if ((n ! = 0 ) and (n ! = 1 ) and
(n ! = 4 ) and (n ! = 9 )):
return 0
N = N / / 10
return 1
def is_perfect(N):
n = math.sqrt(N)
if (math.floor(n) ! = math.ceil(n)):
return 0
return 1
def isFullSquare(N):
if (is_perfect(N) and
check_digits(N)):
print ( "Yes" )
else :
print ( "No" )
N = 144
isFullSquare(N)
|
C#
using System;
class GFG{
static bool check_digits( long N)
{
while (N > 0)
{
int n = ( int ) (N % 10);
if ((n != 0) && (n != 1) &&
(n != 4) && (n != 9))
{
return false ;
}
N = N / 10;
}
return true ;
}
static bool is_perfect( long N)
{
double n = Math.Sqrt(N);
if (Math.Floor(n) != Math.Ceiling(n))
{
return false ;
}
return true ;
}
static void isFullSquare( long N)
{
if (is_perfect(N) &&
check_digits(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
public static void Main()
{
long N = 144;
isFullSquare(N);
}
}
|
Javascript
<script>
function check_digits(N)
{
while (N > 0) {
let n = N % 10;
if ((n != 0) && (n != 1)
&& (n != 4) && (n != 9)) {
return 0;
}
N = Math.floor(N / 10);
}
return 1;
}
function is_perfect(N)
{
let n = Math.sqrt(N);
if (Math.floor(n) != Math.ceil(n)) {
return 0;
}
return 1;
}
function isFullSquare(N)
{
if (is_perfect(N)
&& check_digits(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
let N = 144;
isFullSquare(N);
</script>
|
Time Complexity: O(log10N), where N is the input variable
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...