diff --git a/Maths/FactorialRecursion.java b/Maths/FactorialRecursion.java new file mode 100644 index 00000000..6e12d0ba --- /dev/null +++ b/Maths/FactorialRecursion.java @@ -0,0 +1,26 @@ +package Maths; + +public class FactorialRecursion { + + /* Driver Code */ + public static void main(String[] args) { + assert factorial(0) == 1; + assert factorial(1) == 1; + assert factorial(2) == 2; + assert factorial(3) == 6; + assert factorial(5) == 120; + } + + /** + * Recursive FactorialRecursion Method + * + * @param n The number to factorial + * @return The factorial of the number + */ + public static long factorial(int n) { + if (n < 0) { + throw new IllegalArgumentException("number is negative"); + } + return n == 0 || n == 1 ? 1 : n * factorial(n - 1); + } +} diff --git a/Maths/FibonacciNumber.java b/Maths/FibonacciNumber.java new file mode 100644 index 00000000..89796b33 --- /dev/null +++ b/Maths/FibonacciNumber.java @@ -0,0 +1,37 @@ +package Maths; + +/** + * Fibonacci: 0 1 1 2 3 5 8 13 21 ... + */ +public class FibonacciNumber { + public static void main(String[] args) { + assert isFibonacciNumber(1); + assert isFibonacciNumber(2); + assert isFibonacciNumber(21); + assert !isFibonacciNumber(9); + assert !isFibonacciNumber(10); + } + + /** + * Check if a number is perfect square number + * + * @param number the number to be checked + * @return true if {@code number} is perfect square, otherwise false + */ + public static boolean isPerfectSquare(int number) { + int sqrt = (int) Math.sqrt(number); + return sqrt * sqrt == number; + } + + /** + * Check if a number is 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 true if {@code number} is fibonacci number, otherwise false + * @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); + } +} diff --git a/Others/CountChar.java b/Others/CountChar.java index ee83bce0..8f37217e 100644 --- a/Others/CountChar.java +++ b/Others/CountChar.java @@ -2,13 +2,6 @@ package Others; import java.util.Scanner; - -/** - * @author blast314 - *

- * Counts the number of characters in the text. - */ - public class CountChar { public static void main(String[] args) { @@ -20,11 +13,12 @@ public class CountChar { } /** - * @param str: String to count the characters - * @return int: Number of characters in the passed string + * Count non space character in string + * + * @param str String to count the characters + * @return number of character in the specified string */ private static int CountCharacters(String str) { - str = str.replaceAll("\\s",""); - return str.length(); + return str.replaceAll("\\s", "").length(); } } diff --git a/Others/Factorial.java b/Others/Factorial.java deleted file mode 100644 index 652607e5..00000000 --- a/Others/Factorial.java +++ /dev/null @@ -1,50 +0,0 @@ -package Others; - -import java.util.Scanner; - -/** - * This program will print out the factorial of any non-negative - * number that you input into it. - * - * @author Marcus - */ -public class Factorial { - - /** - * The main method - * - * @param args Command line arguments - */ - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - System.out.print("Enter a non-negative integer: "); - - //If user does not enter an Integer, we want program to fail gracefully, letting the user know why it terminated - try { - int number = input.nextInt(); - - //We keep prompting the user until they enter a positive number - while (number < 0) { - System.out.println("Your input must be non-negative. Please enter a positive number: "); - number = input.nextInt(); - } - //Display the result - System.out.println("The factorial of " + number + " will yield: " + factorial(number)); - - } catch (Exception e) { - System.out.println("Error: You did not enter an integer. Program has terminated."); - } - input.close(); - } - - /** - * Recursive Factorial Method - * - * @param n The number to factorial - * @return The factorial of the number - */ - public static long factorial(int n) { - if (n == 0 || n == 1) return 1; - return n * factorial(n - 1); - } -}