- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

In this problem, we are given a number N. Our task is to *finding n-th number made of prime digits (2, 3, 5 and 7) only*.

The series consisting of prime digits only (2, 3, 5, 7) is, 2, 3, 5, 7, 22, 23, 25, 27, 32, 33...

**Let's take an example to understand the problem,**

Input: N = 6 Output: 23

## Solution Approach

A simple approach to solving the problem is by finding the number at the given index i.e. by finding the term of the series, for this we will be observing the series.

We have four different prime numbers so the series made can be treated as a four-digit number system. In this number system, we have 4x numbers of length x.

Now to solve the problem, we have the series in which we will find the length o numbers that made them create the number. Then we will count the Nth number and print the required number.

To find the Nth number using the length we will be counting from the first number of length (x-1) and then count this N.

## Example

Program to illustrate the working of our solution

#include <iostream> #include <math.h> using namespace std; void findNthNumber(int n){ long x = 1; long lastNum = 0; while (true) { long currNum = lastNum + pow(4, x); if (lastNum < n && currNum >= n) break; x++; lastNum = currNum; } for (int i = 1; i <= x; i++) { for (long j = 1; j <= 4; j++) { if (lastNum + pow(4, x - i) < n) lastNum += pow(4, x - 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; } } } } int main(){ int N = 32; cout<<N<<"th number made of prime digits is "; findNthNumber(N); return 0; }

## Output

32th number made of prime digits is 257

- Related Questions & Answers
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- Find n-th element in a series with only 2 digits (and 7) allowed in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- C++ Programe to find n-th term in series 1 2 2 3 3 3 4
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Find n’th number in a number system with only 3 and 4 in C++
- Find sum of the series 1-2+3-4+5-6+7....in C++
- Sum of series 2/3 – 4/5 + 6/7 – 8/9 + …… upto n terms
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++