Update FibonacciNumber.java (#4195)
This commit is contained in:
parent
deef2ae445
commit
9ce275c16d
@ -17,8 +17,8 @@ public class FibonacciNumber {
|
||||
* Check if a number is perfect square number
|
||||
*
|
||||
* @param number the number to be checked
|
||||
* @return <tt>true</tt> if {@code number} is perfect square, otherwise
|
||||
* <tt>false</tt>
|
||||
* @return <tt>true</tt> if {@code number} is a perfect square, otherwise
|
||||
* <tt>false</tt>
|
||||
*/
|
||||
public static boolean isPerfectSquare(int number) {
|
||||
int sqrt = (int) Math.sqrt(number);
|
||||
@ -26,18 +26,17 @@ public class FibonacciNumber {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a number is fibonacci number This is true if and only if at
|
||||
* Check if a number is a Fibonacci number. This is true if and only if at
|
||||
* least one of 5x^2+4 or 5x^2-4 is a perfect square
|
||||
*
|
||||
* @param number the number
|
||||
* @return <tt>true</tt> if {@code number} is fibonacci number, otherwise
|
||||
* <tt>false</tt>
|
||||
* @return <tt>true</tt> if {@code number} is a Fibonacci number, otherwise
|
||||
* <tt>false</tt>
|
||||
* @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification
|
||||
*/
|
||||
public static boolean isFibonacciNumber(int number) {
|
||||
return (
|
||||
isPerfectSquare(5 * number * number + 4) ||
|
||||
isPerfectSquare(5 * number * number - 4)
|
||||
);
|
||||
int value1 = 5 * number * number + 4;
|
||||
int value2 = 5 * number * number - 4;
|
||||
return isPerfectSquare(value1) || isPerfectSquare(value2);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user