Does anyone have a code example of how to calculate the nth root of large numbers that have hundreds of digits?
Printable View
Does anyone have a code example of how to calculate the nth root of large numbers that have hundreds of digits?
You could try numerical methods (like Newton's Method) to solve the polynomial xn = c--that is, xn - c = 0. Newton's Method is rudimentary but should produce an accurate result in about as many iterations as you want digits of accuracy. Code for this method should be easy to come by or write yourself if you decide to try it.
You could also perform a successive guess-and-check if speed isn't an issue. Guess zero and some arbitrary number (perhaps c/n), then guess midway between the two, pick only the half that could possibly be correct, and repeat as needed. You would end up performing a binary search for the number which would converge quite quickly.
Good luck finding a method!
jemidiah,
Yeah, I'm already aware of the methods, but it's not as easy as you'd think because of the size of the numbers.
I've written some code to add, subtract, and multiply large integers, but I was hoping to refine some existing code for the nth root.
I got to remembering that Python uses infinite-precision integers. Perhaps you could check python.org for the C(++) code that handles this and use whatever they used.
Oh now you tell me. Just kiding.
Thanks, I'll take a look although I've been working on my own too.
Either way I'll be posting it openly for everyone in need.
Cheers