Given a Number N in Decimal Base, find Number of its Digits in any Base (base b)


The problem statement includes finding the number of digits in N when represented in any base b numeral system. Initially, N is given in the base−10 numeral system.

In the problem, we will be provided with a positive integer N in the input which will be in the base−10 numeral system and a positive integer b greater than 1. Our task will be to find the number of digits when N is being represented in the base−b numeral system.

Any number represented in any base number, every digit from right represents the number of times power of that base number starting from 0.

For example when we represent 15 in base−3.

It is being represented as 120 i.e. $\mathrm{0*3^{0}+2*3^{1}+1*3^{2}=0+6+9=15}$

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

Input

N=16   b=5

Output

2

Explanation − Here we are given N in base−10 representation i.e. 16. We need to count the number of digits of N when represented in base−5.

The representation of 16 in base−5 is 31 i.e. $\mathrm{1*5^{0}+3*5^{1}=1+15=16}$

The number of digits in base−5 representation of 16 is 2 (i.e. 31), which is our required output.

Input

N= 22    b=3

Output

3

Explanation − The value of N represented in the base−10 numeral system is 22 and we are given a base value equal to 3. The representation of 22 in base−3 is 211 i.e. $\mathrm{1*3^{0}+1*3^{1}+2*3^{2}=1+3+18=22}$

Thus the number of digits in base−3 representation of 22 is 3, which is our required output.

Let’s explore the different approaches to count the number of digits in representation of any number N in base−b from naive to an efficient solution.

Approach−1 (Naive Approach)

The naive approach to the solution will be representing N in the base−b numeral system and counting the number of digits.

In order to find the number of digits in the representation of N in base−b, we will make a function. We will initialise a variable to store the count of the number of digits and update N by dividing N by b until N is greater than 0 and keep increasing the count of digits in every iteration. This way we can calculate the number of digits in representation of N in base−b.

This could be better explained as to represent any number in any base number, we take the mod of that number with base number which represents each digit from right and update the number by dividing that number with the base value and repeat the process until the number is greater than 0.

The steps to follow to implement the approach in C++:

  • We will create a function to count the number of digits of N when represented in base b.

  • Initialise a variable to store the count of digits.

  • Iterate in a while loop until N is greater than 0.

  • In every iteration update N by dividing N by b and increase the count by 1 as to find the next left digit in representation we take mod of N with b with the updated N.

  • Return the variable once N is equal to zero which will be the number of digits of N when represented in the base−b numeral system.

Example

//C++ code to find the number of digits of N when represented in base b

#include <bits/stdc++.h>

using namespace std;

//function to calculate the number of digits of N in base b
int digits(long long int N, long long int b){
    //to store the number of digits of N in base b
    int a=0;
    
    //iterating to calculate the number of digits
    while(N>0){
        N = N/b; //update N by dividing it with b to find the next digit
        a++; //increase the count by 1 
    }
    
    return a;
}

int main()
{
    long long int N,b; //for taking input values
    
    N=58734;
    b=4;
    //calling the function
    cout<<"No. of digits of "<<N<<" in base-"<<b<<" :"<<digits(N,b)<<endl;

    return 0;
}

Output

No. of digits of 58734 in base-4 :8

Time Complexity : O(log N) ,because we iterate in a while loop until N is greater than 0 and keep dividing N by base value.

Space Complexity : O(1) , as no extra space is taken to find the count of digits.

Approach−2 (Efficient approach)

We can use the concept of logarithmic function in mathematics to solve the problem in a more efficient manner.

There is a mathematical relation between the number of digits in representation of N in base b, and the number N and base number b. The number of digits in N when represented in base b is (1+ logarithmic value of N with base b).

Let’s understand the relation better with the illustration below.

Suppose, there are a digits in representation of N in base b.

The value of N will lie in the range $\mathrm{b^{a−1}<=N<b^{a}}$,since every digit represents the power of base starting from 0. So if N has a digits, then the value of N must be equal to or greater than $\mathrm{b^{a−1}}$ because a starts from 0 and must be less than $\mathrm{b^{a}}$ which is the next power of b.

Taking log with base b on each side in the equation, we get

$$\mathrm{(a−1)*\log_{b}b<=\log_{b}N}$$

log of any number with the base number equal, it is always 1. Thus the final expression will be

$\mathrm{a<=\frac{\log\:N}{\log\:b}+1}$, as $\mathrm{\log_{b}N}$ can be written as $\mathrm{\frac{\log\:N}{\log\:b}}$

We will use the above formula to calculate the number of digits of N in base−b within constant runtime using the log() function in C++ which returns the value equal to the natural log of the parameter passed.

The steps to implement the approach in C++:

  • Make a function to calculate the number of digits of N in base b.

  • Initialise a variable to store the number of digits.

  • Using the formula derived, we will calculate the number of digits. The log() function may return values in decimal so we will use floor() function in order to get the greatest integer which is less than or equal to the value returned by log() function and to ensure a is always less than or equal to $\mathrm{\frac{\log\:N}{\log\:b}}$

  • Return the number of digits calculated using the formula, which will be our output.

Example

//C++ code to find the number of digits of N when represented in base b

#include <bits/stdc++.h>

using namespace std;

//function to calculate number of digits
int digits(long long int N, long long int b){
    //to store the number of digits of N in base b
    int a=0;
    
    a = floor( log(N) / log(b) ) + 1; //using the formula to calculate number of digits
    
    return a; //return number of digits
}

int main()
{
    long long int N,b; //for taking input values
    
    N=1e15; //storing 10^15 in N
    b=9;
    //calling the function
    cout<<"No. of digits of "<<N<<" in base-"<<b<<" :"<<digits(N,b)<<endl;

    return 0;
}

Output

No. of digits of 1000000000000000 in base-9 :16

Time complexity− O(1) , as log() function takes constant time to return the value

Space complexity − O(1) , as no extra space is taken.

Conclusion

The concept of finding the number of digits in N when represented in any base number b which will be given in the input was discussed in the article. We solved the problem using a naive approach to an efficient solution using the concept of logarithmic function in C++ within constant runtime and space.

I hope you understand the problem and the approach to solve the problem after reading this article.

Updated on: 28-Aug-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements