大数快速开方!
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
BigInteger N = sc.nextBigInteger();
System.out.println(sqrt(N));
}
public static BigInteger sqrt(BigInteger input)
{
BigInteger x0 = BigInteger.TEN.pow((int)Math.sqrt(input.toString().length()));
BigInteger x1 = BigInteger.valueOf(11);
while(!x0.equals(x1) && !x0.equals(x1.add(BigInteger.ONE)))
{
x0 = new BigInteger(x1.toString());
x1 = x1.add(input.divide(x1)).divide(new BigInteger("2"));
}
return x1;
}
}
本文介绍了一种用于计算大数开方的高效算法,通过迭代逼近的方法,实现快速准确的大数开方运算。
7547

被折叠的 条评论
为什么被折叠?



