Nth root of M Python
- Get link
- X
- Other Apps
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 = 4A = 81nthRootValue = nthRoot(A, N) print("Nth root is ", nthRootValue) OUTPUT
Nth root is 3
- Get link
- X
- Other Apps
Comments
Post a Comment