Nth root: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
Dialects of BASIC moved to the BASIC section. |
||
Line 646: | Line 646: | ||
See also the [[#Liberty BASIC|Liberty BASIC]] and [[#PureBasic|PureBasic]] solutions. |
See also the [[#Liberty BASIC|Liberty BASIC]] and [[#PureBasic|PureBasic]] solutions. |
||
=={{header|Basic09}}== |
==={{header|Basic09}}=== |
||
{{Works with |OS9 operating system -- RUN nth(root,number,precision)}} |
{{Works with |OS9 operating system -- RUN nth(root,number,precision)}} |
||
<syntaxhighlight lang="basic09"> |
<syntaxhighlight lang="basic09"> |
||
Line 664: | Line 664: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|BASIC256}}== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang="freebasic">function nth_root(n, a) |
<syntaxhighlight lang="freebasic">function nth_root(n, a) |
||
precision = 0.0001 |
precision = 0.0001 |
||
Line 690: | Line 690: | ||
next n</syntaxhighlight> |
next n</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
<syntaxhighlight lang="bbcbasic"> *FLOAT 64 |
<syntaxhighlight lang="bbcbasic"> *FLOAT 64 |
||
Line 709: | Line 709: | ||
Cube root of 5 is 1.709975946677 |
Cube root of 5 is 1.709975946677 |
||
125th root of 5643 is 1.071549111198 |
125th root of 5643 is 1.071549111198 |
||
</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic">' version 14-01-2019 |
|||
' compile with: fbc -s console |
|||
Function nth_root(n As Integer, number As Double) As Double |
|||
Dim As Double a1 = number / n, a2 , a3 |
|||
Do |
|||
a3 = Abs(a2 - a1) |
|||
a2 = ((n -1) * a1 + number / a1 ^ (n -1)) / n |
|||
Swap a1, a2 |
|||
Loop Until Abs(a2 - a1) = a3 |
|||
Return a1 |
|||
End Function |
|||
' ------=< MAIN >=------ |
|||
Dim As UInteger n |
|||
Dim As Double tmp |
|||
Print |
|||
Print " n 5643 ^ 1 / n nth_root ^ n" |
|||
Print " ------------------------------------" |
|||
For n = 3 To 11 Step 2 |
|||
tmp = nth_root(n, 5643) |
|||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n |
|||
Next |
|||
Print |
|||
For n = 25 To 125 Step 25 |
|||
tmp = nth_root(n, 5643) |
|||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n |
|||
Next |
|||
' empty keyboard buffer |
|||
While Inkey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre> n 5643 ^ 1 / n nth_root ^ n |
|||
------------------------------------ |
|||
3 17.80341642 5643.00000000 |
|||
5 5.62732516 5643.00000000 |
|||
7 3.43502583 5643.00000000 |
|||
9 2.61116581 5643.00000000 |
|||
11 2.19303907 5643.00000000 |
|||
25 1.41273402 5643.00000000 |
|||
50 1.18858488 5643.00000000 |
|||
75 1.12207047 5643.00000000 |
|||
100 1.09022240 5643.00000000 |
|||
125 1.07154911 5643.00000000</pre> |
|||
==={{header|FutureBasic}}=== |
|||
<syntaxhighlight lang="futurebasic">window 1 |
|||
local fn NthRoot( root as long, a as long, precision as double ) as double |
|||
double x0, x1 |
|||
x0 = a : x1 = a /root |
|||
while ( abs( x1 - x0 ) > precision ) |
|||
x0 = x1 |
|||
x1 = ( ( root -1.0 ) * x1 + a / x1 ^ ( root -1.0 ) ) /root |
|||
wend |
|||
end fn = x1 |
|||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 ) |
|||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 ) |
|||
print " 125th Root of 5643 Precision .00001", using "#.###############"; fn NthRoot( 125, 5642, 0.00001 ) |
|||
print " Cube Root of 27 Precision .00001", using "#.###############"; fn NthRoot( 3, 27, 0.00001 ) |
|||
print "Square Root of 2 Precision .00001", using "#.###############"; fn NthRoot( 2, 2, 0.00001 ) |
|||
print "Square Root of 2 Precision .00001", using "#.###############"; sqr(2) // Processor floating point calc deviation |
|||
print " 10th Root of 1024 Precision .00001", using "#.###############"; fn NthRoot( 10, 1024, 0.00001 ) |
|||
print " 5th Root of 34 Precision .00001", using "#.###############"; fn NthRoot( 5, 34, 0.00001 ) |
|||
HandleEvents</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
125th Root of 5643 Precision .001 1.071559602191682 |
|||
125th Root of 5643 Precision .001 1.071559602191682 |
|||
125th Root of 5643 Precision .00001 1.071547591944772 |
|||
Cube Root of 27 Precision .00001 3.000000000000002 |
|||
Square Root of 2 Precision .00001 1.414213562374690 |
|||
Square Root of 2 Precision .00001 1.414213562373095 |
|||
10th Root of 1024 Precision .00001 2.000000000000000 |
|||
5th Root of 34 Precision .00001 2.024397458499885 |
|||
</pre> |
|||
==={{header|Liberty BASIC}}=== |
|||
<syntaxhighlight lang="lb"> |
|||
print "First estimate is: ", using( "#.###############", NthRoot( 125, 5642, 0.001 )); |
|||
print " ... and better is: ", using( "#.###############", NthRoot( 125, 5642, 0.00001)) |
|||
print "125'th root of 5642 by LB's exponentiation operator is "; using( "#.###############", 5642^(1 /125)) |
|||
print "27^(1 / 3)", using( "#.###############", NthRoot( 3, 27, 0.00001)) |
|||
print "2^(1 / 2)", using( "#.###############", NthRoot( 2, 2, 0.00001)) |
|||
print "1024^(1 /10)", using( "#.###############", NthRoot( 10, 1024, 0.00001)) |
|||
wait |
|||
function NthRoot( n, A, p) |
|||
x( 0) =A |
|||
x( 1) =A /n |
|||
while abs( x( 1) -x( 0)) >p |
|||
x( 0) =x( 1) |
|||
x( 1) =( ( n -1.0) *x( 1) +A /x( 1)^( n -1.0)) /n |
|||
wend |
|||
NthRoot =x( 1) |
|||
end function |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First estimate is: 1.071559602191682 ... and better is: 1.071547591944771 |
|||
125'th root of 5642 by LB's exponentiation operator is 1.071547591944767 |
|||
27^(1 / 3) 3.000000000000002 |
|||
2^(1 / 2) 1.414213562374690 |
|||
1024^(1 /10) 2.000000000000000 |
|||
</pre> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">#Def_p=0.001 |
|||
Procedure.d Nth_root(n.i, A.d, p.d=#Def_p) |
|||
Protected Dim x.d(1) |
|||
x(0)=A: x(1)=A/n |
|||
While Abs(x(1)-x(0))>p |
|||
x(0)=x(1) |
|||
x(1)=((n-1.0)*x(1)+A/Pow(x(1),n-1.0))/n |
|||
Wend |
|||
ProcedureReturn x(1) |
|||
EndProcedure |
|||
;////////////////////////////// |
|||
Debug "125'th root of 5642 is" |
|||
Debug Pow(5642,1/125) |
|||
Debug "First estimate is:" |
|||
Debug Nth_root(125,5642) |
|||
Debug "And better:" |
|||
Debug Nth_root(125,5642,0.00001)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
125'th root of 5642 is |
|||
1.0715475919447675 |
|||
First estimate is: |
|||
1.0715596021916822 |
|||
And better: |
|||
1.0715475919447714 |
|||
</pre> |
|||
==={{header|Run BASIC}}=== |
|||
<syntaxhighlight lang="runbasic">print "Root 125th Root of 5643 Precision .001 ";using( "#.###############", NthRoot( 125, 5642, 0.001 )) |
|||
print "125th Root of 5643 Precision .001 ";using( "#.###############", NthRoot( 125, 5642, 0.001 )) |
|||
print "125th Root of 5643 Precision .00001 ";using( "#.###############", NthRoot( 125, 5642, 0.00001)) |
|||
print " 3rd Root of 27 Precision .00001 ";using( "#.###############", NthRoot( 3, 27, 0.00001)) |
|||
print " 2nd Root of 2 Precision .00001 ";using( "#.###############", NthRoot( 2, 2, 0.00001)) |
|||
print " 10th Root of 1024 Precision .00001 ";using( "#.###############", NthRoot( 10, 1024, 0.00001)) |
|||
wait |
|||
function NthRoot( root, A, precision) |
|||
x0 = A |
|||
x1 = A /root |
|||
while abs( x1 -x0) >precision |
|||
x0 = x1 |
|||
x1 = x1 / 1.0 ' force float |
|||
x1 = (( root -1.0) *x1 +A /x1^( root -1.0)) /root |
|||
wend |
|||
NthRoot =x1 |
|||
end function |
|||
end</syntaxhighlight> |
|||
<pre>125th Root of 5643 Precision .001 1.071559602456735 |
|||
125th Root of 5643 Precision .00001 1.071547591944771 |
|||
3rd Root of 27 Precision .00001 3.000000000000001 |
|||
2nd Root of 2 Precision .00001 1.414213562374690 |
|||
10th Root of 1024 Precision .00001 2.000000000000000</pre> |
|||
==={{header|S-BASIC}}=== |
|||
When single precision results are sufficient for the task at hand, resort to Newton's method |
|||
seems unnecessarily cumbersome, given the ready availability of S-BASIC's built-in exp and |
|||
natural log functions. |
|||
<syntaxhighlight lang="basic"> |
|||
rem - return nth root of x |
|||
function nthroot(x, n = real) = real |
|||
end = exp((1.0 / n) * log(x)) |
|||
rem - exercise the routine by finding successive roots of 144 |
|||
var i = integer |
|||
print "Finding the nth root of x" |
|||
print " x n root" |
|||
print "-----------------------" |
|||
for i = 1 to 8 |
|||
print using "### #### ###.####"; 144; i; nthroot(144, i) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
But if the six or seven digits supported by S-BASIC's single-precision REAL data type is insufficient, Newton's Method is the way to go, given that the built-in exp and natural log functions are only single-precision. |
|||
<syntaxhighlight lang="basic"> |
|||
rem - return the nth root of real.double value x to stated precision |
|||
function nthroot(n, x, precision = real.double) = real.double |
|||
var x0, x1 = real.double |
|||
x0 = x |
|||
x1 = x / n rem - initial guess |
|||
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 |
|||
print "Finding the nth root of 144 to 6 decimal places" |
|||
print " x n root" |
|||
print "------------------------" |
|||
for i = 1 to 8 |
|||
print using "### #### ###.######"; 144; i; nthroot(i, 144.0, 1E-7) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
From the second version of the program. |
|||
<pre> |
|||
Finding the nth root of 144 to 6 decimal places |
|||
x n root |
|||
------------------------- |
|||
144 1 144.000000 |
|||
144 2 12.000000 |
|||
144 3 5.241483 |
|||
144 4 3.464102 |
|||
144 5 2.701920 |
|||
144 6 2.289428 |
|||
144 7 2.033937 |
|||
144 8 1.861210 |
|||
</pre> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">FUNCTION Nroot (n, a) |
|||
LET precision = .00001 |
|||
LET x1 = a |
|||
LET x2 = a / n |
|||
DO WHILE ABS(x2 - x1) > precision |
|||
LET x1 = x2 |
|||
LET x2 = ((n - 1) * x2 + a / x2 ^ (n - 1)) / n |
|||
LOOP |
|||
LET Nroot = x2 |
|||
END FUNCTION |
|||
PRINT " n 5643 ^ 1 / n nth_root ^ n" |
|||
PRINT " ------------------------------------" |
|||
FOR n = 3 TO 11 STEP 2 |
|||
LET tmp = Nroot(n, 5643) |
|||
PRINT USING "####": n; |
|||
PRINT " "; |
|||
PRINT USING "###.########": tmp; |
|||
PRINT " "; |
|||
PRINT USING "####.########": (tmp ^ n) |
|||
NEXT n |
|||
PRINT |
|||
FOR n = 25 TO 125 STEP 25 |
|||
LET tmp = Nroot(n, 5643) |
|||
PRINT USING "####": n; |
|||
PRINT " "; |
|||
PRINT USING "###.########": tmp; |
|||
PRINT " "; |
|||
PRINT USING "####.########": (tmp ^ n) |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|AWK}} |
|||
<syntaxhighlight lang="yabasic">data 10, 1024, 3, 27, 2, 2, 125, 5642, 4, 16, 0, 0 |
|||
do |
|||
read e, b |
|||
if e = 0 break |
|||
print "The ", e, "th root of ", b, " is ", b^(1/e), " (", nthroot(b, e), ")" |
|||
loop |
|||
sub nthroot(y, n) |
|||
local eps, x, d, e |
|||
eps = 1e-15 // relative accuracy |
|||
x = 1 |
|||
repeat |
|||
d = ( y / ( x^(n-1) ) - x ) / n |
|||
x = x + d |
|||
e = eps * x // absolute accuracy |
|||
until(not(d < -e or d > e )) |
|||
return x |
|||
end sub</syntaxhighlight> |
|||
==={{header|VBA}}=== |
|||
{{trans|Phix}} |
|||
The internal power operator "^" is used in stead of an auxiliary pow_ function and the accuracy has been reduced. |
|||
<syntaxhighlight lang="vb">Private Function nth_root(y As Double, n As Double) |
|||
Dim eps As Double: eps = 0.00000000000001 '-- relative accuracy |
|||
Dim x As Variant: x = 1 |
|||
Do While True |
|||
d = (y / x ^ (n - 1) - x) / n |
|||
x = x + d |
|||
e = eps * x '-- absolute accuracy |
|||
If d > -e And d < e Then |
|||
Exit Do |
|||
End If |
|||
Loop |
|||
Debug.Print y; n; x; y ^ (1 / n) |
|||
End Function |
|||
Public Sub main() |
|||
nth_root 1024, 10 |
|||
nth_root 27, 3 |
|||
nth_root 2, 2 |
|||
nth_root 5642, 125 |
|||
nth_root 7, 0.5 |
|||
nth_root 4913, 3 |
|||
nth_root 8, 3 |
|||
nth_root 16, 2 |
|||
nth_root 16, 4 |
|||
nth_root 125, 3 |
|||
nth_root 1000000000, 3 |
|||
nth_root 1000000000, 9 |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre> 1024 10 2 2 |
|||
27 3 3 3 |
|||
2 2 1,41421356237309 1,4142135623731 |
|||
5642 125 1,07154759194477 1,07154759194477 |
|||
7 0,5 49 49 |
|||
4913 3 17 17 |
|||
8 3 2 2 |
|||
16 2 4 4 |
|||
16 4 2 2 |
|||
125 3 5 5 |
|||
1000000000 3 1000 1000 |
|||
1000000000 9 10 10 |
|||
</pre> |
</pre> |
||
Line 1,395: | Line 1,746: | ||
end program NthRootTest</syntaxhighlight> |
end program NthRootTest</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">' version 14-01-2019 |
|||
' compile with: fbc -s console |
|||
Function nth_root(n As Integer, number As Double) As Double |
|||
Dim As Double a1 = number / n, a2 , a3 |
|||
Do |
|||
a3 = Abs(a2 - a1) |
|||
a2 = ((n -1) * a1 + number / a1 ^ (n -1)) / n |
|||
Swap a1, a2 |
|||
Loop Until Abs(a2 - a1) = a3 |
|||
Return a1 |
|||
End Function |
|||
' ------=< MAIN >=------ |
|||
Dim As UInteger n |
|||
Dim As Double tmp |
|||
Print |
|||
Print " n 5643 ^ 1 / n nth_root ^ n" |
|||
Print " ------------------------------------" |
|||
For n = 3 To 11 Step 2 |
|||
tmp = nth_root(n, 5643) |
|||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n |
|||
Next |
|||
Print |
|||
For n = 25 To 125 Step 25 |
|||
tmp = nth_root(n, 5643) |
|||
Print Using " ### ###.######## ####.########"; n; tmp; tmp ^ n |
|||
Next |
|||
' empty keyboard buffer |
|||
While Inkey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre> n 5643 ^ 1 / n nth_root ^ n |
|||
------------------------------------ |
|||
3 17.80341642 5643.00000000 |
|||
5 5.62732516 5643.00000000 |
|||
7 3.43502583 5643.00000000 |
|||
9 2.61116581 5643.00000000 |
|||
11 2.19303907 5643.00000000 |
|||
25 1.41273402 5643.00000000 |
|||
50 1.18858488 5643.00000000 |
|||
75 1.12207047 5643.00000000 |
|||
100 1.09022240 5643.00000000 |
|||
125 1.07154911 5643.00000000</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic">window 1 |
|||
local fn NthRoot( root as long, a as long, precision as double ) as double |
|||
double x0, x1 |
|||
x0 = a : x1 = a /root |
|||
while ( abs( x1 - x0 ) > precision ) |
|||
x0 = x1 |
|||
x1 = ( ( root -1.0 ) * x1 + a / x1 ^ ( root -1.0 ) ) /root |
|||
wend |
|||
end fn = x1 |
|||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 ) |
|||
print " 125th Root of 5643 Precision .001",, using "#.###############"; fn NthRoot( 125, 5642, 0.001 ) |
|||
print " 125th Root of 5643 Precision .00001", using "#.###############"; fn NthRoot( 125, 5642, 0.00001 ) |
|||
print " Cube Root of 27 Precision .00001", using "#.###############"; fn NthRoot( 3, 27, 0.00001 ) |
|||
print "Square Root of 2 Precision .00001", using "#.###############"; fn NthRoot( 2, 2, 0.00001 ) |
|||
print "Square Root of 2 Precision .00001", using "#.###############"; sqr(2) // Processor floating point calc deviation |
|||
print " 10th Root of 1024 Precision .00001", using "#.###############"; fn NthRoot( 10, 1024, 0.00001 ) |
|||
print " 5th Root of 34 Precision .00001", using "#.###############"; fn NthRoot( 5, 34, 0.00001 ) |
|||
HandleEvents</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
125th Root of 5643 Precision .001 1.071559602191682 |
|||
125th Root of 5643 Precision .001 1.071559602191682 |
|||
125th Root of 5643 Precision .00001 1.071547591944772 |
|||
Cube Root of 27 Precision .00001 3.000000000000002 |
|||
Square Root of 2 Precision .00001 1.414213562374690 |
|||
Square Root of 2 Precision .00001 1.414213562373095 |
|||
10th Root of 1024 Precision .00001 2.000000000000000 |
|||
5th Root of 34 Precision .00001 2.024397458499885 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,978: | Line 2,237: | ||
2 |
2 |
||
</pre> |
</pre> |
||
=={{header|Liberty BASIC}}== |
|||
<syntaxhighlight lang="lb"> |
|||
print "First estimate is: ", using( "#.###############", NthRoot( 125, 5642, 0.001 )); |
|||
print " ... and better is: ", using( "#.###############", NthRoot( 125, 5642, 0.00001)) |
|||
print "125'th root of 5642 by LB's exponentiation operator is "; using( "#.###############", 5642^(1 /125)) |
|||
print "27^(1 / 3)", using( "#.###############", NthRoot( 3, 27, 0.00001)) |
|||
print "2^(1 / 2)", using( "#.###############", NthRoot( 2, 2, 0.00001)) |
|||
print "1024^(1 /10)", using( "#.###############", NthRoot( 10, 1024, 0.00001)) |
|||
wait |
|||
function NthRoot( n, A, p) |
|||
x( 0) =A |
|||
x( 1) =A /n |
|||
while abs( x( 1) -x( 0)) >p |
|||
x( 0) =x( 1) |
|||
x( 1) =( ( n -1.0) *x( 1) +A /x( 1)^( n -1.0)) /n |
|||
wend |
|||
NthRoot =x( 1) |
|||
end function |
|||
end |
|||
</syntaxhighlight> |
|||
First estimate is: 1.071559602191682 ... and better is: 1.071547591944771 |
|||
125'th root of 5642 by LB's exponentiation operator is 1.071547591944767 |
|||
27^(1 / 3) 3.000000000000002 |
|||
2^(1 / 2) 1.414213562374690 |
|||
1024^(1 /10) 2.000000000000000 |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Line 2,708: | Line 2,936: | ||
X = -3. |
X = -3. |
||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">#Def_p=0.001 |
|||
Procedure.d Nth_root(n.i, A.d, p.d=#Def_p) |
|||
Protected Dim x.d(1) |
|||
x(0)=A: x(1)=A/n |
|||
While Abs(x(1)-x(0))>p |
|||
x(0)=x(1) |
|||
x(1)=((n-1.0)*x(1)+A/Pow(x(1),n-1.0))/n |
|||
Wend |
|||
ProcedureReturn x(1) |
|||
EndProcedure |
|||
;////////////////////////////// |
|||
Debug "125'th root of 5642 is" |
|||
Debug Pow(5642,1/125) |
|||
Debug "First estimate is:" |
|||
Debug Nth_root(125,5642) |
|||
Debug "And better:" |
|||
Debug Nth_root(125,5642,0.00001)</syntaxhighlight> |
|||
'''Outputs |
|||
125'th root of 5642 is |
|||
1.0715475919447675 |
|||
First estimate is: |
|||
1.0715596021916822 |
|||
And better: |
|||
1.0715475919447714 |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 3,099: | Line 3,299: | ||
p nthroot(5,34) # => 2.02439745849989</syntaxhighlight> |
p nthroot(5,34) # => 2.02439745849989</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
|||
<syntaxhighlight lang="runbasic">print "Root 125th Root of 5643 Precision .001 ";using( "#.###############", NthRoot( 125, 5642, 0.001 )) |
|||
print "125th Root of 5643 Precision .001 ";using( "#.###############", NthRoot( 125, 5642, 0.001 )) |
|||
print "125th Root of 5643 Precision .00001 ";using( "#.###############", NthRoot( 125, 5642, 0.00001)) |
|||
print " 3rd Root of 27 Precision .00001 ";using( "#.###############", NthRoot( 3, 27, 0.00001)) |
|||
print " 2nd Root of 2 Precision .00001 ";using( "#.###############", NthRoot( 2, 2, 0.00001)) |
|||
print " 10th Root of 1024 Precision .00001 ";using( "#.###############", NthRoot( 10, 1024, 0.00001)) |
|||
wait |
|||
function NthRoot( root, A, precision) |
|||
x0 = A |
|||
x1 = A /root |
|||
while abs( x1 -x0) >precision |
|||
x0 = x1 |
|||
x1 = x1 / 1.0 ' force float |
|||
x1 = (( root -1.0) *x1 +A /x1^( root -1.0)) /root |
|||
wend |
|||
NthRoot =x1 |
|||
end function |
|||
end</syntaxhighlight> |
|||
<pre>125th Root of 5643 Precision .001 1.071559602456735 |
|||
125th Root of 5643 Precision .00001 1.071547591944771 |
|||
3rd Root of 27 Precision .00001 3.000000000000001 |
|||
2nd Root of 2 Precision .00001 1.414213562374690 |
|||
10th Root of 1024 Precision .00001 2.000000000000000</pre> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 3,173: | Line 3,345: | ||
end; |
end; |
||
end;</syntaxhighlight> |
end;</syntaxhighlight> |
||
=={{header|S-BASIC}}== |
|||
When single precision results are sufficient for the task at hand, resort to Newton's method |
|||
seems unnecessarily cumbersome, given the ready availability of S-BASIC's built-in exp and |
|||
natural log functions. |
|||
<syntaxhighlight lang="basic"> |
|||
rem - return nth root of x |
|||
function nthroot(x, n = real) = real |
|||
end = exp((1.0 / n) * log(x)) |
|||
rem - exercise the routine by finding successive roots of 144 |
|||
var i = integer |
|||
print "Finding the nth root of x" |
|||
print " x n root" |
|||
print "-----------------------" |
|||
for i = 1 to 8 |
|||
print using "### #### ###.####"; 144; i; nthroot(144, i) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
But if the six or seven digits supported by S-BASIC's single-precision REAL data type is insufficient, Newton's Method is the way to go, given that the built-in exp and natural log functions are only single-precision. |
|||
<syntaxhighlight lang="basic"> |
|||
rem - return the nth root of real.double value x to stated precision |
|||
function nthroot(n, x, precision = real.double) = real.double |
|||
var x0, x1 = real.double |
|||
x0 = x |
|||
x1 = x / n rem - initial guess |
|||
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 |
|||
print "Finding the nth root of 144 to 6 decimal places" |
|||
print " x n root" |
|||
print "------------------------" |
|||
for i = 1 to 8 |
|||
print using "### #### ###.######"; 144; i; nthroot(i, 144.0, 1E-7) |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
From the second version of the program. |
|||
<pre> |
|||
Finding the nth root of 144 to 6 decimal places |
|||
x n root |
|||
------------------------- |
|||
144 1 144.000000 |
|||
144 2 12.000000 |
|||
144 3 5.241483 |
|||
144 4 3.464102 |
|||
144 5 2.701920 |
|||
144 6 2.289428 |
|||
144 7 2.033937 |
|||
144 8 1.861210 |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 3,438: | Line 3,547: | ||
7131.5 |
7131.5 |
||
49.0</pre> |
49.0</pre> |
||
=={{header|True BASIC}}== |
|||
<syntaxhighlight lang="qbasic">FUNCTION Nroot (n, a) |
|||
LET precision = .00001 |
|||
LET x1 = a |
|||
LET x2 = a / n |
|||
DO WHILE ABS(x2 - x1) > precision |
|||
LET x1 = x2 |
|||
LET x2 = ((n - 1) * x2 + a / x2 ^ (n - 1)) / n |
|||
LOOP |
|||
LET Nroot = x2 |
|||
END FUNCTION |
|||
PRINT " n 5643 ^ 1 / n nth_root ^ n" |
|||
PRINT " ------------------------------------" |
|||
FOR n = 3 TO 11 STEP 2 |
|||
LET tmp = Nroot(n, 5643) |
|||
PRINT USING "####": n; |
|||
PRINT " "; |
|||
PRINT USING "###.########": tmp; |
|||
PRINT " "; |
|||
PRINT USING "####.########": (tmp ^ n) |
|||
NEXT n |
|||
PRINT |
|||
FOR n = 25 TO 125 STEP 25 |
|||
LET tmp = Nroot(n, 5643) |
|||
PRINT USING "####": n; |
|||
PRINT " "; |
|||
PRINT USING "###.########": tmp; |
|||
PRINT " "; |
|||
PRINT USING "####.########": (tmp ^ n) |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 3,505: | Line 3,580: | ||
3.400000e+01, |
3.400000e+01, |
||
7.131500e+03> |
7.131500e+03> |
||
</pre> |
|||
=={{header|VBA}}== |
|||
{{trans|Phix}} |
|||
The internal power operator "^" is used in stead of an auxiliary pow_ function and the accuracy has been reduced. |
|||
<syntaxhighlight lang="vb">Private Function nth_root(y As Double, n As Double) |
|||
Dim eps As Double: eps = 0.00000000000001 '-- relative accuracy |
|||
Dim x As Variant: x = 1 |
|||
Do While True |
|||
d = (y / x ^ (n - 1) - x) / n |
|||
x = x + d |
|||
e = eps * x '-- absolute accuracy |
|||
If d > -e And d < e Then |
|||
Exit Do |
|||
End If |
|||
Loop |
|||
Debug.Print y; n; x; y ^ (1 / n) |
|||
End Function |
|||
Public Sub main() |
|||
nth_root 1024, 10 |
|||
nth_root 27, 3 |
|||
nth_root 2, 2 |
|||
nth_root 5642, 125 |
|||
nth_root 7, 0.5 |
|||
nth_root 4913, 3 |
|||
nth_root 8, 3 |
|||
nth_root 16, 2 |
|||
nth_root 16, 4 |
|||
nth_root 125, 3 |
|||
nth_root 1000000000, 3 |
|||
nth_root 1000000000, 9 |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre> 1024 10 2 2 |
|||
27 3 3 3 |
|||
2 2 1,41421356237309 1,4142135623731 |
|||
5642 125 1,07154759194477 1,07154759194477 |
|||
7 0,5 49 49 |
|||
4913 3 17 17 |
|||
8 3 2 2 |
|||
16 2 4 4 |
|||
16 4 2 2 |
|||
125 3 5 5 |
|||
1000000000 3 1000 1000 |
|||
1000000000 9 10 10 |
|||
</pre> |
</pre> |
||
Line 3,619: | Line 3,650: | ||
2.000000000000000 |
2.000000000000000 |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
{{trans|AWK}} |
|||
<syntaxhighlight lang="yabasic">data 10, 1024, 3, 27, 2, 2, 125, 5642, 4, 16, 0, 0 |
|||
do |
|||
read e, b |
|||
if e = 0 break |
|||
print "The ", e, "th root of ", b, " is ", b^(1/e), " (", nthroot(b, e), ")" |
|||
loop |
|||
sub nthroot(y, n) |
|||
local eps, x, d, e |
|||
eps = 1e-15 // relative accuracy |
|||
x = 1 |
|||
repeat |
|||
d = ( y / ( x^(n-1) ) - x ) / n |
|||
x = x + d |
|||
e = eps * x // absolute accuracy |
|||
until(not(d < -e or d > e )) |
|||
return x |
|||
end sub</syntaxhighlight> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |