Returns the nth root of a number
(n,x)
| 13 | from decimal import * |
| 14 | |
| 15 | def 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 | |
| 38 | while True: |
| 39 |