Nth root of M Python

PROGRAM TO FIND Nth ROOT OF M


import math
import random
  
# method returns Nth power of A
def nthRoot(A,N):
  
    # initially guessing a random number between
    # 0 and 9
    xPre = random.randint(1,101) % 10
   
    #  smaller eps, denotes more accuracy
    eps = 0.001
   
    # initializing difference between two
    # roots by INT_MAX
    delX = 2147483647
   
    #  xK denotes current value of x
    xK=0.0
   
    #  loop untill we reach desired accuracy
    while (delX > eps):
  
        # calculating current value from previous
        # value by newton's method
        xK = ((N - 1.0) * xPre +
              A/pow(xPre, N-1)) /N
        delX = abs(xK - xPre)
        xPre = xK;
          
    return xK
  
# Driver code
N = 4
A = 81
nthRootValue = nthRoot(A, N)
print("Nth root is ", nthRootValue)


OUTPUT
Nth root is 3

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java