mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added a function to get the factorial of a number
This commit is contained in:
@@ -70,6 +70,12 @@ public class testAlgorithms{
|
||||
timer.stop();
|
||||
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
||||
|
||||
//Test factorial
|
||||
timer.start();
|
||||
testFactorial();
|
||||
timer.stop();
|
||||
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
||||
|
||||
//Tets getSum
|
||||
timer.start();
|
||||
testGetSum();
|
||||
@@ -360,6 +366,100 @@ public class testAlgorithms{
|
||||
System.out.println("getAllFib passed all tests");
|
||||
}
|
||||
}
|
||||
//This function tests the factorial function
|
||||
private static void testFactorial(){
|
||||
Boolean failed = false; //Holds whether a test was failed
|
||||
|
||||
//Test 1
|
||||
Integer correctAnswer = 720;
|
||||
Integer number = 6;
|
||||
Integer answer = Algorithms.factorial(number);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswer.equals(answer)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 2
|
||||
correctAnswer = 479001600;
|
||||
number = 12;
|
||||
answer = Algorithms.factorial(number);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswer.equals(answer)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
Long correctAnswerLong = 720L;
|
||||
Long numberLong = 6L;
|
||||
Long answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 4
|
||||
correctAnswerLong = 479001600L;
|
||||
numberLong = 12L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 5
|
||||
correctAnswerLong = 2432902008176640000L;
|
||||
numberLong = 20L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 6
|
||||
BigInteger correctAnswerBig = BigInteger.valueOf(720L);
|
||||
BigInteger numberBig = BigInteger.valueOf(6);
|
||||
BigInteger answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 7
|
||||
correctAnswerBig = BigInteger.valueOf(479001600L);
|
||||
numberBig = BigInteger.valueOf(12);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 8
|
||||
correctAnswerBig = BigInteger.valueOf(2432902008176640000L);
|
||||
numberBig = BigInteger.valueOf(20L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 9
|
||||
correctAnswerBig = new BigInteger("265252859812191058636308480000000");
|
||||
numberBig = BigInteger.valueOf(30L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Print a message if all of the tests passed
|
||||
if(!failed){
|
||||
System.out.println("factorial passed all tests");
|
||||
}
|
||||
}
|
||||
//This function tests the getSum function
|
||||
private static void testGetSum(){
|
||||
Boolean failed = false; //Holds whether a test was failed
|
||||
@@ -615,6 +715,9 @@ It took 6.107 milliseconds to run this test
|
||||
getAllFib passed all tests
|
||||
It took 708.100 microseconds to run this test
|
||||
|
||||
factorial passed all tests
|
||||
It took 522.200 microseconds to run this test
|
||||
|
||||
getSum passed all tests
|
||||
It took 1.736 milliseconds to run this test
|
||||
|
||||
|
||||
Reference in New Issue
Block a user