Check if given number is perfect square
Given a number, check if it is a perfect square or not.
Examples :
Input : 2500 Output : Yes Explanation: 2500 is a perfect square. 50 * 50 = 2500 Input : 2555 Output : No
Approach:
- Take the floor()ed square root of the number.
- Multiply the square root twice.
- Use boolean equal operator to verify if the product of square root is equal to the number given.
C++
// CPP program to find if x is a // perfect square. #include <bits/stdc++.h> using namespace std; bool isPerfectSquare( long double x) { // Find floating point value of // square root of x. if (x >= 0) { long long sr = sqrt (x); // if product of square root //is equal, then // return T/F return (sr * sr == x); } // else return false if n<0 return false ; } int main() { long long x = 2502; if (isPerfectSquare(x)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java program to find if x is a // perfect square. class GFG { static boolean isPerfectSquare( int x) { if (x >= 0 ) { // Find floating point value of // square root of x. int sr = ( int )Math.sqrt(x); // if product of square root // is equal, then // return T/F return ((sr * sr) == x); } return false ; } // Driver code public static void main(String[] args) { int x = 2502 ; if (isPerfectSquare(x)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to find if x is a # perfect square. import math def isPerfectSquare(x): #if x >= 0, if (x > = 0 ): sr = int (math.sqrt(x)) # sqrt function returns floating value so we have to convert it into integer #return boolean T/F return ((sr * sr) = = x) return false # Driver code x = 2502 if (isPerfectSquare(x)): print ( "Yes" ) else : print ( "No" ) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find if x is a // perfect square. using System; class GFG { static bool isPerfectSquare( double x) { // Find floating point value of // square root of x. if (x >= 0) { double sr = Math.Sqrt(x); // if product of square root // is equal, then // return T/F return (sr * sr == x); } // else return false if n<0 return false ; } // Driver code public static void Main() { double x = 2502; if (isPerfectSquare(x)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find if x is // a perfect square. function isPerfectSquare( $x ) { // Find floating point value // of square root of x. $sr = sqrt( $x ); // If square root is an integer return (( $sr - floor ( $sr )) == 0); } // Driver code $x = 2502; if (isPerfectSquare( $x )) echo ( "Yes" ); else echo ( "No" ); // This code is contributed by Ajit. ?> |
Javascript
<script> // JavaScript program to find if x is a // perfect square. function isPerfectSquare(x) { if (x >= 0) { // Find floating point value of // square root of x. let sr = Math.sqrt(x); // if product of square root // is equal, then // return T/F return ((sr * sr) == x); } return false ; } // Driver code let x = 2500; if (isPerfectSquare(x)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by souravghosh0416. </script> |
No
Time Complexity: O(log(x))
Auxiliary Space: O(1)
To know more about the inbuilt sqrt function, refer this Stackoverflow and this StackExchange threads.
Another Approach :
- Use the floor and ceil function .
- If they are equal that implies the number is a perfect square.
C++
// C++ program for the above approach #include <iostream> #include <math.h> using namespace std; void checkperfectsquare( int n) { // If ceil and floor are equal // the number is a perfect // square if ( ceil (( double ) sqrt (n)) == floor (( double ) sqrt (n))) { cout << "perfect square" ; } else { cout << "not a perfect square" ; } } // Driver Code int main() { int n = 49; checkperfectsquare(n); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG{ static void checkperfectsquare( int n) { // If ceil and floor are equal // the number is a perfect // square if (Math.ceil(( double )Math.sqrt(n)) == Math.floor(( double )Math.sqrt(n))) { System.out.print( "perfect square" ); } else { System.out.print( "not a perfect square" ); } } // Driver Code public static void main(String[] args) { int n = 49 ; checkperfectsquare(n); } } // This code is contributed by subhammahato348 |
Python3
# Python3 program for the above approach import math def checkperfectsquare(x): # If ceil and floor are equal # the number is a perfect # square if (math.ceil(math.sqrt(n)) = = math.floor(math.sqrt(n))): print ( "perfect square" ) else : print ( "not a perfect square" ) # Driver code n = 49 checkperfectsquare(n) # This code is contributed by jana_sayantan |
C#
// C# program for the above approach using System; class GFG{ static void checkperfectsquare( int n) { // If ceil and floor are equal // the number is a perfect // square if (Math.Ceiling(( double )Math.Sqrt(n)) == Math.Floor(( double )Math.Sqrt(n))) { Console.Write( "perfect square" ); } else { Console.Write( "not a perfect square" ); } } // Driver Code public static void Main() { int n = 49; checkperfectsquare(n); } } // This code is contributed by subhammahato348 |
Javascript
<script> // Javascript program for the above approach function checkperfectsquare(n) { // If ceil and floor are equal // the number is a perfect // square if (Math.ceil(Math.sqrt(n)) == Math.floor(Math.sqrt(n))) { document.write( "perfect square" ); } else { document.write( "not a perfect square" ); } } // Driver Code let n = 49; checkperfectsquare(n); // This code is contributed by rishavmahato348 </script> |
perfect square
Time Complexity : O(sqrt(n))
Auxiliary space: O(1)
Another Approach : without using sqrtx() function
we will use binary search to do this
Below is the implementation of the above approach:
C++
#include <iostream> using namespace std; bool isPerfectSquare( int num) { int64_t n = num; int64_t lo = 1; int64_t hi = num; while (hi - lo > 1) { int64_t mid = (hi + lo) / 2; if (num == 1) { return true ; } else if (mid * mid == num) { return true ; } else if (mid * mid > num) { hi = mid - 1; } else if (mid * mid < num) { lo = mid + 1; } } if (lo * lo == num || hi * hi == num) { return true ; } return false ; } int main() { int n = 676; cout << isPerfectSquare(n); return 0; } |
Java
public class PerfectSquare { static boolean isPerfectSquare( int num) { long n = num; long lo = 1 ; long hi = num; while (hi - lo > 1 ) { long mid = (hi + lo) / 2 ; if (num == 1 ) { return true ; } else if (mid * mid == num) { return true ; } else if (mid * mid > num) { hi = mid - 1 ; } else if (mid * mid < num) { lo = mid + 1 ; } } if (lo * lo == num || hi * hi == num) { return true ; } return false ; } public static void main(String[] args) { int n = 676 ; System.out.println(isPerfectSquare(n)); } } // This code is contributed by factworx412 |
Python3
def isPerfectSquare( num): n = num; lo = 1 ; hi = num; while (hi - lo > 1 ) : mid = ( int )(hi + lo) / 2 ; if (num = = 1 ) : return 1 ; elif (mid * mid = = num) : return 1 ; elif (mid * mid > num) : hi = mid - 1 ; elif (mid * mid < num) : lo = mid + 1 ; if (lo * lo = = num or hi * hi = = num) : return 1 ; return 0 ; n = 676 ; print (isPerfectSquare(n)); # This code is contributed by ratiagrawal. |
C#
using System; class Program { static bool IsPerfectSquare( int num) { long n = num; long lo = 1; long hi = num; while (hi - lo > 1) { long mid = (hi + lo) / 2; if (num == 1) { return true ; } else if (mid * mid == num) { return true ; } else if (mid * mid > num) { hi = mid - 1; } else if (mid * mid < num) { lo = mid + 1; } } if (lo * lo == num || hi * hi == num) { return true ; } return false ; } static void Main( string [] args) { int n = 676; Console.WriteLine(IsPerfectSquare(n)); } } // This code is contributed by lokeshpotta20. |
Javascript
function isPerfectSquare(num) { let n = num; let lo = 1; let hi = num; while (hi - lo > 1) { let mid = Math.floor(hi + lo) / 2; if (num == 1) { return 1; } else if (mid * mid == num) { return 1; } else if (mid * mid > num) { hi = mid - 1; } else if (mid * mid < num) { lo = mid + 1; } } if (lo * lo == num || hi * hi == num) { return 1; } return 0; } let n = 676; document.write(isPerfectSquare(n)); |
1
Time Complexity: O(log n)
The time complexity of this algorithm is O(log n) since the algorithm will run for log n iterations at most.
Space Complexity: O(1)
The algorithm uses constant space, as it only stores a few variables which don’t increase with the size of the input.
METHOD 4: Using ** operator
APPROACH:
This program checks whether a given number is a perfect square or not using a brute-force approach.
ALGORITHM:
1.Take the input number, n.
2.Loop through all numbers from 0 to n.
3.Check whether the square of the current number is equal to n.
4.If it is, print “Yes” and break out of the loop.
5.If the loop completes without finding a perfect square, print “No”.
Python3
n = 2500 for i in range (n + 1 ): if i * * 2 = = n: print ( "Yes" ) break else : print ( "No" ) |
Yes
Time complexity: O(n)
Space complexity: O(1)
Please Login to comment...