Java Reference
InDepth Information
7.257415615307994E306
7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000
1.5511210043330986E25
15511210043330985984000000
Thefirstthreevaluesrepresentthehighestfactorialsthatcanbereturnedbythe
int

based,
long
based, and
double
based
factorial()
methods. The fourth value
represents the
BigInteger
equivalent of the highest
double
factorial.
Noticethatthe
double
methodfailstoaccuratelyrepresent170!(!isthemathsym
bol for factorial). Its precision is simply too small. Although the method attempts to
roundthesmallestdigit,roundingdoesnotalwayswork—thenumberendsin7994in
stead of 7998. Rounding is only accurate up to argument 25.0, as the last two output
lines reveal.
Note
RSA encryption,
BigDecimal
, and factorial are practical examples of
BigInteger
's usefulness. However, you can also use
BigInteger
in unusual
ways.Forexample,myFebruary2006
JavaWorld
articletitled“TravelThroughTime
with Java” (
http://www.javaworld.com/javaworld/jw022006/
jw0213funandgames.html
)
, a part of my Java Fun and Games series, used
BigInteger
to store an image as a very large integer. The idea was to experiment
with
BigInteger
methodstolookforimages(possiblybydiscoveringmathematical
patterns)ofpeopleandplacesthatexistedinthepast,willexistinthefuture,ormight
never exist. If this craziness appeals to you, check out my article.
EXERCISES
The following exercises are designed to test your understanding of Java's language
APIs:
1. A
prime number
is a positive integer greater than 1 that is evenly divisible
by 1 and itself. Create a
PrimeNumberTest
application that determines
ifitssolitaryintegerargumentisprimeornotprime,andoutputsasuitable
message. For example,
java PrimeNumberTest 289
should output
themessage
289 is not prime
.Asimplewaytocheckforprimality
is to loop from 2 through the square root of the integer argument, and use
the remainder operator in the loop to determine if the argument is divided