Open In App

JavaScript Program to Check Whether a Number is Perfect Square

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The number that results from squaring another integer is called a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. There are several ways available in JavaScript to check whether a number is a perfect square or not which are as follows:

Using the Math.sqrt() Function

The simplest approach to checking whether a number is a perfect square is to use the Math.sqrt() function. If the square root of a number results in an integer, then the number is a perfect square.

Example: It returns true if the input number is a perfect square (i.e., an integer that is the square of an integer), otherwise false. It also includes examples of usage demonstrating its functionality.

Javascript




function isPerfectSquare(num) {
    if (num <= 0 || typeof num !== "number") {
        return false;
    }
 
    const sqrt = Math.sqrt(num);
    return Number.isInteger(sqrt);
}
 
const number1 = 16;
const number2 = 9;
const number3 = 15;
 
console.log(`${number1} is perfect square:
 ${isPerfectSquare(number1)}`);
console.log(`${number2} is perfect square:
 ${isPerfectSquare(number2)}`);
console.log(`${number3} is perfect square:
 ${isPerfectSquare(number3)}`);


Output

16 is perfect square: true
9 is perfect square: true
15 is perfect square: false

Using for loop

One can iterate from 1 to the square root of the number using for loop and check if any integer square results in the given number.

Example: It starts at 1 and iterates through possible square roots until the iterator’s square reaches the specified number. It returns true if the square of any iterator is equal to the given integer, displaying that the number is a perfect square; if not, it returns false.

Javascript




function isPerfectSquare(num) {
    // Check for negative and non-numeric input
    if (num <= 0 || typeof num !== "number") {
        return false;
    }
 
    // Loop through potential square roots
    for (let i = 1; i * i <= num; i++) {
        if (i * i === num) {
            return true;
        }
    }
 
    return false;
}
 
// Example usage
const number1 = 16;
const number2 = 9;
const number3 = 15;
 
console.log(`${number1} is perfect square:
   ${isPerfectSquare(number1)}`);
console.log(`${number2} is perfect square:
   ${isPerfectSquare(number2)}`);
console.log(`${number3} is perfect square:
   ${isPerfectSquare(number3)}`);


Output

16 is perfect square: true
9 is perfect square: true
15 is perfect square: false

A more optimized approach is to use binary search to find the square root of the number. We can narrow down the search range by halving it in each iteration.

Example: It utilizes a binary search approach to find if the square of any number between 1 and num is equal to the given number. If found, it returns true, indicating that the number is a perfect square; otherwise, it returns false

Javascript




function isPerfectSquare3(num) {
    let left = 1;
    let right = num;
 
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        let square = mid * mid;
 
        if (square === num) {
            return true;
        } else if (square < num) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
 
    return false;
}
 
const number1 = 16;
const number2 = 9;
const number3 = 15;
 
console.log(`${number1} is perfect square:
 ${isPerfectSquare3(number1)}`);
console.log(`${number2} is perfect square:
 ${isPerfectSquare3(number2)}`);
console.log(`${number3} is perfect square:
 ${isPerfectSquare3(number3)}`);


Output

16 is perfect square: true
9 is perfect square: true
15 is perfect square: false



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads