UVa - 10023 Square Root

//java solution

import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;

class Main
{

static BigInteger bigsqrt(BigInteger x)
{
            BigInteger x_0,x_1=x,div=new BigInteger("2");
            do
            {
                x_0=x_1;
                x_1=x_0.add(x.divide(x_0)).divide(div);

            }while(x_0.compareTo(x_1)!=0);
return x_1;
}

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin()
    {
     boolean nl=true;
BigInteger t=new BigInteger("0");
BigInteger n=new BigInteger("0");
   //  Scanner sc=new Scanner(new File("in.txt"));
Scanner sc=new Scanner(System.in);
PrintWriter pr=new PrintWriter(System.out); 
   //default delimiter : "\\s+"
     // that means spaces , tabs , newlines(form feeds , carriage returns)
     // are skipped
t=sc.nextBigInteger();
for(BigInteger i=new BigInteger("0");t.compareTo(i)==1;i=i.add(BigInteger.ONE))
{
n=sc.nextBigInteger();
n=bigsqrt(n);
if(nl)
{
nl=false;
}
else
{
pr.println();
}
pr.println(n);
}
pr.close();
sc.close();
    }


}

No comments:

Post a Comment