Skip to content

Instantly share code, notes, and snippets.

@thenormalsquid
Last active December 10, 2018 21:26
Show Gist options
  • Save thenormalsquid/ae990a2e3bdb475723ba to your computer and use it in GitHub Desktop.
Save thenormalsquid/ae990a2e3bdb475723ba to your computer and use it in GitHub Desktop.
function isFib(val) {
var check1 = 5 * Math.pow(val, 2) + 4;
var check2 = 5 * Math.pow(val, 2) - 4;
function isPerfectSquare(num) {
return Math.sqrt(num) % 1 === 0;
}
//we see if the checks are perfect squares
var isPerfect1 = isPerfectSquare(check1);
var isPerfect2 = isPerfectSquare(check2);
if(isPerfect1 && isPerfect2) {
return true;
}
else if(isPerfect1 || isPerfect2) {
return true;
}
else {
return false;
}
};
//do some tests
//return true
console.log(isFib(5));
//return false
console.log(isFib(9));
//return true
console.log(isFib(317811));
//return true
console.log(isFib(0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment