Question: To go beyond the primitive types, we need to use a new class. Fortunately, the BigInteger class is one of the standard Java classes, so

To go beyond the primitive types, we need to use a new class. Fortunately, the BigInteger
class is one of the standard Java classes, so to get extremely large integers, we can use this
class. Copy the program factorial.java to bifactorial.java using the cp
command. Now, go in and change the class name to bifactorial. Think about and modify
all of the places where you will need to change int to BigInteger (result variable
declaration at the end of main, method return type, method local variable).
2. Changing to a non-primitive value takes a bit of doing. First of all, you need to include the
library at the top of the program with
import java.math.BigInteger;
3. Second, in the fact class method, there is no direct conversion between integers and
BigIntegers, so you'll need to replace product =1; with
BigInteger product = BigInteger.ONE;
BigInteger.ONE is a public class variable in the BigInteger class. Your parameter will
still be a regular, simple int and your for loop index will be an int too. If you are using an
integer value other than 0 or 1, you can create a new BigInteger with its value using
BigInteger.valueOf (the integer). So, you could have initialized the product
using
product = BigInteger.valueOf(1);
4. Finally, you can not use the standard mathematical symbols +,-,*, etc on new types. (Some
programming languages do allow you to redefine these, but Java is not one of those languages.)
So, it should look like this:
private static BigInteger fact (int n)
{
BigInteger product = BigInteger.ONE;
for (int i =2; i <= n; i++)
{
product = product.multiply (BigInteger.valueOf(i));
}
return product;
}
Get your program to compile and try a run that computes the factorial of a large number
(somewhere between 200 and 1000). Notice that we are returning a BigInteger value
now, not a simple integer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!