Computer Science, asked by camlin6249, 1 year ago

Design an algorithm for computing √n for any positive integer n.

Answers

Answered by Snowden1738
5
double sqrt(int n, int prec)
{
double s = 0, d = 1;
for(int i = 0; i < prec; i++, d /= 10)
{
for(; (s + d) * (s + d) <= n; s += d);
if(s * s == n) return s;
}
return s;
}

// prec stands for precision

Snowden1738: There's a small mistake that just got unnoticed: set the int n in that argument to double n.
Similar questions