Nth root: Difference between revisions

Content deleted Content added
PureFox (talk | contribs)
m →‎{{header|Wren}}: Changed to Wren S/H
J7M (talk | contribs)
Add SmallBASIC
 
(3 intermediate revisions by one other user not shown)
Line 1,038:
-------------------------
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|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
func nthroot(a, n)
precision = .00001
x1 = a
x2 = a / n
repeat
x1 = x2
.xx2 = [.x[2], ((.n - 1) x* .x[2]x2 + .Aa / (.x[2]x2 ^ (.n - 1))) / .n]
until (abs(x2 - x1) < precision)
return x2
end
 
print "Finding the nth root of 144"
print " x n nthroot"
print "------------------------"
for n = 2 to 8
print using "### #### ###.000000"; 144; n; nthroot(144, n)
next
</syntaxhighlight>
{{out}}
<pre>
Finding the nth root of 144
x n nthroot
-------------------------
144 2 12.000000
144 3 5.241483
Line 2,369 ⟶ 2,403:
Langur has a root operator. Here, we show use of both the root operator and an nth root function.
 
{{works with|langur|0.8}}
{{trans|D}}
<syntaxhighlight lang="langur">writeln "operator"
writeln( (7131.5 ^ 10) ^/ 10 )"operator"
writeln .nthroot(10, 7131.5 ^ 10,) 0.001)^/ 10
writeln 64 ^/ 6
writeln()
 
val .nthroot = ffn(.n, .A, .p) {
# To make the example from the D language work, we set a low maximum for the number of digits after a decimal point in division.
var .x = [.A, .A / .n]
mode divMaxScale = 7
while abs(.x[2]-.x[1]) > .p {
 
x = [x[2], ((n-1) * x[2] + A / (x[2] ^ (n-1))) / n]
val .nthroot = f(.n, .A, .p) {
var .x = [.A, .A / .n]
while abs(.x[2]-.x[1]) > .p {
.x = [.x[2], ((.n-1) x .x[2] + .A / (.x[2] ^ (.n-1))) / .n]
}
simplify .x[2]
}
 
# To make the example from the D language work, we set a low maximum for the number of digits after a decimal point in division.
writeln "calculation"
mode divMaxScale = 7
writeln .nthroot(10, 7131.5 ^ 10, 0.001)
 
writeln .nthroot(6, 64, 0.001)</syntaxhighlight>
writeln "calculationfunction"
writeln nthroot(10, 7131.5 ^ 10, 0.001)
writeln .nthroot(6, 64, 0.001)</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,396 ⟶ 2,431:
2
 
function
calculation
7131.5
2