nth root

Tim Roberts t.roberts at cqu.edu.au
Sat Jan 31 05:00:09 CET 2009

Unfortunately, unless I'm doing something wrong, this appears to take 20 times as long... :-)
What on earth are numpy and psyco?  Do I need to watch the Lord of the Rings?
Tim wrote:
> In PythonWin I'm running a program to find the 13th root (say) of
> millions of hundred-digit numbers.  I'm using
>     n = 13
>     root = base**(1.0/n)
> which correctly computes the root to a large number of decimal
> places, but therefore takes a long time.  All I need is the integer
> component.  Is there a quicker way?
Have you tried an iterative approach?

def root_13(x):
     step = 1
     while step ** 13 < x:
         step *= 2
     root = 0
     while step:
         if (root + step) ** 13 <= x:
             root += step
         step //= 2
     return root

Probably not that fast in Python, though. numpy or psyco might help.

More information about the Python-list mailing list