18 lines
377 B
Text
18 lines
377 B
Text
class NthRoot {
|
|
function : Main(args : String[]) ~ Nil {
|
|
NthRoot(5, 34, .001)->PrintLine();
|
|
}
|
|
|
|
function : NthRoot(n : Int, A: Float, p : Float) ~ Float {
|
|
x := Float->New[2];
|
|
x[0] := A;
|
|
x[1] := A / n;
|
|
|
|
while((x[1] - x[0])->Abs() > p) {
|
|
x[0] := x[1];
|
|
x[1] := ((n - 1.0) * x[1] + A / x[1]->Power(n - 1.0)) / n;
|
|
};
|
|
|
|
return x[1];
|
|
}
|
|
}
|