62,614
社区成员
发帖
与我相关
我的任务
分享
String str = "12345678901234567890";
BigInteger input = new BigInteger(str);
BigInteger max = input;
BigInteger min = BigInteger.ONE;
BigInteger two = new BigInteger("2");
BigInteger avg = BigInteger.ZERO;
BigInteger lastAvg = null;
while(!avg.equals(lastAvg)) {
lastAvg = avg;
avg = max.add(min).divide(two);
int res = avg.multiply(avg).compareTo(input);
if(res == 0)
break;
if(res > 0)
max = avg;
else
min = avg;
}
System.out.println(avg);