FibonacciNumber
This commit is contained in:
parent
cbc1899b38
commit
e141a33fa7
26
Maths/FactorialRecursion.java
Normal file
26
Maths/FactorialRecursion.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
37
Maths/FibonacciNumber.java
Normal file
37
Maths/FibonacciNumber.java
Normal file
@ -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 <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
|
||||
*/
|
||||
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 <tt>true</tt> if {@code number} is 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);
|
||||
}
|
||||
}
|
@ -2,13 +2,6 @@ package Others;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
/**
|
||||
* @author blast314
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user