RosettaCodeData/Task/Nth-root/EMal/nth-root.emal
2024-10-16 18:07:41 -07:00

12 lines
754 B
Text

fun nthRoot ← real by real n, real x, real precision
Pair result ← real%real(x ⇒ x / n).named("previous", "current")
while abs(result.current - result.previous) > precision
result.previous ← result.current
result.current ← ( (n - 1) * result.current + x / result.current ** (n - 1) ) / n
end
return result.current
end
writeLine("{ \"nthroot\": ", nthRoot(2, 152.2756, 0.00000000000001), ", \"builtin\": ", √152.2756, " }")
writeLine("{ \"nthroot\": ", nthRoot(5, 34.0, 0.00000000000001), ", \"builtin\": ", 5 // 34.0, " }")
writeLine("{ \"nthroot\": ", nthRoot(10, 42.0, 0.00000000000001), ", \"builtin\": ", 10 // 42.0, " }")
writeLine("{ \"nthroot\": ", nthRoot(0.5, 7.0, 0.000000000001), ", \"builtin\": ", 0.5 // 7, " }")