I am just playing around with approximating functions, and was curious if anyone can find a better or different way to approximate sqrt(x). Hopefully we'll find something interesting or clever.
Here's my code written in Java, but any language is fine. ``` public static double sqrtApprox(int n) { double i = 1; while (i * i < n) i++; i--; return .5 * (n + i * i) / i; } ``` It's based on the idea that we can sort of use the limit definition, so it's better the closer your number is to a perfect square. \[\Large f'(a) = \frac{f(a+h)-f(a)}{h} \implies f(a)+h f'(a) \approx f(a+h)\]
Well, it can always bounce around...
if the first estimate is close enough, you can iterate using Newton's method: x1=x-f(x)/f'(x) so x1=x-(x^2-N)/(2x) where N is number whose square-root is to be found. Accuracy (in number of significant figures) doubles with every iteration (if it converges).
Using your proposed algorithm, you can improve accuracy by multiplying by 10^(2n), and dividing the result by 10^n. However, in that case, you're better off using the bisection algorithm or else it will run for a long time. Example: sqrt(2)=sqrt(20000)/100=141/100=1.41
Join our real-time social learning platform and learn together with your friends!