- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Position of n among the numbers made of 2, 3, 5 & 7

The problem statement includes printing the position of n among the numbers made of 2, 3, 5 and 7, where n will be any positive number given by the user.

The numbers made of 2, 3, 5 and 7 means this will be the sequence of the strictly increasing numbers which comprises only digits 2,3,5 or 7 i.e. first four prime numbers. The first few numbers of the sequence where all numbers have only 2,3,5 and 7 as their digits are **2, 3, 5, 7, 22, 23, 25, 27, 32, 33, 35, 37, and so on.**

Basically, every number in the sequence is made with the combination of these 4 digits i.e. 2, 3, 5 or 7 and the sequence is in ascending order.

In this problem, we will give a number N which will have only 2, 3, 5 and 7 as its digits and we need to figure out the position of the given number in the sequence and print it which will be the required output.

Let’s understand the problem better with the below examples.

INPUT : N=33

OUTPUT : 10

**Explanation** − The given number with only 2,3,5 or 7 as its digits is 33 in the input. The position of 33 in the sequence of such numbers with only their digits as 2, 3, 5 or 7 is 10th. Hence, our output is 10.

INPUT : 52

OUTPUT : 13

**Explanation** − The number in the input is 52. When we follow the pattern in the sequence where each number has 2, 3, 5 or 7 as its digit and the sequence is in strictly increasing order, we find 52 on the 13th position. Hence our required output is 13.

Let’s understand the algorithm to find out the position of the given number directly without creating the whole sequence.

## Algorithm

If we observe the sequence of the numbers with their digits only 2, 3, 5 or 7, we can see that we can only form 4 combinations of the numbers with a particular digit. We will be using this logic to figure out the position of any number given, N.

The first four numbers have the positions as below:

2 : 1st position

3 : 2nd position

5 : 3rd position

7 : 4th position

Since the sequence is in the ascending order, the next four numbers will be 2 digit numbers with the first digit as 2 because we can only form 4 numbers with a particular digit.

We can find the position of any number with any number of digits by multiplying the position of the first digit from left by 4 and adding it with the position of that particular digit.

For example, N=52

Initially position will be 0.

Starting from left digit, position of 5 in the sequence will be **position*4+position of 5** i.e. 0*4+3=3. Now the position will be 3.

The next digit is 2 so the position of the number will be 3*4+1 because the current position is 3 multiplied by 4 and adding the position of the current digit makes the position 13, which is the position of the number 52 in the sequence.

To solve the problem, we will simply initialise the position with 0. And then keep checking every digit until the last digit of the number and update the position accordingly −

For the digit 2, position will be position*4+1.

For the digit 3, position will be position*4+2.

For the digit 5, position will be position*4+3.

For the digit 7, position will be position*4+4.

We are updating the position by multiplying it by 4 because with every possible digit we can only make 4 combinations. So every time multiplying the position with 4 and adding the position of the current digit, we can get the position of the number N with its digits only 2,3,5 or 7.

We will be using this algorithm in our approach in order to solve the problem efficiently.

## Approach

The steps to follow to implement the algorithm in our approach to print the position of the number N made of 2, 3, 5 or 7 only −

We will create a function to get the position of the given number with its digits as only 2,3,5 or 7.

We will take the input number N in the form of a string.

Thus, from i=0 to i<string.size, we iterate over the string (). We will employ a variety of conditional statements within the loop to account for all potential scenarios.

For the first case if the ith digit is 2 we will multiply the position with 4 and with 1 as 1 is the position of 2. Similarly we will use the formula for the position of the number up to i position according to the ith digit as discussed in the algorithm section.

Every iteration, keep updating the position in accordance with the digit that is now in ith place.

Return the value stored in the position, which is the output we need, when we've finished iterating through the entire string.

## Example

The C++ code for the approach:

#include <bits/stdc++.h> using namespace std; //to find the position of the number with only 2,3,5 or 7 as its digits int position(string N){ int p=0; //to store the position of the number //iterating in a for loop to calculate the position for(int i=0;i<N.size();i++){ if(N[i]=='2'){ //if the digit is 2 p = p * 4 + 1; //multiplying the position by 4 and adding the position of the digit } else if(N[i]=='3'){ // if the digit is 3 p = p * 4 + 2; } else if(N[i]=='5'){ //if the digit is 5 p = p * 4 + 3; } else{ // for the case when digit is 7 p = p * 4 + 4; } } return p; //return the position of the number N } using namespace std; int main() { string N; N = "2357"; //calling the function cout<<"The position of "<<N<<" in the sequence is : "<<position(N)<<endl; N = "3327"; cout<<"The position of "<<N<<" in the sequence is : "<<position(N)<<endl; return 0; }

## Output

The position of 2357 in the sequence is : 112 The position of 3327 in the sequence is : 168

**Time Complexity** − O(n) , where n is the length of the string or the number of digits in the number as we iterates in a for loop n times to calculate the position of the given number in the sequence

**Space Complexity** − O(1) , because we didn’t use any extra space to solve the problem.

## Conclusion

The algorithm to find the position of the number N in the sequence of the numbers with their digits as only 2,3,5 or 7 was discussed in the article and we implemented the algorithm in our approach in order to solve the problem in C++ efficiently without using any extra space within O(n) time.

I hope after reading this article you understand the problem and the approach to solve it in C++.