Jump to content

Nth root: Difference between revisions

Added solution for Action!
(add BQN)
(Added solution for Action!)
Line 212:
Root= +2.000000000000000
Root= +1.414213562373095
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
 
PROC NthRoot(REAL POINTER a,n REAL POINTER res)
REAL n1,eps,one,tmp1,tmp2,tmp3
ValR("0.0001",eps)
IntToReal(1,one)
RealSub(n,one,n1)
 
Sqrt(a,res) ;res=sqrt(a)
DO
Power(res,n,tmp1) ;tmp=res^n
RealSub(a,tmp1,tmp2) ;tmp2=a-res^n
RealAbs(tmp2,tmp1) ;tmp1=abs(a-res^n)
IF RealGreaterOrEqual(eps,tmp1) THEN
RETURN
FI
 
Power(res,n1,tmp1) ;tmp1=res^(n-1)
RealDiv(a,tmp1,tmp2) ;tmp2=a/(res^(n-1))
RealMult(n1,res,tmp1) ;tmp1=(n-1)*res
RealAdd(tmp1,tmp2,tmp3) ;tmp3=((n-1)*res + a/(res^(n-1)))
RealDiv(tmp3,n,res) ;res=((n-1)*res + a/(res^(n-1)))/n
OD
RETURN
 
PROC Test(CHAR ARRAY sa,sn)
REAL a,n,res
 
ValR(sa,a)
ValR(sn,n)
PrintR(n) Print(" root of ")
PrintR(a) Print(" is ")
NthRoot(a,n,res)
PrintRE(res)
RETURN
 
PROC Main()
Put(125) PutE() ;clear screen
MathInit()
Test("2","2")
Test("81","4")
Test("1024","10")
Test("7","0.5")
Test("12.34","56.78")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nth_root.png Screenshot from Atari 8-bit computer]
<pre>
2 root of 2 is 1.41421355
4 root of 81 is 3.00000047
10 root of 1024 is 2.00000001
.5 root of 7 is 48.99975418
56.78 root of 12.34 is 1.04524972
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.