Calculate Root Mean Kth power of all array elements | Python3 program for the above approach ; Function to find the Nth root ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by INT_MAX ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function Call
import sys NEW_LINE import random NEW_LINE def nthRoot ( A , N ) : NEW_LINE INDENT xPre = random . random ( ) % 10 NEW_LINE eps = 1e-3 NEW_LINE delX = sys . maxsize NEW_LINE xK = 0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N ) NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK NEW_LINE DEDENT return xK NEW_LINE DEDENT def RMNValue ( arr , n , k ) : NEW_LINE INDENT Nth = 0 NEW_LINE mean = 0.0 NEW_LINE root = 0.0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Nth += pow ( arr [ i ] , k ) NEW_LINE DEDENT mean = ( Nth // ( n ) ) NEW_LINE root = nthRoot ( mean , k ) NEW_LINE return root NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 4 , 6 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( RMNValue ( arr , N , K ) ) NEW_LINE DEDENT