Skip to content

Instantly share code, notes, and snippets.

@manjotsidhu
Created March 17, 2020 09:26
Show Gist options
  • Save manjotsidhu/461703e7c79c809709ed570141ba7ee7 to your computer and use it in GitHub Desktop.
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
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))
@manjotsidhu
Copy link
Author

Output :

false
true
true
false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment