MCPcopy Create free account
hub / github.com/ActiveState/code / nthRoot

Function nthRoot

recipes/Python/576857_nthRoot/recipe-576857.py:15–36  ·  view source on GitHub ↗

Returns the nth root of a number

(n,x)

Source from the content-addressed store, hash-verified

13from decimal import *
14
15def nthRoot(n,x):
16 """ Returns the nth root of a number """
17
18 seq_start = 1.0 #sequence starting value
19 counter = 0 #initialize the generator counter to zero
20
21 if x < 0:
22 raise ValueError,\
23 " Cannot compute a Square root on a negative number"
24 elif n == 0:
25 raise ValueError,\
26 " Cannot compute 0 root of a number"
27
28 while counter < 700:
29
30 yield seq_start #return nthRoot(x)
31
32 #compute the next sequence term (Xn+1)
33 seq_start = 1/float(n) * ((n-1)*float(seq_start )+ x/(float(seq_start)**(n-1)))
34
35
36 counter += 1
37
38while True:
39

Callers 1

recipe-576857.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected