利用逼近的思路直接二分开方找出值

 1 package lanqiao;
 2 
 3 import java.math.BigInteger;
 4 import java.util.Scanner;
 5 public class Main {
 6 
 7     static BigInteger cal(BigInteger x){
 8         BigInteger l = BigInteger.ONE ;
 9         BigInteger r = x ;
10         BigInteger temp = BigInteger.ZERO ;
11         while(!l.equals(r)){
12             BigInteger mid = (l.add(r)).divide(BigInteger.valueOf(2)) ;
13             if(temp.compareTo(BigInteger.ZERO)!=0&&temp.compareTo(mid)==0){
14                 break ;
15             }else{
16                 temp = mid ;
17             }
18             if(temp.compareTo(BigInteger.ZERO)==0){
19                 temp = mid ;
20             }
21             if(mid.multiply(mid).compareTo(x)==1){
22                 r=mid ;
23             }else{
24                 l=mid ;
25             }
26         }
27         if(l.multiply(l).compareTo(x)==1){
28             l=l.subtract(BigInteger.ONE) ;
29         }
30         return l;
31         
32     }
33     public static void main(String[] args) {
34         // TODO Auto-generated method stub
35         Scanner in = new Scanner(System.in) ;
36         
37         while(in.hasNextBigInteger()){
38             BigInteger n = in.nextBigInteger() ;
39             
40             
41             BigInteger x = cal(n) ;
42             System.out.println(x);
43             
44         }
45     }
46 
47 }