nth root

casevh casevh at gmail.com
Mon Feb 2 08:01:58 CET 2009


On Feb 1, 10:02 pm, Mensanator <mensana... at aol.com> wrote:
> On Feb 1, 8:20 pm, casevh <cas... at gmail.com> wrote:
>
>
>
> > On Feb 1, 1:04 pm, Mensanator <mensana... at aol.com> wrote:
>
> > > On Feb 1, 2:27 am, casevh <cas... at gmail.com> wrote:
>
> > > > On Jan 31, 9:36 pm, "Tim Roberts" <t.robe... at cqu.edu.au> wrote:
>
> > > > > Actually, all I'm interested in is whether the 100 digit numbers have an exact integral root, or not. At the moment, because of accuracy concerns, I'm doing something like
>
> > > > > for root in powersp:
> > > > > nroot = round(bignum**(1.0/root))
> > > > > if bignum==long(nroot)**root:
> > > > > .........
> > > > > which is probably very inefficient, but I can't see anything better.....
>
> > > > > Tim
>
> > > > Take a look at gmpy and the is_power function. I think it will do
> > > > exactly what you want.
>
> > > And the root function will give you the root AND tell you whether
> > > it was an integral root:
>
> > > >>> gmpy.root(a,13)
>
> > > (mpz(3221), 0)
>
> > > In this case, it wasn't.
>
> > I think the original poster wants to know if a large number has an
> > exact integral root for any exponent. is_power will give you an answer
> > to that question but won't tell you what the root or exponent is. Once
> > you know that the number is a perfect power, you can root to find the
> > root.
>
> But how do you know what exponent to use?

That's the gotcha. :) You still need to test all prime exponents until
you find the correct one. But it is much faster to use is_power to
check whether or not a number has representation as a**b and then try
all the possible exponents than to just try all the possible exponents
on all the numbers.

>
>
>
> > > >http://code.google.com/p/gmpy/
>
> > > > casevh
>
>




More information about the Python-list mailing list