In this problem, we are given an element N. we need to find the N’th number in a number system with only 3 and 4.
The number system consists of elements 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, …
Let’s take an example to understand the problem,
Input
N = 6
Output
44
Explanation
The numbers of the number system are − 3, 4, 33, 34, 43, 44...
Solution Approach
The number system is similar to the binary number system but the number 0 is replaced by 3 and number 1 is replaced by 4.
Let’s say this as sBinary.
So, number Nth number is (n-1)’s Sbinary conversion.
With this knowledge, we can easily solve the problem by finding the binary equivalent of (N-1) and then adding each digit of the binary number found by 3.
How to Convert Decimal to Binary?
Program to illustrate the working of our solution,
Example
#include<iostream>
using namespace std;
void findNThTermNumberSystem(int N) {
if(N == 1 || N == 2) {
cout<<(N-1) + 3;
return;
}
N -= 1;
findNThTermNumberSystem(N/2);
cout<<((N % 2) + 3);
}
int main(){
int N = 12;
cout<<N<<"th term of the number system is ";
findNThTermNumberSystem(N);
return 0;
}Output
12th term of the number system is 434