I'm trying to find the best way to solve for rational approximations of the square root of a number, given some pretty serious constraints on the operations I can use to calculate it. My criteria for the algorithm are that it must:
- be solvable through recursion
- operate solely on ratios of integers
- use only addition, subtraction, multiplication, division or equality
(The criteria are important because I'm trying to use c++ to approximate the root at compile-time using template-meta programming, and the available operations are extremely limited)
The approach that I'm considering is supposedly based on an ancient Babylonian method and involves iteratively solving:
$$ k_{n+1} = \frac{(k_{n} + N / k_{n})}{2} $$
Where:
- N is the number whose root we are looking for
- K is the approximation of the root
- K[0] is chosen such that the value of k^2 is less than N
So, it seems I could pretty trivially implement this algorithm if:
- I fix K{0} to be 1, which probably hurts convergence but will work for any N > 1
- If I choose a fixed recursion depth
The problems that I would like to solve are:
- Is there in general a well known method that is faster/more accurate/etc that I should use instead?
- Is there a smarter way that I can choose the initial value of K if N is known?
- Is it possible to easily choose a recursion depth that will converge to 10-14 decimal places, a priori, if N is known? OR
- Can I test for convergence somehow using only the arithmetic mentioned above and equality? (No type of comparison operations are available, but I could for example test that the difference between two ratios is exactly equal to some other ratio)
If it's not already obvious, I'm a programmer, not a mathematician, and have no formal experience with diophantine approximation.
The typical approach here is probably to use Newton's method (https://en.wikipedia.org/wiki/Newton's_method), which for this case gives:
$k_{n+1} = k_n - \frac{k_n^2-N}{2k_n}$
Since you are solving a quadratic this will always converge to the correct root (or it's negative) no matter what starting value you give, however it will be faster if you pick an initial value nearby. If you're able to, you could try combining a lookup table with linear interpolation to get a reasonable initial guess (but maybe that requires too much utility?)