Check if a number is perfect square without finding square root
Check whether a number is a perfect square or not without finding its square root.
Examples:
Input: n = 36
Output: Yes
Input: n = 12
Output: No
Method 1:
The idea is to run a loop from i = 1 to floor(sqrt(n)) and then check if squaring it makes n.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int n)
{
for ( int i = 1; i * i <= n; i++) {
if ((n % i == 0) && (n / i == i)) {
return true ;
}
}
return false ;
}
int main()
{
long long int n = 36;
if (n == 0 || isPerfectSquare(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class GfG {
static boolean isPerfectSquare( int n)
{
for ( int i = 1 ; i * i <= n; i++) {
if ((n % i == 0 ) && (n / i == i)) {
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int n = 36 ;
if (n == 0 || isPerfectSquare(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPerfectSquare(n) :
i = 1
while (i * i< = n):
if ((n % i = = 0 ) and (n / i = = i)):
return True
i = i + 1
return False
if __name__ = = "__main__" :
n = 36
if (isPerfectSquare(n)):
print ( "Yes, it is a perfect square." )
elif n = = 0 :
print ( "Yes, it is a perfect square." )
else :
print ( "No, it is not a perfect square." )
|
C#
using System;
public class GfG {
static bool isPerfectSquare( int n)
{
for ( int i = 1; i * i <= n; i++) {
if ((n % i == 0) && (n / i == i)) {
return true ;
}
}
return false ;
}
public static void Main()
{
int n = 36;
if (n == 0 || isPerfectSquare(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isPerfectSquare( $n )
{
for ( $i = 1; $i * $i <= $n ; $i ++) {
if (( $n % $i == 0) && ( $n / $i == $i )) {
return true;
}
}
return false;
}
$n = 36;
if ( $n == 0 || isPerfectSquare( $n ))
echo "Yes" ;
else
echo "No" ;
|
Javascript
<script>
function isPerfectSquare(n)
{
for (let i = 1; i * i <= n; i++) {
if ((n % i == 0) && (Math.floor(n / i) == i)) {
return true ;
}
}
return false ;
}
let n = 36;
if (n == 0 || isPerfectSquare(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity : O(sqrt(N))
Auxiliary Space: O(1)
Method 2:
The idea is to use binary search to find a number in the range 1 to n whose square is equal to n, such that at each iteration the problem statement reduces to half [1 to n/2-1 OR n/2 to n].
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int x)
{
long long left = 1, right = x;
while (left <= right) {
long long mid = (left + right) / 2;
if (mid * mid == x) {
return true ;
}
if (mid * mid < x) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return false ;
}
int main()
{
int n = 2500;
if (n == 0 || isPerfectSquare(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isPerfectSquare( int num)
{
long left = 1 , right = num;
while (left <= right)
{
long mid = (left + right) / 2 ;
if (mid * mid == num)
{
return true ;
}
if (mid * mid < num)
{
left = mid + 1 ;
}
else
{
right = mid - 1 ;
}
}
return false ;
}
public static void main(String[] args)
{
int x = 2500 ;
if (x == 0 || isPerfectSquare(x))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isPerfectSquare(x):
left = 1
right = x
while (left < = right):
mid = (left + right) >> 1
if ((mid * mid) = = x):
return True
if (mid * mid < x):
left = mid + 1
else :
right = mid - 1
return False
if __name__ = = "__main__" :
x = 2500
if x = = 0 :
print ( "Yes, it is a perfect square." )
elif (isPerfectSquare(x)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPerfectSquare( int x)
{
long left = 1, right = x;
while (left <= right)
{
long mid = (left + right) / 2;
if (mid * mid == x)
{
return true ;
}
if (mid * mid < x)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return false ;
}
public static void Main( string [] args)
{
int n = 2500;
if (n == 0 || isPerfectSquare(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
let left = 1, right = x;
while (left <= right)
{
let mid = parseInt((left + right) / 2);
if (mid * mid == x)
{
return true ;
}
if (mid * mid < x)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return false ;
}
let x = 2500;
if (x == 0 || isPerfectSquare(x))
document.write( "Yes" );
else
document.write( "Yes" );
</script>
|
Time Complexity : O(log(N))
Auxiliary Space: O(1)
Method 3: Using the property that the sum of odd numbers is a perfect square
The given program checks if a number is a perfect square without finding the square root. It does this by iterating over the odd numbers, starting from 1 and subtracting them from the given number n. If n becomes zero, it means that n is a perfect square.
C++
#include <iostream>
using namespace std;
bool is_perfect_square( int n) {
int i = 1;
while (n > 0) {
n -= i;
i += 2;
}
return n == 0;
}
int main() {
int n = 36;
if (is_perfect_square(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
n = 12;
if (is_perfect_square(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
public class GFG {
public static boolean isPerfectSquare( int n) {
int i = 1 ;
while (n > 0 ) {
n -= i;
i += 2 ;
}
return n == 0 ;
}
public static void main(String[] args) {
int n = 36 ;
if (isPerfectSquare(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
n = 12 ;
if (isPerfectSquare(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def is_perfect_square(n):
i = 1
while n > 0 :
n - = i
i + = 2
return n = = 0
n = 36
if is_perfect_square(n):
print ( "Yes" )
else :
print ( "No" )
n = 12
if is_perfect_square(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class Program {
static bool IsPerfectSquare( int n)
{
int i = 1;
while (n > 0) {
n -= i;
i += 2;
}
return n == 0;
}
static void Main( string [] args)
{
int n = 36;
if (IsPerfectSquare(n)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
n = 12;
if (IsPerfectSquare(n)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function is_perfect_square(n) {
let i = 1;
while (n > 0) {
n -= i;
i += 2;
}
return n === 0;
}
let n = 36;
if (is_perfect_square(n)) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
n = 12;
if (is_perfect_square(n)) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time complexity: O(sqrt(n)) since it takes at most sqrt(n) iterations to check if a number is a perfect square.
Auxiliary space: O(1).
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...