Created
April 19, 2020 12:14
-
-
Save chunrapeepat/4c124a910dc0d7710d787a636df59c17 to your computer and use it in GitHub Desktop.
DAY5 - Binary Search - has square root
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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