Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Changing to a nonprimitive 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;
Second, in the fact class method, there is no direct conversion between integers and
BigIntegers, so you'll need to replace product ; 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 or 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;
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 ; i n; i
product product.multiply BigIntegervalueOfi;
return product;
Get your program to compile and try a run that computes the factorial of a large number
somewhere between and Notice that we are returning a BigInteger value
now, not a simple integer.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started