Skip to content

Instantly share code, notes, and snippets.

@chunrapeepat
Created April 19, 2020 12:14
Show Gist options
  • Save chunrapeepat/4c124a910dc0d7710d787a636df59c17 to your computer and use it in GitHub Desktop.
Save chunrapeepat/4c124a910dc0d7710d787a636df59c17 to your computer and use it in GitHub Desktop.
DAY5 - Binary Search - has square root
function hasSqrt(N) {
let L = 0;
let R = N;
while (L <= R) {
let mid = Math.floor(L + (R - L) / 2);
let sqare = Math.pow(mid, 2);
if (sqare === N) {
return true;
} else if (sqare < N) {
L = mid + 1;
} else {
R = mid - 1;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment