Calculate Root Mean Kth power of all array elements | 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 Integer . MAX_VALUE ; 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
function nthRoot ( A , N ) { var xPre = ( Math . random ( ) * 10 ) % 10 ; var eps = 1e-3 ; var delX = Number . MAX_VALUE ; var xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + A / Math . pow ( xPre , N - 1 ) ) / N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } function RMNValue ( arr , n , k ) { var Nth = 0 ; var mean = 0 , root = 0 ; for ( var i = 0 ; i < n ; i ++ ) { Nth += Math . pow ( arr [ i ] , k ) ; } mean = ( Nth / ( n ) ) ; root = nthRoot ( mean , k ) ; return root ; } var arr = [ 10 , 4 , 6 , 8 ] ; var N = arr . length ; var K = 3 ; document . write ( RMNValue ( arr , N , K ) ) ;