SOURCE

// 求任意整数的平方根

const getSquareRoot = function (int, precise) {
    let low = 0, high = int, mid = (low + high) / 2
    while ((high - low) >= precise){
        const mid = (low + high) / 2
        const temp = mid ** 2
        if (temp - int > precise) {
            high = mid
        } else if (int - temp > precise) {
            low = mid
        } else {
            return Math.ceil(mid) ** 2 === int ? Math.ceil(mid) : mid 
        }
    }
    return Math.ceil(low) ** 2 === int ? Math.ceil(low) : low
}

console.log(getSquareRoot(1, 0.000001))
console 命令行工具 X clear

                    
>
console