
Problem Statement
Given a positive integer num, the task is to determine whether or not this number is a perfect square. A number is considered a perfect square if there exists some integer n such that n * n = num. This problem requires a solution without using advanced mathematical functions like square root (sqrt), provided by built-in libraries. The challenge is to check for the perfect square nature using possibly simpler algorithms or iterative approaches.
Examples
Example 1
Input:
num = 16
Output:
true
Explanation:
We return true because 4 * 4 = 16 and 4 is an integer.
Example 2
Input:
num = 14
Output:
false
Explanation:
We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
Constraints
1 <= num <= 231 - 1
Approach and Intuition
To solve the problem of determining if a number is a perfect square, without using built-in functions like sqrt, a basic method is required that checks each number sequentially or uses a more efficient algorithm like binary search. Below is an intuitive breakdown and approach using a simple algorithm:
- Initialize a variable
istarting from 1 and increment it in each step. - In each iteration, calculate
i * i.- If
i * iequalsnum, thennumis a perfect square, and returntrue. - If
i * ibecomes greater thannum, then no integerncan satisfyn * n = num(since squares grow quickly), and returnfalse. - Continue the iteration until
i * iexceedsnum.
- If
- This approach works efficiently for smaller numbers, but for very large numbers close to 2^31 - 1, this method can be slow.
- An optimization can be achieved using a binary search approach on
ifrom 1 tonum, reducing the number of comparisons significantly.- Set
lowto 1 andhightonum. - While
lowis less than or equal tohigh, compute the middle pointmid. - If
mid * midequalsnum, returntrue. - If
mid * midis less thannum, setlowtomid + 1. - Otherwise, set
hightomid - 1.
- Set
These steps ensure that the number is checked against all possible square roots in an optimal manner, provided the constraints.
Solutions
- Java
class Solution {
public boolean checkPerfectSquare(int number) {
if (number < 2) return true;
long mid = number / 2;
while (mid * mid > number) {
mid = (mid + number / mid) / 2;
}
return (mid * mid == number);
}
}
The provided Java class named Solution includes a method checkPerfectSquare that determines if a given integer is a perfect square.
The method employs an efficient approach - not brute-force, but rather a form of Newton's method to approximate the square root:
- If the number is less than 2, it immediately returns true, since both 0 and 1 are perfect squares.
- It initializes
midto half of the number to start with a reasonable estimate of the square root. - The loop continues adjusting
midusing the formula(mid + number / mid) / 2, which is characteristic of Newton's method for approximating roots, untilmid * midis no longer greater than the number. - Finally, the method returns true if
mid * midequals the number, confirming it's a perfect square, otherwise false.
This technique ensures a quick and precise check even for large numbers, avoiding the inefficiency of testing every integer up to the number. The solution is well-optimized for performance, leveraging mathematical foundations for root approximation.