Saturday, July 2, 2022

Nth number made of prime digits

 Nth number made of prime digits

Problem Statement
Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.

c++ code:

#include<bits/stdc++.h>
using namespace std;
class Solution
{
    public:
    int primeDigits(int n)
    {
        queue<int>q;
        q.push(0);
        for(int i=0;i<=n;)
        {
            int res=q.front();
            q.pop();
             i++;
            q.push((res*10)+2);
            if(i==n)break;
            i++;
            q.push((res*10)+3);
            if(i==n)break;
            i++;
            q.push((res*10)+5);
            if(i==n)break;
            i++;
            q.push((res*10)+7);
            if(i==n)break;
            
        }
        return q.back();
    }
};

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        Solution ob;
        cout << ob.primeDigits(n) << "\n";   
    }
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...