Finding n | Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else prvar the ith digit and break ; Driver method
function nthprimedigitsnumber ( n ) { var len = 1 ; var prev_count = 0 ; while ( true ) { var curr_count = ( prev_count + Math . pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( var i = 1 ; i <= len ; i ++ ) { for ( var j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + Math . pow ( 4 , len - i ) < n ) prev_count += Math . pow ( 4 , len - i ) ; else { if ( j == 1 ) document . write ( " " ) ; else if ( j == 2 ) document . write ( " " ) ; else if ( j == 3 ) document . write ( " " ) ; else if ( j == 4 ) document . write ( " " ) ; break ; } } } document . write ( ' ' ) ; } nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ;