Find a square root of a number without using the square root function.
The number is n and we guess the square root is a
While(1)
{
If(a*a==n)
Return a;
a=(a+n/a)/2;
}
If a is square root of n, a^2=n, a=n/a, if a is not square root of n, sr of n must be between a and n/a, so the average b=1/2(a+a/n) create a shorter distance between b and n/b where sr of n lies in, we do the iteration until we get the square root.
Or
1+3=4
1+3+5=9
1+3+5+7=16
….
sum=0;
for(int n=1;sum < num;n++)
sum=sum+(2n-1);
return n;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment