FibonacciNumber · primary-code-test/Java@e141a33 · GitHub
Skip to content

Commit e141a33

Browse files
FibonacciNumber
1 parent cbc1899 commit e141a33

4 files changed

Lines changed: 68 additions & 61 deletions

File tree

Maths/FactorialRecursion.java

Lines changed: 26 additions & 0 deletions

Maths/FibonacciNumber.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Maths;
2+
3+
/**
4+
* Fibonacci: 0 1 1 2 3 5 8 13 21 ...
5+
*/
6+
public class FibonacciNumber {
7+
public static void main(String[] args) {
8+
assert isFibonacciNumber(1);
9+
assert isFibonacciNumber(2);
10+
assert isFibonacciNumber(21);
11+
assert !isFibonacciNumber(9);
12+
assert !isFibonacciNumber(10);
13+
}
14+
15+
/**
16+
* Check if a number is perfect square number
17+
*
18+
* @param number the number to be checked
19+
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
20+
*/
21+
public static boolean isPerfectSquare(int number) {
22+
int sqrt = (int) Math.sqrt(number);
23+
return sqrt * sqrt == number;
24+
}
25+
26+
/**
27+
* Check if a number is fibonacci number
28+
* This is true if and only if at least one of 5x^2+4 or 5x^2-4 is a perfect square
29+
*
30+
* @param number the number
31+
* @return <tt>true</tt> if {@code number} is fibonacci number, otherwise <tt>false</tt>
32+
* @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification
33+
*/
34+
public static boolean isFibonacciNumber(int number) {
35+
return isPerfectSquare(5 * number * number + 4) || isPerfectSquare(5 * number * number - 4);
36+
}
37+
}

Others/CountChar.java

Lines changed: 5 additions & 11 deletions

Others/Factorial.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)