Created
March 17, 2020 09:26
-
-
Save manjotsidhu/461703e7c79c809709ed570141ba7ee7 to your computer and use it in GitHub Desktop.
TypeScript code to check if the given number is present in the Fibonacci series
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 isPerfectSquare(n: number) : boolean { | |
return n > 0 && Math.sqrt(n) % 1 === 0; | |
} | |
function isFibonacci(input: number) : boolean { | |
return isPerfectSquare(5*input*input + 4) || isPerfectSquare(5*input*input - 4) | |
} | |
console.log(isFibonacci(4)) | |
console.log(isFibonacci(8)) | |
console.log(isFibonacci(1)) | |
console.log(isFibonacci(41)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output :