Talk:Nth root algorithm

message: Newton steps and time      bsearch steps and time
    2-root of 123^13              5 in 0.000124 s             45 in 0.000358 s
    3-root of 123^13              4 in 0.000081 s             30 in 0.000198 s
   15-root of 123^13              1 in 0.000053 s              6 in 0.000077 s
   37-root of 123^13            185 in 0.003073 s              2 in 0.000072 s
   68-root of 123^13              0 in 0.000040 s              1 in 0.000046 s
  111-root of 123^13              0 in 0.000029 s              0 in 0.000030 s
  150-root of 123^13              0 in 0.000028 s              0 in 0.000029 s
    2-root of 123^15              5 in 0.000083 s             52 in 0.000356 s
    3-root of 123^15              7 in 0.000203 s             34 in 0.000229 s
   15-root of 123^15             97 in 0.000851 s              6 in 0.000076 s
   37-root of 123^15            536 in 0.008276 s              2 in 0.000063 s
   68-root of 123^15              0 in 0.000039 s              1 in 0.000046 s
  111-root of 123^15              0 in 0.000029 s              0 in 0.000030 s
  150-root of 123^15              0 in 0.000028 s              0 in 0.000029 s
    2-root of 123^74              9 in 0.000191 s            256 in 0.002559 s
    3-root of 123^74              8 in 0.000207 s            171 in 0.001480 s
   15-root of 123^74             13 in 0.000239 s             34 in 0.001284 s
   37-root of 123^74            679 in 0.015252 s             13 in 0.000179 s
   68-root of 123^74           1471 in 0.057066 s              7 in 0.000138 s
  111-root of 123^74           4564 in 0.710314 s              4 in 0.000125 s
  150-root of 123^74           5358 in 1.115691 s              3 in 0.000112 s
   2-root of 123^137              9 in 0.000343 s            475 in 0.006759 s
   3-root of 123^137              7 in 0.000333 s            317 in 0.008288 s
  15-root of 123^137             28 in 0.000767 s             63 in 0.000929 s
  37-root of 123^137            517 in 0.015948 s             25 in 0.000565 s
  68-root of 123^137           2814 in 0.278242 s             13 in 0.000247 s
 111-root of 123^137           4291 in 0.634543 s              8 in 0.000209 s
 150-root of 123^137           4360 in 0.722420 s              6 in 0.000179 s
   2-root of 123^150              9 in 0.000388 s            520 in 0.007006 s
   3-root of 123^150              8 in 0.000509 s            347 in 0.007097 s
  15-root of 123^150             30 in 0.000885 s             69 in 0.001091 s
  37-root of 123^150             29 in 0.000684 s             28 in 0.000420 s
  68-root of 123^150            705 in 0.025949 s             15 in 0.000300 s
 111-root of 123^150           2709 in 0.244051 s              9 in 0.000241 s
 150-root of 123^150         13713 in 10.464777 s              6 in 0.000187 s
This clearly shows Newton being superior for small n, and bsearch superior for large n. Interesting, eh? Fredrik | talk 10:47, 25 August 2005 (UTC)
thread_title: More timing results

message: Today I needed to write a routine to take the integer nth root of a number (i.e. \lfloor x^{1/n}\rfloor.  I think the problem Frederik is seeing above is due to choosing a bad initial guess.  If you set
: x_0=2^{\lceil\lceil\log(A)\rceil/n\rceil}
(where \log is the log base 2) then Newton is very efficient.  And it has the advantage that you can use truncating integer arithmetic when calculating
: x_{k+1} = ((n-1)x_k +A/x_k^{n-1})/n
since each x_k is larger than the required answer.  For example, taking the 100th root of 12345678901234567890123456789012345^100 with this initial guess (Frederik's first comment) only requires 59 iterations.  dbenbenn | talk 10:34, 19 March 2006 (UTC)
thread_title:  Efficiency depends on choosing a good initial guess 

message: As far as I can see, it could be substantially faster to perform Newton iteration on \frac{1}{\sqrt[n]{X}} rather than \sqrt[n]{X}, by using an iteration like:
#Make an initial guess x_0
#Set x_{k+1} = \frac{1}{n} \left[{(n+1)x_k - A{x_k^{n+1}}}\right]
#Repeat step 2 until the desired precision is reached.
since you that way can get rid of the per-iteration division (which is much slower than multiplication on most modern computers). I'm a bit afraid that there could be issues with convergence radius or the WP:NOR guideline, which is why I am not just inserting it into the article itself. 80.202.214.26 16:44, 25 May 2006 (UTC)
replies: text: I just redid the derivation from Newton's iteration where  f(x) = \frac{1}{X^n}-A and it agrees with the above users derivation. 132.228.195.207 (talk) 02:20, 29 April 2009 (UTC)
thread_title:  Other possible way to do the iteration 

message: Halley's method is a well-documented root finding algorithm that can be applied to finding nth roots.  It has the benefit of converging in cubic time as opposed to quadratic time for newton's method which is already covered in the article.  The only catch is that the initial seed has to be a good guess for it to converge rapidly:
* x_{k+1} = x_k \left[{\frac{(n+1)A + (n-1)x_k^n}{(n-1)A + (n+1)x_k^n}}\right]
Cnadolski (talk) 21:01, 16 September 2010 (UTC)
thread_title:  What about Halley's method? 

message: Nth root can be calculated using the maclaurin series of exp and ln like this
x^{\frac{1}{n}} = e^{\frac{1}{n} \log{(x)}}
\ln{\left(\frac{1+x}{1-x}\right)} = \sum^{\infty}_{n=0}2 \frac{x^{2 n+1}}{2 n+1}
\log{x} = \ln{\frac{x-1}{1+x}}
e^x = \sum^{\infty}_{n=0}\frac{x^n}{n!}
Since n is always an integer >= 0, a simple pow function can be used for the calculations.
Using Common Lisp as a programming language.
example:
 (defun powint (a b)
  (if (= b 0)
      1
    (* a (powint a (- b 1)))))</lang>
Ofcourse it's not possible to calculate up to an infinity in a computer, but the value can be calculated til it is good enough.
Using a library that implements a type that includes nominator and a denominator (like GMP's mpt_q), it is possible to calculate
the Nth root using only integers.
Maybe not the best solution but it could be something like this
 (defun factorial (n)
  (if (= n 0)
      1
    (* n (factorial (- n 1)))))
 (defun powint (a b)
  (if (= b 0)
      1
    (* a (powint a (- b 1)))))
 (defun logS(x n) (if (= n 0) (* 2 x) (+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) (logS x (- n 1)) )  ))
 (defun loge(x n) (logS (/ (- x 1) (+ 1 x)) n))
 (defun expon(x n) (if (= n 0) 1 (+ (/ (powint x n) (factorial n)) (expon x (- n 1)))))
 (defun pown(x a n) (expon (* a (loge x n)) n))
example output:
 [9]> (* (pown 2 1/3 20) 1.0)
 1.2599211
--Spektrum1983 14:12, 29 November 2011 (UTC)
thread_title: Maclaurin Series