Provided L (the list of roots) is always in ascending order we could do:
return L[-1]
But the main issue of my solution is:
it is too slow and even worse it gulps so much memory that it fails
For example:
*nth_real_root_2(4,2000)*
Traceback (click to the left of this block for traceback)
...
MemoryError: Unable to allocate 4096000000 bytes memory for PARI.
Am Donnerstag, 17. Juli 2014 19:29:41 UTC+2 schrieb Martin Flashman:
>
> Norbert's solution is short but unfortunately for nth_real_root(4,2)
> gives a result of -2 instead of what the TC MITS expected result of 2.
>
> One slight modification handles that issue to give results that match
> these expectations- Unfortunately- this function doesn't seem to plot!???:
>
> def nth_real_root(a,n):
>
> p = x^n - a
> L = p.roots(ring=RR,
> multiplicities=False)
> if L:
> return sgn(a)*sgn(L[0])*L[0] # return L ?
> else:
> raise RuntimeError("no real root")
>
>
>
> ALSO-
> On naming the function: eventually a shorter name would be helpful if this
> is to become a core real function:
> perhaps r_rootn(x,n) or simply rootn(x,n).
>
> From a foggy morning in Arcata,
> Martin Flashman
> On Monday, July 14, 2014 1:13:55 PM UTC-7, Norbert Domes wrote:
>>
>> My suggestion for nth real root:
>>
>>
>>
>> def nth_real_root(a,n):
>>
>> p = x^n - a
>> L = p.roots(ring=RR,multiplicities=False)
>> if L:
>> return L[0] # return L ?
>> else:
>> raise RuntimeError("no real root")
>>
>
--
You received this message because you are subscribed to the Google Groups
"sage-edu" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-edu.
For more options, visit https://groups.google.com/d/optout.