RosettaCodeData/Task/Nth-root/Groovy/nth-root-1.groovy
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

13 lines
355 B
Groovy

import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}