What's the best way in JavaScript to test if a given parameter is a square number?

57.1k views Asked by At

I created a function that will test to see if a given parameter is a square number.

Read about square numbers here: https://en.wikipedia.org/?title=Square_number

If the number is a square number, it returns true and otherwise false. Negative numbers also return false.

Examples:

isSquare(-12) // => false
isSquare( 5) // => false
isSquare( 9) // => true
isSquare(25) // => true
isSquare(27) // => false

Right now, I am using this method: http://jsfiddle.net/marcusdei/ujtc82dq/5/

But, is there a shorter more cleaner way to get the job done?

7

There are 7 answers

3
Tushar On BEST ANSWER

Try this:

var isSquare = function (n) {
    return n > 0 && Math.sqrt(n) % 1 === 0;
};
  1. Check if number is positive
  2. Check if sqrt is complete number i.e. integer

Demo

0
Fred Cal On

I went that route:

var isSquare = (n) => n === 0 ? true : n > 0 && Math.sqrt(n) % 1 === 0;

console.log(isSquare(25));
console.log(isSquare(10));
console.log(isSquare(16));

0
A.McLoof On

I think that this one is a shorter and a cleaner option:

  var isSquare = function(n) {

  return Number.isInteger(Math.sqrt(n));
};

isSquare(25); //true

for even shorter and cleaner than that you could use:

var isSquare = n => Number.isInteger(Math.sqrt(n));

isSquare(25);//true
1
ASHISH R On

//1st 
var isPerfectSquare = function(num) {
   return Math.sqrt(num) % 1 === 0;
}

//2nd: loop through all the number from 1 to num 
var isPerfectSquare = function(num) {
    
    for(let i=1; i <= num ; i++){
        let d = i * i;
        if(d === num){
            return true
        }
    }
}

// Optimize solution: Binary Search 
var isPerfectSquare = function(num) {

    if(num ==1)return true
    let left = 2;
    let right = Math.floor(num/2);
    while(left <= right){
        let middle = Math.floor((left + right)/2)
        let sqr = middle * middle;
        if(sqr == num){
            return true
        }else{
            if(sqr > num){
              right = middle -1
            }else{
                left = middle + 1
            }
        }  
    }
    
    return false
};

0
Stephane Moreau On

I would definitely go for:

var isSquare = function (n) {
    return Math.sqrt(n) % 1 === 0;
};

PS: 0 is a square number for those who wonder

Demo

0
Jeff Lowery On

It's a bit trickier if you're using the new BigInt in JavaScript:

// integer square root function (stolen from the interwebs)
function sqrt(n) {
  let a = 1n;
  let b = (n >> 5n) + 8n;
  while (b >= a) {
    let mid = (a + b) >> 1n;
    if (mid * mid > n) {
      b = mid -= 1n;
    } else {
      a = mid += 1n;
    }
  }
  return a -= 1n;
}

sqrt(25n) === 5n
sqrt(26n) === 5n
...
sqrt(35n) === 5n

The best and fastest way I've found (so far) to determine if n is a square is:

function isSquare(n) {
   return n%sqrt(n) === 0n
}

But there's gotta be a faster way for BigInt operations.

1
AnonymousContribute On

Isn't this (Math.sqrt(number) % 1 === 0) basically enough? it just checks if the sqrt of the number is a whole number, if so, then it's a perfect square.

Obviously, depending on what you want to do with that information, it may require extra code.