Task

Implement the algorithm to compute the principal   nth   root   





A

n





{\displaystyle {\sqrt[{n}]{A}}}

   of a positive real number   A,   as explained at the   Wikipedia page.



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