File tree 4 files changed +68
-61
lines changed
4 files changed +68
-61
lines changed Original file line number Diff line number Diff line change
1
+ package Maths ;
2
+
3
+ public class FactorialRecursion {
4
+
5
+ /* Driver Code */
6
+ public static void main (String [] args ) {
7
+ assert factorial (0 ) == 1 ;
8
+ assert factorial (1 ) == 1 ;
9
+ assert factorial (2 ) == 2 ;
10
+ assert factorial (3 ) == 6 ;
11
+ assert factorial (5 ) == 120 ;
12
+ }
13
+
14
+ /**
15
+ * Recursive FactorialRecursion Method
16
+ *
17
+ * @param n The number to factorial
18
+ * @return The factorial of the number
19
+ */
20
+ public static long factorial (int n ) {
21
+ if (n < 0 ) {
22
+ throw new IllegalArgumentException ("number is negative" );
23
+ }
24
+ return n == 0 || n == 1 ? 1 : n * factorial (n - 1 );
25
+ }
26
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import java .util .Scanner ;
4
4
5
-
6
- /**
7
- * @author blast314
8
- * <p>
9
- * Counts the number of characters in the text.
10
- */
11
-
12
5
public class CountChar {
13
6
14
7
public static void main (String [] args ) {
@@ -20,11 +13,12 @@ public static void main(String[] args) {
20
13
}
21
14
22
15
/**
23
- * @param str: String to count the characters
24
- * @return int: Number of characters in the passed string
16
+ * Count non space character in string
17
+ *
18
+ * @param str String to count the characters
19
+ * @return number of character in the specified string
25
20
*/
26
21
private static int CountCharacters (String str ) {
27
- str = str .replaceAll ("\\ s" ,"" );
28
- return str .length ();
22
+ return str .replaceAll ("\\ s" , "" ).length ();
29
23
}
30
24
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments