Created
April 21, 2016 15:30
-
-
Save skylerto/c1f137bf88079e7d84b99f803176bd2c to your computer and use it in GitHub Desktop.
This file contains 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 solution(A, B) { | |
if (A == B){ | |
// Same values | |
return 0; | |
} else if(A > 0 && B > 0){ | |
// Positive values | |
var left = Math.sqrt(A); | |
var right = Math.sqrt(B); | |
return (Math.floor(right) - Math.floor(left)) + 1; | |
} else if(A < 0) { | |
// A is less than 0. | |
var left = Math.sqrt(-1 * A); | |
var right = Math.sqrt(B); | |
return (Math.floor(right) + Math.floor(left)) + 1; | |
} else { | |
// Both are less than 0 (A<=B). | |
var left = Math.sqrt(-1 * A); | |
var right = Math.sqrt(-1 * B); | |
return (Math.floor(right) - Math.floor(left)) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment