Find the Nth Pure number | Function to check if it is a power of 2 or not ; if a number belongs to 4 series it should lie between 2 ^ blocks - 1 to 2 ^ blocks + 2 ^ ( blocks - 1 ) - 1 ; Method to find pure number ; Iterate from 1 to N ; Check if number is power of two ; Distance to previous block numbers ; Distance to previous block numbers ; Driver Code ; Function call to find the Nth pure number
function isPowerOfTwo ( N ) { let number = Math . log ( N ) / Math . log ( 2 ) ; let checker = Math . floor ( number ) ; return number - checker == 0 ; } function isSeriesFour ( N , digits ) { let upperBound = Math . floor ( Math . pow ( 2 , digits ) + Math . pow ( 2 , digits - 1 ) - 1 ) ; let lowerBound = Math . floor ( Math . pow ( 2 , digits ) - 1 ) ; return ( N >= lowerBound ) && ( N < upperBound ) ; } function getPureNumber ( N ) { let numbers = new Array ( N + 1 ) ; numbers [ 0 ] = " " ; let blocks = 0 ; let displacement = 0 ; for ( let i = 1 ; i < N + 1 ; i ++ ) { if ( isPowerOfTwo ( i + 1 ) ) { blocks = blocks + 1 ; } if ( isSeriesFour ( i , blocks ) ) { displacement = Math . floor ( Math . pow ( 2 , blocks - 1 ) ) ; numbers [ i ] = " " + numbers [ i - displacement ] + " " ; } else { displacement = Math . floor ( Math . pow ( 2 , blocks ) ) ; numbers [ i ] = " " + numbers [ i - displacement ] + " " ; } } return numbers [ N ] ; } let N = 5 ; let pure = getPureNumber ( N ) ; document . write ( pure + " " ) ;