Given a number n, determine whether it is a perfect square. Return true if it is a perfect square; otherwise, return false.
Examples :
Input : n = 36
Output : true
Explanation: Since 6 × 6 = 36, therefore 36 is a perfect square.Input: n = 2500
Output: true
Explanation: Since 50 × 50 = 2500, therefore 2500 is a perfect square.Input: n = 8
Output: false
Explanation: No integer multiplied by itself equals 8, so 8 is not a perfect square.
Table of Content
Using inbuilt sqrt() function - O(log n) Time and O(1) Space
Since, a number is a perfect square if its square root is an integer therefore, the idea is to compute the integer square root of the given number and square it again; if the result equals the original number, it is a perfect square, otherwise it is not.
- If the given number is negative, return false since negative numbers cannot be perfect squares.
- Compute the integer square root of the given number.
- Square the computed root.
- If the squared value equals the original number, return true.
- Otherwise, return false.
#include <cmath>
#include <iostream>
using namespace std;
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// Compute the square root of the number.
int root = sqrt(n);
// If squaring the integer square root gives the original number,
// then it is a perfect square.
return (root * root == n);
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
cout << "true";
else
cout << "false";
return 0;
}
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// Compute the square root of the number.
int root = (int)sqrt(n);
// If squaring the integer square root gives the original number,
// then it is a perfect square.
return (root * root == n);
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
printf("true");
else
printf("false");
return 0;
}
class GFG {
static boolean isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// Compute the square root of the number.
int root = (int)Math.sqrt(n);
// If squaring the integer square root gives the
// original number, then it is a perfect square.
return (root * root == n);
}
public static void main(String[] args)
{
int n = 36;
if (isPerfectSquare(n))
System.out.print("true");
else
System.out.print("false");
}
}
import math
def isPerfectSquare(n):
# Negative numbers cannot be perfect squares.
if n < 0:
return False
# Compute the square root of the number.
root = int(math.sqrt(n))
# If squaring the integer square root gives the original number,
# then it is a perfect square.
return root * root == n
# Driver Code
if __name__ == "__main__":
n = 36
if isPerfectSquare(n):
print("true")
else:
print("false")
using System;
class GFG {
static bool isPerfectSquare(int n) {
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// Compute the square root of the number.
int root = (int)Math.Sqrt(n);
// If squaring the integer square root gives the original number,
// then it is a perfect square.
return (root * root == n);
}
static void Main()
{
int n = 36;
if (isPerfectSquare(n))
Console.Write("true");
else
Console.Write("false");
}
}
function isPerfectSquare(n) {
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// Compute the square root of the number.
let root = Math.floor(Math.sqrt(n));
// If squaring the integer square root gives the original number,
// then it is a perfect square.
return root * root === n;
}
// Driver Code
let n = 36;
if (isPerfectSquare(n))
console.log("true");
else
console.log("false");
Output
true
Using Binary search - O(log n) Time and O(1) Space
Since the square of a number increases as the number increases, we can apply binary search on the range [1, n]. At each step, compare mid × mid with n and discard the half that cannot contain the answer.
- If the given number is negative, return false since negative numbers cannot be perfect squares.
- Base Case: if the number is 0 or 1, return true.
- Apply binary search on the range [1, n] and compute the square of the middle element.
- If the square equals the given number, return true; otherwise, search the left or right half based on the comparison.
- If no such integer is found after the search completes, return false.
#include <iostream>
using namespace std;
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 and 1 are perfect squares.
if (n <= 1)
return true;
int left = 1, right = n;
while (left <= right)
{
// Find the middle element.
int mid = left + (right - left) / 2;
// Compute the square of the middle element.
long long square = 1LL * mid * mid;
// If the square equals the given number,
// then it is a perfect square.
if (square == n)
return true;
// If the square is smaller than the given number,
// search in the right half.
if (square < n)
left = mid + 1;
// Otherwise, search in the left half.
else
right = mid - 1;
}
// No integer whose square equals n was found.
return false;
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
cout << "true";
else
cout << "false";
return 0;
}
#include <stdbool.h>
#include <stdio.h>
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 and 1 are perfect squares.
if (n <= 1)
return true;
int left = 1, right = n;
while (left <= right)
{
// Find the middle element.
int mid = left + (right - left) / 2;
// Compute the square of the middle element.
long long square = 1LL * mid * mid;
// If the square equals the given number,
// then it is a perfect square.
if (square == n)
return true;
// If the square is smaller than the given number,
// search in the right half.
if (square < n)
left = mid + 1;
// Otherwise, search in the left half.
else
right = mid - 1;
}
// No integer whose square equals n was found.
return false;
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
printf("true");
else
printf("false");
return 0;
}
class GFG {
static boolean isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 and 1 are perfect squares.
if (n <= 1)
return true;
int left = 1, right = n;
while (left <= right) {
// Find the middle element.
int mid = left + (right - left) / 2;
// Compute the square of the middle element.
long square = 1L * mid * mid;
// If the square equals the given number,
// then it is a perfect square.
if (square == n)
return true;
// If the square is smaller than the given
// number, search in the right half.
if (square < n)
left = mid + 1;
// Otherwise, search in the left half.
else
right = mid - 1;
}
// No integer whose square equals n was found.
return false;
}
public static void main(String[] args)
{
int n = 36;
if (isPerfectSquare(n))
System.out.print("true");
else
System.out.print("false");
}
}
def isPerfectSquare(n):
# Negative numbers cannot be perfect squares.
if n < 0:
return False
# 0 and 1 are perfect squares.
if n <= 1:
return True
left, right = 1, n
while left <= right:
# Find the middle element.
mid = left + (right - left) // 2
# Compute the square of the middle element.
square = mid * mid
# If the square equals the given number,
# then it is a perfect square.
if square == n:
return True
# If the square is smaller than the given number,
# search in the right half.
if square < n:
left = mid + 1
# Otherwise, search in the left half.
else:
right = mid - 1
# No integer whose square equals n was found.
return False
# Driver Code
if __name__ == "__main__":
n = 36
if isPerfectSquare(n):
print("true")
else:
print("false")
using System;
class GFG {
static bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 and 1 are perfect squares.
if (n <= 1)
return true;
int left = 1, right = n;
while (left <= right) {
// Find the middle element.
int mid = left + (right - left) / 2;
// Compute the square of the middle element.
long square = 1L * mid * mid;
// If the square equals the given number,
// then it is a perfect square.
if (square == n)
return true;
// If the square is smaller than the given
// number, search in the right half.
if (square < n)
left = mid + 1;
// Otherwise, search in the left half.
else
right = mid - 1;
}
// No integer whose square equals n was found.
return false;
}
static void Main()
{
int n = 36;
if (isPerfectSquare(n))
Console.Write("true");
else
Console.Write("false");
}
}
function isPerfectSquare(n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 and 1 are perfect squares.
if (n <= 1)
return true;
let left = 1, right = n;
while (left <= right) {
// Find the middle element.
let mid = Math.floor(left + (right - left) / 2);
// Compute the square of the middle element.
let square = mid * mid;
// If the square equals the given number,
// then it is a perfect square.
if (square === n)
return true;
// If the square is smaller than the given number,
// search in the right half.
if (square < n)
left = mid + 1;
// Otherwise, search in the left half.
else
right = mid - 1;
}
// No integer whose square equals n was found.
return false;
}
// Driver Code
let n = 36;
if (isPerfectSquare(n))
console.log("true");
else
console.log("false");
Output
true
Using Mathematical Properties - O(sqrt(n)) Time and O(1) Space
The idea is based on the fact that every perfect square is equal to the sum of the first few consecutive odd numbers. Therefore, we repeatedly subtract consecutive odd numbers (1, 3, 5...) from the given number. If it becomes exactly 0, the number is a perfect square; otherwise, it is not.
1 + 3 = 4
1 + 3 + 5 = 9
1 + 3 + 5 + 7 = 16
1 + 3 + 5 + 7 + 9 = 25
1 + 3 + 5 + 7 + 9 + 11 = 36 and so on.
- If the given number is negative, return false since negative numbers cannot be perfect squares.
- Initialize the first odd number as 1.
- Repeatedly subtract the current odd number from the given number and increment the odd number by 2.
- Continue this process until the given number becomes 0 or negative.
- If the remaining value is 0, return true; otherwise, return false.
#include <iostream>
using namespace std;
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 is a perfect square.
if (n == 0)
return true;
// Start with the first odd number.
int odd = 1;
// Keep subtracting consecutive odd numbers
// until the number becomes zero or negative.
while (n > 0)
{
n -= odd;
odd += 2;
}
// If the remaining value is zero,
// the original number is a perfect square.
return (n == 0);
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
cout << "true";
else
cout << "false";
return 0;
}
#include <stdbool.h>
#include <stdio.h>
bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 is a perfect square.
if (n == 0)
return true;
// Start with the first odd number.
int odd = 1;
// Keep subtracting consecutive odd numbers
// until the number becomes zero or negative.
while (n > 0)
{
n -= odd;
odd += 2;
}
// If the remaining value is zero,
// the original number is a perfect square.
return (n == 0);
}
int main()
{
int n = 36;
if (isPerfectSquare(n))
printf("true");
else
printf("false");
return 0;
}
class GFG {
static boolean isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 is a perfect square.
if (n == 0)
return true;
// Start with the first odd number.
int odd = 1;
// Keep subtracting consecutive odd numbers
// until the number becomes zero or negative.
while (n > 0) {
n -= odd;
odd += 2;
}
// If the remaining value is zero,
// the original number is a perfect square.
return (n == 0);
}
public static void main(String[] args)
{
int n = 36;
if (isPerfectSquare(n))
System.out.print("true");
else
System.out.print("false");
}
}
def isPerfectSquare(n):
# Negative numbers cannot be perfect squares.
if n < 0:
return False
# 0 is a perfect square.
if n == 0:
return True
# Start with the first odd number.
odd = 1
# Keep subtracting consecutive odd numbers
# until the number becomes zero or negative.
while n > 0:
n -= odd
odd += 2
# If the remaining value is zero,
# the original number is a perfect square.
return n == 0
# Driver Code
if __name__ == "__main__":
n = 36
if isPerfectSquare(n):
print("true")
else:
print("false")
using System;
class GFG {
static bool isPerfectSquare(int n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 is a perfect square.
if (n == 0)
return true;
// Start with the first odd number.
int odd = 1;
// Keep subtracting consecutive odd numbers
// until the number becomes zero or negative.
while (n > 0) {
n -= odd;
odd += 2;
}
// If the remaining value is zero,
// the original number is a perfect square.
return (n == 0);
}
static void Main()
{
int n = 36;
if (isPerfectSquare(n))
Console.Write("true");
else
Console.Write("false");
}
}
function isPerfectSquare(n)
{
// Negative numbers cannot be perfect squares.
if (n < 0)
return false;
// 0 is a perfect square.
if (n === 0)
return true;
// Start with the first odd number.
let odd = 1;
// Keep subtracting consecutive odd numbers
// until the number becomes zero or negative.
while (n > 0) {
n -= odd;
odd += 2;
}
// If the remaining value is zero,
// the original number is a perfect square.
return n === 0;
}
// Driver Code
let n = 36;
if (isPerfectSquare(n))
console.log("true");
else
console.log("false");
Output
true