Created
December 4, 2014 05:14
-
-
Save fanzhang312/e0b91caf60186d0058bb to your computer and use it in GitHub Desktop.
Check if a number is a Fibonacci number
This file contains hidden or 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
public static boolean isFibo(long n){ | |
return isPerfectSquare(5*n*n+4) || isPerfectSquare(5*n*n-4); | |
} | |
public static boolean isPerfectSquare(double a){ | |
int s = (int) Math.sqrt(a); | |
return s*s == a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment