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 print the ith digit and break ; Driver Code
< ? php function nthprimedigitsnumber ( $ n ) { $ len = 1 ; $ prev_count = 0 ; while ( true ) { $ curr_count = $ prev_count + pow ( 4 , $ len ) ; if ( $ prev_count < $ n && $ curr_count >= $ n ) break ; $ len ++ ; $ prev_count = $ curr_count ; } for ( $ i = 1 ; $ i <= $ len ; $ i ++ ) { for ( $ j = 1 ; $ j <= 4 ; $ j ++ ) { if ( $ prev_count + pow ( 4 , $ len - $ i ) < $ n ) $ prev_count += pow ( 4 , $ len - $ i ) ; else { if ( $ j == 1 ) echo "2" ; else if ( $ j == 2 ) echo "3" ; else if ( $ j == 3 ) echo "5" ; else if ( $ j == 4 ) echo "7" ; break ; } } } echo " STRNEWLINE " ; } nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; ? >