I have tried to find out the square root but could have precision only up to double. So, is there a possible way to find the square root of a big number with greater precision than double…Please HELP!!

@ab1234

Can’t we simply use pow() function of the BigDecimal class.

i.e. BigDecimal sqrt=(new BigDecimal(b)).pow(0.5)…

Hope this helps…

```
for(int i=0;i<=150;i++)
{
mid=(lo.add(hi)).divide(two);
BigDecimal ans2=mid.multiply(mid);
if(ans2.compareTo(b)==0) break;
else if(mid.multiply(mid).compareTo(b)<0)lo=mid;
else hi=mid;
}
```

‘int i’ for what purpose ? you haven’t use it in the block why and how ?

*I’m new to java.

Try using this:

```
public static BigInteger sqrt(BigInteger n) {
BigInteger a = BigInteger.ONE;
BigInteger b = new BigInteger(n.shiftRight(5).add(new BigInteger("8")).toString());
while(b.compareTo(a) >= 0) {
BigInteger mid = new BigInteger(a.add(b).shiftRight(1).toString());
if(mid.multiply(mid).compareTo(n) > 0) b = mid.subtract(BigInteger.ONE);
else a = mid.add(BigInteger.ONE);
}
return a.subtract(BigInteger.ONE);
}
```

Hope this Helps

@dutta here is my crrct implementation as far i know chk this out.

use the binary search along with properties…

this is my own idea which i tried to perform initially, but it’s fails.

i actually divided the question into two part first to find the integer solution of the given number using binary search

and for decimal one i break 1 into upto the desired sol.

like if u have any x

it can be written as x=(a+b)^2;

x=a^2+b^2+2*a*b; where it’s a is integer sol of the x and b is decimal part hope this will help you

similar problem on this topic is http://www.spoj.com/problems/CUBERT/

/*Author- Akshay verma

Aim- To find squareroots of a given positive number

Idea- Use a covergent sequence whose square converges to the given positive number (binary search)

*/

using namespace std;

inline double mean (double a, double b) //function that generates mean

{

return (a+b)/2.0;

}

int main()

{

double a;

```
cin>>a;
double l,m,u;
int i;
for (i=0; i*i<=a; i++){}
l=(double)i-1.0;
u=(double)i;
m=mean(l,u);
for (int i=0; i<=1000; i++)
{
if (m*m<a)
{l=m;
m=mean(l,u);
}
if (m*m>a)
{
u=m;
m=mean(l,u);
}
if (m*m==a)
{
break;
}
}
printf("%lf\n",m);
return 0;
```

}

i think no it will not wrk.

This won’t work. BigDecimal.pow and BigInteger.pow both require ints as arguments.

Just to have a loop, that runs 150 times…

it’s the fact i wrote that bsearch give the answer in log n tym

ur code will work for only perfect square…

haha… So it took you a day to figure that out.

NOT BAD, I would say.

i am not giving too much tym for it my labs are going on…

any primary student can understand it easily as u used biginteger in ur code…

try to find the mistake in this

http://ideone.com/QHo4gq

PS i am not good coder as you may be that’s my problem…

Well actually your solution is good. Breaking the root down into an integer and a decimal was nice. And that function I used up there, was for the bullseye problem for code jam 1A this year, which basically required the floored integral value of the root. That was just provided as a platform for this guy dutta to proceed.

P.S. Don’t be so desperate to show others off that you are a good programmer, providing them with codes, copying codes, and stuff. That ain’t gonna take you anywhere.

P.P.S. By giving out codes (or taking in for that matter) wont help anyone. But sharing an approach would.

u r ryt but this is my own idea which i tried to perform initially, but it’s fails.

i actually divided the question into two part first to find the integer solution of the given number using binary search

and for decimal one i break 1 into upto the desired sol.

like if u have any x

it can be written as x=(a+b)^2;

x=a^2+b^2+2*a*b; where it’s a is integer sol of the x and b is decimal part hope this will help you