Last active
December 10, 2018 21:26
-
-
Save thenormalsquid/ae990a2e3bdb475723ba 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 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