(Edited to take comment into account.)
For square roots, the function you are looking for is isqrt
(for "integer square root").
sage: isqrt(124)
11
Also available as a method of integers:
sage: 124.isqrt()
11
For n-th roots with n other than 2, one can build on @castor's answer and define:
def inthroot(a, n):
"""
Return the integer `n`-th root of `a`, rounded towards zero.
EXAMPLES::
sage: a, b, c, d, e, f = 26, 27, 28, -26, -27, -28
sage: inthroot(a, 3)
2
sage: inthroot(b, 3)
3
sage: inthroot(c, 3)
3
sage: inthroot(d, 3)
-2
sage: inthroot(e, 3)
-3
sage: inthroot(f, 3)
-3
"""
return a.nth_root(n, truncate_mode=True)[0]
The [0]
at the end discards the boolean indicating whether the n-th root
extraction was exact.
Alternatively, one could imagine modifying the nth_root
method
in src/sage/rings/integer.pyx
, adding an optional argument to say
whether one wants that boolean or not. The call sequence would
change from the current
def nth_root(self, int n, bint truncate_mode=0):
to either
def nth_root(self, int n, bint truncate_mode=0, bint return_whether_exact=1):
or
def nth_root(self, int n, bint truncate_mode=0, bint return_whether_exact=0):
depending on whether we decide the default should be to return that boolean or not.
To check the current version of the nth_root
method, do this:
sage: a = 2
sage: a.nth_root??
or look at it online, say at GitHub:
This exists at least for square roots: it is called
sqrtrem()
and also provides the remainder.