[front end algorithm] effective complete square number, traversal once

Time:2021-6-5

Given a positive integer num, write a function that returns true if num is a complete square, otherwise returns false.

  • Note: do not use any built-in library functions, such as sqrt.
Example 1:
Input: 16
Output: true
Example 2:
Input: 14
Output: false

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;
};

[front end algorithm] effective complete square number, traversal once