Finding n | C ++ implementation for finding nth number made of prime digits only ; 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 function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nthprimedigitsnumber ( long long n ) { long long len = 1 ; long long prev_count = 0 ; while ( true ) { long long curr_count = prev_count + pow ( 4 , len ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + pow ( 4 , len - i ) < n ) prev_count += pow ( 4 , len - i ) ; else { if ( j == 1 ) cout << "2" ; else if ( j == 2 ) cout << "3" ; else if ( j == 3 ) cout << "5" ; else if ( j == 4 ) cout << "7" ; break ; } } } cout << endl ; } int main ( ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; return 0 ; }