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

17 lines
301 B
Text

vardef mnthroot(expr n, A) =
x0 := A / n;
m := n - 1;
forever:
x1 := (m*x0 + A/(x0 ** m)) / n;
exitif abs(x1 - x0) < abs(x0 * 0.0001);
x0 := x1;
endfor;
x1
enddef;
primarydef n nthroot A = mnthroot(n, A) enddef;
show 5 nthroot 34; % 2.0244
show 0.5 nthroot 7; % 49.00528
bye