LOGARITHMS
Java provides us with only one method for working with Logarithms:
java.lang.Math.log(). It returns the log (base 10) of the number
passed as its argument. In the absence of functions for log base 2 and
log base e, we must create our own. Here's the basic formula:
With this information it is not difficult to define methods which
return the log base anything of any number.
A problem occurs, however, when you work with really big numbers (the
kind that require more than 64-bits of memory). Java provides a class
called BigInteger which will deal with these really big numbers, but
it doesn't provide a method to find logarithmic values for these numbers.
So, you've got to come up with a way to find logarithms for really big numbers
on your own.
There are two strategies for dealing with this situation:
1. Calculate the Log Value:
You will need to experiment with this infinite series to figure out how
far you have to go to get a good approximation of log values (in fact, that's
what your assignment is going to be).
2. Use the Sum of Factors:
If you can factor a very large number into values which can be handled by
a 64-bit long variable, then you can find its sum. I used this strategy in
the example shown below to find the value of n!. Instead of actually calculating
the value of n! and then attempting to determine it's log, I simply calculated
the sum of log 1 + log 2 + log 3 + ... log n.
Example:
Assignment: As mentioned above, you will create an applet which
implements the formula shown above for calculating a log base e value.
You will provide one
input textarea and two output textareas. One will show the value generated by
your function using the above equation and the other will show the value
generated by the log function provided with Java. FYI, java.lang.math provides
a double variable E.