Given a positive integer num, write a function that returns True if num is a perfect square and False otherwise.

  • Note: Do not use any built-in library functions, such as SQRT.
The sample1Input:16Output: True,Copy the code
The sample2Input:14Output: FalseCopy the code

The problem solving code

/** * @param {number} num * @return {boolean} */
var isPerfectSquare = function(num) {
 let i = 0;
 let result = 0;
 while (result < num) {
  result = i * i;
  i++;
 }
 return result == num;
};
Copy the code
The execution time

This article is formatted using MDNICE