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.



def nthroot
	var n var y
	1e-15 var eps		/# relative accuracy #/
	1 var x
	true
	while
		y x n 1 - power / x - n / var d
		x d + var x
		eps x * var e	/# absolute accuracy #/
		d 0 e - < d e > or
	endwhile
	x
enddef
 
def printList
	len for get print endfor
enddef
 
10 1024 3 27 2 2 125 5642 4 16 stklen tolist
 
len 1 swap 2 3 tolist
for
	var i
	i get swap i 1 + get rot var e var b
	"The " e "th root of " b " is "	b 1 e / power " (" b e nthroot ")" 9 tolist
	printList drop nl
endfor