Check whether a number can be represented by the product of two squares
Last Updated :
05 Nov, 2021
Given an integer n, our task is to check whether number n can be represented by the product of two squares. If it is possible then print “yes” otherwise print “no”.
Examples :
Input: n = 144
Output: Yes
Explanation:
The given number 144 can be represented as 22 * 62 = 144.
Input: n = 25
Output: No
Explanation:
The given number 25 cannot be represented as product of two square numbers.
Naive Approach:
To solve the problem mentioned above the naive method is to use the Brute Force method. Use two for loop iterating till n and each time we will check whether the product of the square of both numbers is equal to N. If we find such a combination then we will print Yes otherwise No.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prodSquare( int n)
{
for ( long i = 2; i * i <= n; i++)
for ( long j = 2; j <= n; j++)
if (i * i * j * j == n)
return true ;
return false ;
}
int main()
{
int n = 25;
if (prodSquare(n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class GFG{
static boolean prodSquare( int n)
{
for ( long i = 2 ; i * i <= n; i++)
for ( long j = 2 ; j <= n; j++)
if (i * i * j * j == n)
return true ;
return false ;
}
public static void main(String[] args)
{
int n = 25 ;
if (prodSquare(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def prodSquare(n):
for i in range ( 2 , (n) + 1 ):
if (i * i < (n + 1 )):
for j in range ( 2 , n + 1 ):
if ((i * i * j * j) = = n):
return True ;
return False ;
if __name__ = = '__main__' :
n = 25 ;
if (prodSquare(n)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool prodSquare( int n)
{
for ( long i = 2; i * i <= n; i++)
for ( long j = 2; j <= n; j++)
if (i * i * j * j == n)
return true ;
return false ;
}
public static void Main(String[] args)
{
int n = 25;
if (prodSquare(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function prodSquare(n) {
for (i = 2; i * i <= n; i++)
for (j = 2; j <= n; j++)
if (i * i * j * j == n)
return true ;
return false ;
}
var n = 25;
if (prodSquare(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above method, use a hashmap where we will store the squares of number till sqrt(n) and each time we will search for (n / sqrt(i)) in the hashmap if it exists then return Yes else return No.
C++
#include <bits/stdc++.h>
using namespace std;
bool prodSquare( int n)
{
unordered_map< float , float > s;
for ( int i = 2; i * i <= n; ++i) {
s[i * i] = 1;
if (s.find(n / (i * i)) != s.end())
return true ;
}
return false ;
}
int main()
{
int n = 25;
if (prodSquare(n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
static boolean prodSquare( int n)
{
HashMap<Float, Float> s = new HashMap<Float, Float>();
for ( int i = 2 ; i * i <= n; ++i)
{
s.put(( float )(i * i), ( float ) 1 );
if (s.containsKey(( float ) n / (i * i)))
return true ;
}
return false ;
}
public static void main(String[] args)
{
int n = 25 ;
if (prodSquare(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def prodSquare(n):
s = dict ()
i = 2
while (i * i < = n):
s[i * i] = 1
if ((n / / (i * i)) in s):
return True
i + = 1
return False
if __name__ = = '__main__' :
n = 25
if (prodSquare(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool prodSquare( int n)
{
Dictionary< float ,
float > s = new Dictionary< float ,
float >();
for ( int i = 2; i * i <= n; ++i)
{
s.Add(( float )(i * i), ( float ) 1);
if (s.ContainsKey(( float ) n / (i * i)))
return true ;
}
return false ;
}
public static void Main(String[] args)
{
int n = 25;
if (prodSquare(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function prodSquare(n)
{
var s = new Map();
for ( var i = 2; i * i <= n; ++i) {
s.set(i * i,1);
if (s.has(n / (i * i)))
return true ;
}
return false ;
}
var n = 25;
if (prodSquare(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(sqrt n)
Auxiliary Space: O(sqrt n)
Share your thoughts in the comments
Please Login to comment...