Given a number n, check if it is a perfect square or not.
Examples :
Input : n = 2500
Output : Yes
Explanation: 2500 is a perfect square of 50
Input : n = 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++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( long double x)
{
if (x >= 0) {
long long sr = sqrt (x);
return (sr * sr == x);
}
return false ;
}
int main()
{
long long x = 2502;
if (isPerfectSquare(x))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean isPerfectSquare( int x)
{
if (x >= 0 ) {
int sr = ( int )Math.sqrt(x);
return ((sr * sr) == x);
}
return false ;
}
public static void main(String[] args)
{
int x = 2502 ;
if (isPerfectSquare(x))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def isPerfectSquare(x):
if (x > = 0 ):
sr = int (math.sqrt(x))
return ((sr * sr) = = x)
return false
x = 2502
if (isPerfectSquare(x)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isPerfectSquare( double x)
{
if (x >= 0) {
double sr = Math.Sqrt(x);
return (sr * sr == x);
}
return false ;
}
public static void Main()
{
double x = 2502;
if (isPerfectSquare(x))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
if (x >= 0) {
let sr = Math.sqrt(x);
return ((sr * sr) == x);
}
return false ;
}
let x = 2500;
if (isPerfectSquare(x))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function isPerfectSquare( $x )
{
$sr = sqrt( $x );
return (( $sr - floor ( $sr )) == 0);
}
$x = 2502;
if (isPerfectSquare( $x ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Time Complexity: O(log(x))
Auxiliary Space: O(1)
Check if given number is perfect square using ceil, floor and sqrt() function.
- Use the floor and ceil and sqrt() function.
- If they are equal that implies the number is a perfect square.
C++
#include <iostream>
#include <math.h>
using namespace std;
void checkperfectsquare( int n)
{
if ( ceil (( double ) sqrt (n)) == floor (( double ) sqrt (n))) {
cout << "perfect square" ;
}
else {
cout << "not a perfect square" ;
}
}
int main()
{
int n = 49;
checkperfectsquare(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void checkperfectsquare( int n)
{
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" );
}
}
public static void main(String[] args)
{
int n = 49 ;
checkperfectsquare(n);
}
}
|
Python3
import math
def checkperfectsquare(x):
if (math.ceil(math.sqrt(n)) = =
math.floor(math.sqrt(n))):
print ( "perfect square" )
else :
print ( "not a perfect square" )
n = 49
checkperfectsquare(n)
|
C#
using System;
class GFG{
static void checkperfectsquare( int n)
{
if (Math.Ceiling(( double )Math.Sqrt(n)) ==
Math.Floor(( double )Math.Sqrt(n)))
{
Console.Write( "perfect square" );
}
else
{
Console.Write( "not a perfect square" );
}
}
public static void Main()
{
int n = 49;
checkperfectsquare(n);
}
}
|
Javascript
<script>
function checkperfectsquare(n)
{
if (Math.ceil(Math.sqrt(n)) ==
Math.floor(Math.sqrt(n)))
{
document.write( "perfect square" );
}
else
{
document.write( "not a perfect square" );
}
}
let n = 49;
checkperfectsquare(n);
</script>
|
Time Complexity : O(sqrt(n))
Auxiliary space: O(1)
Check if given number is perfect square using Binary search:
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int n)
{
if (n <= 1) {
return true ;
}
long long left = 1, right = n;
while (left <= right) {
long long mid = left + (right - left) / 2;
long long square = mid * mid;
if (square == n) {
return true ;
}
else if (square < n) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return false ;
}
int main()
{
int n = 2500;
if (isPerfectSquare(n)) {
cout << n << " is a perfect square." << endl;
}
else {
cout << n << " is not a perfect square."
<< std::endl;
}
return 0;
}
|
Java
public class PerfectSquareCheck {
static boolean isPerfectSquare( int n)
{
if (n <= 1 ) {
return true ;
}
long left = 1 , right = n;
while (left <= right) {
long mid = left + (right - left) / 2 ;
long square = mid * mid;
if (square == n) {
return true ;
}
else if (square < n) {
left = mid + 1 ;
}
else {
right = mid - 1 ;
}
}
return false ;
}
public static void main(String[] args)
{
int n = 2500 ;
if (isPerfectSquare(n)) {
System.out.println(n + " is a perfect square." );
}
else {
System.out.println(
n + " is not a perfect square." );
}
}
}
|
Python
def isPerfectSquare(n):
if n < = 1 :
return True
left, right = 1 , n
while left < = right:
mid = left + (right - left) / / 2
square = mid * mid
if square = = n:
return True
elif square < n:
left = mid + 1
else :
right = mid - 1
return False
n = 2500
if isPerfectSquare(n):
print (n, "is a perfect square." )
else :
print (n, "is not a perfect square." )
|
C#
using System;
class Program
{
static bool IsPerfectSquare( int n)
{
if (n <= 1)
{
return true ;
}
long left = 1, right = n;
while (left <= right)
{
long mid = left + (right - left) / 2;
long square = mid * mid;
if (square == n)
{
return true ;
}
else if (square < n)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return false ;
}
static void Main( string [] args)
{
int n = 2500;
if (IsPerfectSquare(n))
{
Console.WriteLine(n + " is a perfect square." );
}
else
{
Console.WriteLine(n + " is not a perfect square." );
}
}
}
|
Javascript
function isPerfectSquare(n) {
if (n <= 1) {
return true ;
}
let left = 1, right = n;
while (left <= right) {
let mid = Math.floor(left + (right - left) / 2);
let square = mid * mid;
if (square === n) {
return true ;
}
else if (square < n) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return false ;
}
const n = 2500;
if (isPerfectSquare(n)) {
console.log(n + " is a perfect square." );
} else {
console.log(n + " is not a perfect square." );
}
|
Output
2500 is a perfect square.
Time Complexity: O(log n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!