Skip to content

Instantly share code, notes, and snippets.

@jamesxv7
Created December 6, 2016 14:57
Show Gist options
  • Save jamesxv7/9e8d36eddc3ae4d0ea9f2d62ed46089a to your computer and use it in GitHub Desktop.
Save jamesxv7/9e8d36eddc3ae4d0ea9f2d62ed46089a to your computer and use it in GitHub Desktop.
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