Added a function to get the factorial of a number

This commit is contained in:
2020-01-31 19:32:30 -05:00
parent 93bad2b80d
commit 0bbb3ba406
2 changed files with 135 additions and 0 deletions

View File

@@ -625,6 +625,38 @@ public class Algorithms{
fibNums.remove(fibNums.size() - 1);
return fibNums;
}
//This function returns the factorial of the number passed to it
public static Integer factorial(Integer num){
Integer fact = 1; //The value of the factorial
//TODO: Throw an exception for values < 0
//Loop through every number up to and including num and add the product to the factorial
for(Integer cnt = 2;cnt.compareTo(num) <= 0;++cnt){
fact *= cnt;
}
return fact;
}
public static Long factorial(Long num){
Long fact = 1L; //The value of the factorial
//Loop through every number up to and including num and add the product to the factorial
for(Long cnt = 2L;cnt.compareTo(num) <= 0;++cnt){
fact *= cnt;
}
return fact;
}
public static BigInteger factorial(BigInteger num){
BigInteger fact = BigInteger.valueOf(1L);
//Loop through every number up to and including num and add the product to the factorial
for(BigInteger cnt = BigInteger.TWO;cnt.compareTo(num) <= 0;cnt = cnt.add(BigInteger.ONE)){
fact = fact.multiply(cnt);
}
return fact;
}
//This function returns the sum of all elements in the list
public static Integer getSum(ArrayList<Integer> nums){
//If a blank list was passed to the function return 0 as the sum

View File

@@ -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