Re: Square Root Of java.math.BigInteger



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;
}


.



Relevant Pages

  • Re: Square Root Of java.math.BigInteger
    ... just to not to confuse you, it actually finds the nth root of any number, ... int workingDecimalPlaceNumber; ... BigDecimal numerator; ...
    (comp.lang.java.programmer)
  • prog for electronis lessons.
    ... int vars_count; ... return res; ... int comparator(const void *p1, const void *p2) ... int print_tree(NODE *root) ...
    (Debian-User)
  • =?windows-1252?Q?Re=3A_E=3Dmc=28squared=29_=3D_E=3Dmc=28circled=29=2C_and_celestial_orb?= =?wind
    ... I find it interesting that both -1 and pi don’t seem to have a square ... root until you consider the natural unite c. ... also the basic natural unites of, E, M(mass), T, Q ... waves, to create rest mass particles, which are quantum particles, ...
    (sci.math)
  • Re: tree roots
    ... expressed an interest in getting rid of the tree. ... The surface root is raising the fence and Iwant to ... I had pine tree roots lifting my sidewalk. ... sidewalk square back, and I had to do it two more times. ...
    (alt.home.repair)
  • Graphs and identifying adjacent nodes
    ... I have an applet which has a graph that has a central node (root) from ... int from; ...
    (comp.lang.java.programmer)

Loading