Answered step by step
Verified Expert Solution
Link Copied!

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).
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 15th International Conference Dexa 2004 Zaragoza Spain August 30 September 3 2004 Proceedings Lncs 3180

Authors: Fernando Galindo ,Makoto Takizawa ,Roland Traunmuller

2004th Edition

3540229361, 978-3540229360

More Books

Students also viewed these Databases questions

Question

How did you feel about taking piano lessons as a child? (general)

Answered: 1 week ago