So I was given a problem telling me to make a table of factorials of integers up to number 30. The book specifically tells me to use the object BigInteger. (using BigInteger big= BigInteger.valueOf(x)) However doing so is pretty tricky and gives me a bunch of errors that I have no idea how to fix.
for example
public static BigInteger factorial(int a){
if(a == 0){
return BigInteger.valueOf(1);
}
else{
return BigInteger.valueOf(a*BigInteger.valueOf(factorial(a-1)));// this will keep giving me an error message that I need to change the value type to "long" and back and forth to BIgInteger. I've tried many different variations including where I use BigInteger.valueOf(..) to every values.
}
}
Do you know a correct way to use the BigInteger object?
When would you ever use BigInteger instead of double?
import java.math.BigInteger;
public class BigInt {
public static double factorial(int a){
if(a == 0){
return 1;
}
else{
return a* factorial(a-1);
}
}
public static void table(int a){
for(int i =0; i<=a; i++){
System.out.println(i + ", " + factorial(i) );
}
}
public static void main(String[] args) {
table(30);
}
}