Re: Square Root Of java.math.BigInteger
- From: "Jeremy Watts" <jwatts1970@xxxxxxxxxxx>
- Date: Fri, 23 May 2008 09:45:54 +0100
I have a BigDecimal square root finder here you can have, just use it and
round up/down the resulting decimal and convert it to a BigInteger.
It uses Newton-Raphson.
/**
* Write a description of class bigRoot here.
*
* @author jeremy watts
* @version (a version number or a date)
* @modified 22/10/06
*/
import java.math.BigDecimal;
public class bigRoot
{
// instance variables - replace the example below with your own
public static void main(String[] args)
{
BigDecimal argument;
BigDecimal result;
BigDecimal reconstituted;
int workingDecimalPlaceNumber;
argument = new BigDecimal("8.0");
workingDecimalPlaceNumber = 25;
int root = 10;
int index;
result = bigRoot(argument, root, workingDecimalPlaceNumber);
reconstituted = result;
reconstituted = reconstituted.pow(root);
System.out.println(result);
System.out.println(reconstituted);
}
public static BigDecimal bigRoot(BigDecimal argument, int root, int
workingDecimalPlaceNumber)
{
/* returns uncorrected root of a BigDecimal - uses the Newton Raphson
method.
* argument must be positive
*/
BigDecimal result;
BigDecimal xn;
BigDecimal oldxn;
BigDecimal xnPlusOne;
BigDecimal numerator;
BigDecimal denominator;
BigDecimal quotient;
BigDecimal constant;
int index;
int runIndex;
int iterationNumber = 200;
constant = new BigDecimal(root);
boolean halt;
if (argument.compareTo(BigDecimal.ZERO) != 0) {
xn = argument;
oldxn = xn;
halt = false;
runIndex = 1;
while ((halt == false) & (runIndex <= iterationNumber)) {
oldxn = xn;
numerator = xn;
denominator = numerator;
numerator = numerator.pow(root);
denominator = denominator.pow(root - 1);
denominator = (constant.multiply(denominator));
numerator = (numerator.subtract(argument));
if (denominator.compareTo(BigDecimal.ZERO) == 0) {
halt = true;
}
else {
quotient = (numerator.divide(denominator,
workingDecimalPlaceNumber, BigDecimal.ROUND_HALF_UP));
xnPlusOne = (xn.subtract(quotient));
xnPlusOne = xnPlusOne.setScale(workingDecimalPlaceNumber,
BigDecimal.ROUND_HALF_UP);
xn = xnPlusOne;
if (xnPlusOne.compareTo(oldxn) == 0) {
halt = true;
}
}
runIndex++;
}
result = xn;
}
else {
result = BigDecimal.ZERO;
}
return(result);
}
}
"j1mb0jay" <j1mb0jay@xxxxxxxxx> wrote in message
news:spbdg5xp2l.ln2@xxxxxxxxxxxxxxxxxx
I was very surprised to see that java did not have a square root method
at all of the java.math.BigInteger class, why is this?
I have tried to write a simple trial and error Square Root function that
works with BigIntegers. It does not return decimal values and instead
returns null if the number is decimal, or rounds the numbers depending.
The code is below, i am currently trying to re-implement my prime checker
using BigIntegers and require this method, it seems to find the square
root of several thousand digit numbers in just over a second which i
think is pretty good, please can i have some thoughts on how to improve
the code.
Regards j1mb0jay.
--------------------Some Code-------------------------------------
/**
* A number is square free if it is divisible by no perfect
square (except 1).
*
* @param testSubject
* @return - true if the "testSubject" is a square free number.
*/
public static boolean isSquareFree(double testSubject)
{
double answer;
for(int i = 1; i < testSubject; i++)
{
answer = Math.sqrt(testSubject / (double)i);
if(answer < 1)
return false;
if(answer % 1.0 == 0)
return false;
}
return true;
}
.
- Follow-Ups:
- Re: Square Root Of java.math.BigInteger
- From: Jeremy Watts
- Re: Square Root Of java.math.BigInteger
- Prev by Date: Re: math equations in java
- Next by Date: Re: error with arrayList
- Previous by thread: Re: Square Root Of java.math.BigInteger
- Next by thread: Re: Square Root Of java.math.BigInteger
- Index(es):
Relevant Pages
|
Loading