Given a number N in decimal base, find number of its digits in any base (base b)

Given A Number n in a base 10, find the number of digits in its base b representation.
Constraints : N \in \mathbb{W} Whole

Examples :

Input : Number = 48 
        Base = 4
Output: 3
Explanation : (48)10 = (300)4

Input : Number = 1446
        Base = 7
Output: 4
Explanation : (446)10 = (4134)7

A simple approach: convert the decimal number into the given base r and then count number of digits.

An efficient approach : It resides on the relationship between the base of the number and number of digits of that number.
Typically : Let n be a positive integer. The base b representation of n has d digits if b^{d-1}\leq n < b^d, which is the case if d-1 \leq \log_b n < d or \lfloor log_b n \rfloor = d-1 .The number of digits in the base b representation of n is therefore
 \lfloor log_b N \rfloor + 1 = \left \lfloor \dfrac {ln N}{ln b} \right \rfloor + 1 = \left \lfloor \dfrac {log N}{log b} \right \rfloor + 1
In above equation the base changing logarithmic property has been used. So we calculate the logarithm of the number in that base which we want to calculate the number of digits. And take its ceiling and then add 1.

This idea can be further used to find the number of digits of a given number n of base b in base r. All have to be done is to convert the number in base 10 and then apply the above formula of finding digits. It would be easier to calculate log of any base when number is in base 10.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Find Number of digits 
// in base b.
#include <iostream>
#include <math.h> 
using namespace std;
  
// function to print number of
// digits
void findNumberOfDigits(long n, int base)
{
    // Calculating log using base
    // changing property and then
    // taking it floor and then 
    // adding 1.
    int dig = (int)(floor( log(n) / 
                         log(base)) + 1); 
      
    // printing output
    cout << "The Number of digits of "
         << "Number " << n << " in base "
         << base << " is " << dig;
}
  
// Driver method 
int main()
{
    // taking inputs
    long n = 1446;
    int base = 7;
      
    // calling the method
    findNumberOfDigits(n, base);
    return 0;
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Find Number
// of digits in base b.
class GFG {
      
    // function to print number of digits
    static void findNumberOfDigits(long n, int base)
    {
          
        // Calculating log using base changing
        // property and then taking it 
        // floor and then adding 1.
        int dig = (int)(Math.floor(
                        Math.log(n) / Math.log(base))
                        + 1); 
          
          
        // printing output
        System.out.println("The Number of digits of Number "
                            + n + " in base " + base 
                            + " is " + dig);
    }
  
    // Driver method    
    public static void main(String[] args)
    {
        // taking inputs
        long n = 1446;
        int base = 7;
          
        // calling the method
        findNumberOfDigits(n, base);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find Number of digits 
# in base b.
  
import math
  
# function to print number of
# digits
def findNumberOfDigits(n, base):
      
    # Calculating log using base
    # changing property and then
    # taking it floor and then 
    # adding 1.
    dig = (math.floor(math.log(n) /
                 math.log(base)) + 1)
      
    # printing output
    print ("The Number of digits of"
      " Number {} in base {} is {}"
            . format(n, base, dig))
  
# Driver method 
  
# taking inputs
n = 1446
base = 7
  
# calling the method
findNumberOfDigits(n, base)
  
# This code is contributed by 
# Manish Shaw (manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Find Number of digits 
// in base b.
using System;
  
class GFG {
      
    // function to print number of
    // digits
    static void findNumberOfDigits(long n,
                                    int b)
    {
        // Calculating log using base
        // changing property and then
        // taking it floor and then 
        // adding 1.
        int dig = (int)(Math.Floor(
          Math.Log(n) / Math.Log(b)) + 1); 
          
        // printing output
        Console.Write("The Number of digits"
           + " of Number " + n + " in base "
                        + b + " is " + dig);
    }
  
    // Driver method 
    public static void Main()
    {
        // taking inputs
        long n = 1446;
        int b = 7;
          
        // calling the method
        findNumberOfDigits(n, b);
    }
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to Find Number 
// of digits in base b.
      
// function to print
// number of digits
function findNumberOfDigits($n, $b)
{
    // Calculating log using base
    // changing property and then
    // taking it floor and then 
    // adding 1.
    $dig = (int)(floor(log($n) / 
                       log($b)) + 1); 
      
    // printing output
    echo ("The Number of digits".
               " of Number ". $n.
                  " in base ".$b.
                    " is ".$dig);
}
  
// Driver Code
$n = 1446;
$b = 7;
      
// calling the method
findNumberOfDigits($n, $b);
  
// This code is contributed by 
// Manish Shaw (manishshaw1)
?>

chevron_right



Output :

The Number of digits of Number 1446 in base 7 is 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : manishshaw1



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.