Given a number ‘n’, we need to find the nth number whose each digit is a prime number i.e 2, 3, 5, 7….In other words you have to find nth number of this sequence. 2, 3, 5, 5, 22, 23……
Given that the nth number such found will be less then equal to 10^18
Input : 10 Output : 33 2, 3, 5, 7, 22, 23, 25, 27, 32, 33 Input : 21 Output : 222
There are four prime digits 2, 3, 5 and 7. First observation is that the number of numbers of x length and made of prime digits are because for each position you have 4 choices so total number is 4^x.
So total count of such numbers whose length is = 1en) will be 4*((4len – 1)/3). (This is sum of G.P with first term 4 and common ratio 4)
The algorithm is mainly divided in two steps.
- We find the number of digits in n-th number using above observation. We start from len = 0 and keep incrementing it while value of it is smaller than 4*((4len – 1)/3).
- Now we know number of digits in n-th number. We also know the count of numbers with with (len-1) digits. Let this count be ‘prev_count’. Now we one by one find digits in our result. First fix 2 at i-th place (assuming all the places upto 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 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 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 check 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 nth such number is len only.
Below is implementation of above steps.
Alternate Solution (Works in O(Log n)
In this post, a O(log n) solution is discussed which is based on below pattern in numbers. The numbers can be seen "" / | | \ 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. 3nd. 7th, 11th. 15th, ..... numbers have 5 as last digit. 4th. 8th, 12th. 16th, ..... numbers have 7 as last digit.
# Python3 program to find n-th number
# with prime digits 2, 3 and 7
num = “”;
while (number > 0):
# remainder for check element position
rem = number % 4;
# if number is 1st position in tree
if (rem == 1):
num += ‘2’;
# if number is 2nd position in tree
if (rem == 2):
num += ‘3’;
# if number is 3rd position in tree
if (rem == 3):
num += ‘5’;
# if number is 4th position in tree
if (rem == 0):
num += ‘7’;
number = number // 4;
# Driver code
number = 21;
# This code is contributed by mits
This article is contributed by Ayush Jha and Devanshu agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Nth number made up of odd digits only
- Find the n-th number made of even digits only
- Finding power of prime number p in n!
- Finding sum of digits of a number until sum becomes single digit
- Number of digits in the nth number made of given four digits
- Finding number of digits in n'th Fibonacci number
- Recursive sum of digits of a number is prime or not
- Largest number with prime digits
- Largest number not greater than N which can become prime after rearranging its digits
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Print prime numbers with prime sum of digits in an array
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Check if elements of array can be made equal by multiplying given prime numbers
- Print the nearest prime number formed by adding prime numbers to N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number