Created
December 6, 2016 14:57
-
-
Save jamesxv7/9e8d36eddc3ae4d0ea9f2d62ed46089a 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 findNextSquare(sq) { | |
// Optimal solution | |
// return Math.sqrt(sq)%1? -1 : Math.pow(Math.sqrt(sq)+1,2); | |
// Return the next square if sq if a perfect square, -1 otherwise | |
var curVal = Math.sqrt(sq) + 1 | |
var nextSq = Math.sqrt(sq) + 1 | |
nextSq *= nextSq | |
//console.log(sq) | |
//console.log(currentNumber) | |
//console.log(nextSq * nextSq) | |
if (nextSq == Math.floor(curVal*curVal)) | |
//console.log(nextSq * nextSq) | |
return nextSq | |
else | |
return -1; | |
//console.log(-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment