10.9 The BigInteger and BigDecimal Classes The BigInteger and BigDecimal classes
ID: 3815716 • Letter: 1
Question
10.9 The BigInteger and BigDecimal Classes The BigInteger and BigDecimal classes can be used to represent integers or at decimal numbers of any size and precision. If you need to compute with very large integers or high-precision floating-point val- ues, you can use the BigInteger and BigDecimal classes in the java.math pack age. Both are immutable. The largest integer of the long type is Long .MAX VALUE (i.e., 9223372036854775807). An instance of BigInteger can represent an integer of any size. You can use new BigInteger String and new BigDecimal (String) to create an instance of BigInteger and BigDecimal, use the add, subtract, multiply, divide, and remainder methods to perform arithmetic operations, and use the compareTo method to compare two big numbers. For example, the following code creates two BigInteger objects and multiplies them BigInteger a new BigInteger( 9223372036854775807 BigInteger b new BigInteger( C"2'); BigInteger c a multiply (b) 9223372036854775807 2 System. out. printlnCC)Explanation / Answer
HI, Please find my recursive implementation of factorial
Please let me know in case of any issue.
import java.math.BigInteger;
public class LargeFactorial {
public static BigInteger factorialRec(long n){
// Base case
if(n <= 1)
return BigInteger.ONE;
// recursive
return new BigInteger(n+"").multiply(factorialRec(n-1));
}
public static void main(String[] args) {
System.out.println("6! is: "+factorialRec(6));
}
}
/*
Sample run:
50! is: 30414093201713378043612608166064768844377641568960512000000000000
6! is: 720
*/
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.