VOOZH about

URL: https://www.geeksforgeeks.org/dsa/finding-n-th-number-made-prime-digits/

⇱ Finding n-th number made of prime digits (2, 3, 5 and 7) only - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding n-th number made of prime digits (2, 3, 5 and 7) only

Last Updated : 23 Jul, 2025

Given a number 'n', find the nth number whose each digit is a prime number i.e 2, 3, 5, 7. . . In other words, find the nth number of this sequence. 2, 3, 5, 5, 22, 23...... 
Given that the nth number such found will be less than equal to 10^18 

Examples : 

Input: 10
Output: 3
Explanation: 2, 3, 5, 7, 22, 23, 25, 27, 32, 33

Input: 21
Output: 222

Finding the n-th number made of prime digits (2, 3, 5, and 7) using Mathematics:

There are four prime digits 2, 3, 5, and 7. The first observation is that the number of numbers of x length and made of prime digits are 4x because for each position you have 4 choices so the total number is 4^x. So the total count of such numbers whose length is = 1 to len (i.e. 2 or 3 or more) will be 4*((4len - 1)/3). (This is the sum of G.P with first term 4 and common ratio 4)

Follow the steps below to solve the problem:

  • First finds the number of digits in the n-th number using the above observation. Start from len = 0 and keep incrementing it while the value of it is smaller than 4*((4len - 1)/3).
  • Now we know the number of digits in the n-th number. We also know the count of numbers with (len-1) digits. Let this count be prev_count. Now one by one find digits in our result. 
  • First, fix 2 at the ith place (assuming all the places up to i-1 are already filled), we have 4(len - i) numbers possible and to check if 2 is the right candidate or not check if the count of numbers after putting 2 is greater than or equal to n or not. If it is true then 2 is the right candidate if this is not true this means if we fix 2 at the ith place only prev_count + 4(len-i) numbers can be covered. 
  • So increase prev_count by 4(len-i) and repeat this step for 3 checks if 3 fits at ith place or not. If not go for 5. If 5 also does not fit, go for 7. It is guaranteed that 7 will fit it if 2, 3, and 5 do not fit because we are sure that the length of the nth such number is len only.

Below is the implementation of the above steps: 


Output
33
222

Time Complexity: O(Constant), Length of digits in the worst case will be 18, to count numbers with len - 1 will take 18 operations and to calculate the nth it will take 18 * 4  = 72, so total operation will be 90 which is constant.
Auxiliary Space: O(1)

Approach 2:

The idea to make the nth number by identifying the below pattern:

      /                                 |                        |                            \
     2                                3                       5                            7
 / |  | \                         / | |  \                 /  | | \                    /  | |  \ 
22 23 25 27        32 33 35 37         52 53 55 57        72 73 75 77
/||\/||\/||\/||\       /||\/||\/||\/||\         /||\/||\/||\/||\        /||\/||\/||\/||\
We can notice following :
1st. 5th, 9th. 13th, ..... numbers have 2 as last digit.
2nd. 6th, 10th. 14th, ..... numbers have 3 as last digit.
3rd. 7th, 11th. 15th, ..... numbers have 5 as last digit.
4th. 8th, 12th. 16th, ..... numbers have 7 as last digit.

Follow the steps below to solve the problem:

  • First, calculate the remainder when the n is divided by 4 to identify which number is there at that place.
  • After identifying the number add that number to the answer.
  • At last reduce the nth number by dividing it by 4 to calculate the rest of the numbers.
  • Repeat above steps till n becomes zero.

Below is the implementation of above approach:


Output
33
222

Time Complexity: O(log4(N)), Looping till the Nth number becomes zero which is reducing by 4 every time.
Auxiliary Space: O(1)

Comment
Article Tags: