
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Find n-th element in a series with only 2 digits (and 7) allowed in C++
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- Position of n among the numbers made of 2, 3, 5 & 7
- n-th number whose sum of digits is ten in C++
- Finding the power of prime number p in n! in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Finding n-th term of series 3, 13, 42, 108, 235... in C++
- Find the Nth Number Made Up of only Odd Digits using C++
- Minimum number with digits as and 7 only and given sum in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Summing up digits and finding nearest prime in JavaScript
- Convert to number with digits as 3 and 8 only in C++
- Number of digits in the nth number made of given four digits in C++
- Largest number with prime digits in C++
