Count perfect power of K in a range [ L , R ] | Function to find the Nth root of the number ; initially guessing a random number between 0 to 9 ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by INT_MAX ; xK denotes current value of x ; loop until we reach desired accuracy ; calculating current value from previous value ; Function to count the perfect powers of K in range L to R ; Driver code
function nthRoot ( A , N ) { var xPre = Math . random ( ) % 10 ; var eps = 0.001 ; var delX = 1000000000 ; var xK ; 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 countPowers ( a , b , k ) { return ( Math . floor ( nthRoot ( b , k ) ) - Math . ceil ( nthRoot ( a , k ) ) + 1 ) ; } var a = 7 , b = 28 , k = 2 ; document . write ( " " + countPowers ( a , b , k ) ) ;