RosettaCodeData/Task/Nth-root/S-BASIC/nth-root-2.basic

27 lines
624 B
Text
Raw Permalink Normal View History

2025-02-27 18:35:13 -05:00
rem - return the nth root of x to stated precision
2023-07-01 11:58:00 -04:00
function nthroot(n, x, precision = real.double) = real.double
var x0, x1 = real.double
x0 = x
2025-02-27 18:35:13 -05:00
x1 = x / n rem - initial guess
2023-07-01 11:58:00 -04:00
while abs(x1 - x0) > precision do
begin
x0 = x1
x1 = ((n-1.0) * x1 + x / x1 ^ (n-1.0)) / n
end
end = x1
rem -- exercise the routine
var i = integer
2025-02-27 18:35:13 -05:00
var x = real.double
x = 144
print "Finding the nth root of"; x; " to 8 decimal places"
2023-07-01 11:58:00 -04:00
print " x n root"
print "------------------------"
2025-02-27 18:35:13 -05:00
for i = 2 to 8
print using "### #### ###.########"; x; i; nthroot(i, x, 1E-9)
2023-07-01 11:58:00 -04:00
next i
end