Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to find the root of a large number in Java!
How to find the root of a large number in Java!
In java, for large integers, large decimal roots do not provide functions. You can refer to the following implementation methods:

Import java.math.bigdecimal;

Import java.math.biginteger;

Public class BigSquareRoot {

Final static biginteger Bailey = biginteger.valueof (100);

public static BigDecimal sqrt(BigDecimal number,int scale,int roundingMode) {

if (number.compareTo(BigDecimal。 Zero)<0)

Throw a new arithmetic exception ("sqrt with negative");

big integer integer = number . tobiginteger();

string buffer sb = new string buffer();

string strInt = integer . tostring();

int lenInt = strint . length();

if (lenInt % 2! = 0) {

strInt =“0”+strInt;

len int++;

}

BigInteger res = BigInteger zero;

BigInteger rem = BigInteger zero;

for(int I = 0; I & ltlenint/2; i++) {

res = res.multiply(BigInteger。 10);

Rem = rem.multiply (hundred);

big integer temp = new big integer(strint . substring(I * 2,I * 2+2));

rem = rem . add(temp);

Big integer j = big integer. Ten;

while (j.compareTo(BigInteger。 Zero) > 0) {

j = j.subtract(BigInteger。 1);

if (((res.add(j))。 Multiply by (j)). compare to(rem)& lt; = 0) {

Break;

}

}

RES = RES . add(j);

rem = rem . subtract(RES . multiply(j));

RES = RES . add(j);

Sb. add (j);

}

Someone adds ('));

BigDecimal fraction = number . subtract(number . set scale(0,BigDecimal。 ROUND _ DOWN));

int frac len =(fraction . scale()+ 1)/2;

fraction = fraction . movepointright(frac len * 2);

string str frac = fraction . toplainstring();

for(int I = 0; I < = scale; i++) {

res = res.multiply(BigInteger。 10);

Rem = rem.multiply (hundred);

If (I & ltfracLen) {

big integer temp = new big integer(str frac . substring(I * 2,I * 2+2));

rem = rem . add(temp);

}

Big integer j = big integer. Ten;

while (j.compareTo(BigInteger。 Zero) > 0) {

j = j.subtract(BigInteger。 1);

if (((res.add(j))。 Multiply by (j)). compare to(rem)& lt; = 0) {

Break;

}

}

RES = RES . add(j);

rem = rem . subtract(RES . multiply(j));

RES = RES . add(j);

Sb. add (j);

}

Returns a new BigDecimal(sb.toString ()). setScale(scale,rounding mode);

}

Public static bigdecimal sqrt (bigdecimal number, int scale) (

Returns sqrt(number, scale, BigDecimal). ROUND _ HALF _ UP);

}

Public static bigdecimal sqrt (bigdecimal number) {

int scale = number . scale()* 2;

if(scale & lt; 50)

Proportion = 50;

Returns sqrt(number, scale, BigDecimal). ROUND _ HALF _ UP);

}

Public static void main (strinargs []) {

BigDecimal num = new BigDecimal(" 65 103545 13.65648974 135 145684 13 ");

long time = system . nano time();

BigDecimal root = sqrt(num, 1000);

time = system . nano time()-time;

system . out . println(root);

system . out . println(root . pow(2));

system . out . println(time);

}

}

Implementation results:

80686.76789695 129274934 163 1674 1557266722739984372 15 16347 158767523580494926630778 1 7843059095 146637 9 1 1 18049088575874465 127328 13032883 1737488533260705 1330 1 76028572558 1720542 170290426420802846 5438+02 195089 16055 18862273493239 19 1320 132 / kloc-0/482936884453475292438465 1775 10253838847 1074286543 8+092523540 14378344895438280908 159584992 1 / kloc-0/204 135480843346632 15893873 18739 1659928 1337739966965438+ 0705498 1 1 70407625807865354874900325 150479 12273090549 1370 106292985850043374597 163 1 9984878355766005793739 29233933246442803804 1322980 16737 1596723 174825202497634647 1358 1048 1429 / kloc-0/550900 1995943 19246543 8+056948 15489364740 1523 124 1673630 12332695879 106288856 / kloc-0/4893 1252358224933 17 1849 17626076 2233258 1940240322053 1926392808333854523694780539563293 1 332327299009882430 1346402044097639608479673900258 1380094075 1 692874927 1030 107 14873 1249 / kloc-0/5302903422 1356905368046 190 190748 1289230 152643599970 13886654 38+0788489599 1 1 86748498 15 164425 194 13840 19 1849923300957 165076 162594378 1367455 1 06 5438+00 1972034874 1842 17 17729 159422780 1 190559403 1830367343 193606047 1 2437396895 1524359600676406 16250636288 1367

65 103545 13.65648974 135 145684 1 29999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999926 10 18570 18822 195229 120 1 08939890605223 1483 1 12444 155 137889353596770526763 176032 1528768 168 7248342873265 / kloc-0/504536335267475372360 14827438259428569752954222443778544944033 142404025 1426099649509 195223 1 436565438+ 0828659587364 153208947 1369852852836288689 1537824020 10942 1322773475625 1 495878566340409599684823659444904292099882985 668906808623 17720866525 19 19370268937893457306 12 1 80276656627062704 16805262523684 1 102345948799655 16 5438+0046039 1390302950 170083523059788586776 1 18307 1537 17 19588265686850863492 1003283484 107506056543 8+02309444622 1456 1655 1 / kloc-0/08 1 198936238645790 138 137 1094 1 16726 1 17997257 1 233574705638862 / kloc-0/4035746556929599452326 174296080759360 1727929262728856 15388050356 14597363009 10 1 032997078737707 132670 18 154 17 1 165545 17843000234245994056 1678884530 15 1 166769964 18045399873420955 405 169 12223265537007 1294850845464960894274665 1 683572634224323098274435576290709769 148239 120722342 126902645574 6097705582 1 18299727055 14 1 530064348466 140 1600695692 15945066067588326622405934859626299 103202056784740473 222325770445674793369858309345342905 15788689

46726 188