The Wayback Machine - https://web.archive.org/web/20240717024726/https://www.geeksforgeeks.org/given-number-n-decimal-base-find-number-digits-base-base-b/
Open In App

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

Last Updated : 28 Feb, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 floor value 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++

// 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)

                    

Java

// Java program to Find Number
// of digits in base b.
import java.io.*;
public 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);
    }
}

                    

Python3

# 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)

                    

C#

// 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)

                    

PHP

<?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)
?>

                    

Javascript

<script>
 
// Javascript program to Find Number of digits
// in base b.
 
// function to print number of
// digits
function findNumberOfDigits(n, base)
{
    // Calculating log using base
    // changing property and then
    // taking it floor and then
    // adding 1.
    var dig = parseInt(Math.floor( Math.log(n) /
                        Math.log(base)) + 1);
     
    // printing output
    document.write("The Number of digits of "
        + "Number " + n + " in base "
        + base + " is " + dig);
}
 
// Driver method
 
// taking inputs
var n = 1446;
var base = 7;
 
// calling the method
findNumberOfDigits(n, base);
 
</script>

                    

Output : 
 

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

Complexity Analysis:
Time complexity : O(logN)
Space complexity : O(1)

Optimized Approach: 

Here are few optimizations that can be made to the given program:

  • Avoid using math.h library: The log function provided by the math.h library is a costly operation. Instead of using it, we can use the base changing property of logarithm and use log10 function to calculate the logarithm of n in base b. This will reduce the overhead of including the math.h library.
  • Replace floor function with casting: Instead of using the floor function, we can cast the result of the logarithmic operation to an integer type. This is faster and simpler than calling the floor function.

Here’s the optimized version of the program with the above optimizations:

C++

#include <iostream>
using namespace std;
 
void findNumberOfDigits(long n, int base)
{
    int dig = 0;
    while (n > 0) {
        n /= base;
        dig++;
    }
    cout << "The Number of digits of "
         << "Number " << n << " in base "
         << base << " is " << dig;
}
 
int main()
{
    long n = 1446;
    int base = 7;
    findNumberOfDigits(n, base);
    return 0;
}

                    

Java

import java.util.*;
 
public class Main {
    public static void findNumberOfDigits(long n, int base) {
        int dig = 0;
        while (n > 0) {
            n /= base;
            dig++;
        }
        System.out.println("The Number of digits of Number " + n + " in base " + base + " is " + dig);
    }
 
    public static void main(String[] args) {
        long n = 1446;
        int base = 7;
        findNumberOfDigits(n, base);
    }
}

                    

Python3

def findNumberOfDigits(n, base):
    dig = 0
    while n > 0:
        n //= base
        dig += 1
    print(f"The Number of digits of Number {n} in base {base} is {dig}")
 
n = 1446
base = 7
findNumberOfDigits(n, base)

                    

C#

using System;
 
public class Program
{
    public static void FindNumberOfDigits(long n, int baseVal)
    {
        int dig = 0;
        while (n > 0)
        {
            n /= baseVal;
            dig++;
        }
        Console.WriteLine("The Number of digits of Number " + n + " in base " + baseVal + " is " + dig);
    }
 
    public static void Main()
    {
        long n = 1446;
        int baseVal = 7;
        FindNumberOfDigits(n, baseVal);
    }
}

                    

Javascript

function findNumberOfDigits(n, base) {
    let dig = 0;
    while (n > 0) {
        n = Math.floor(n / base);
        dig++;
    }
    console.log(`The Number of digits of Number ${n} in base ${base} is ${dig}`);
}
 
let n = 1446;
let base = 7;
findNumberOfDigits(n, base);

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

Complexity Analysis:
Time complexity : O(logN)
Auxiliary Space : O(1)



Similar Reads

Given a number N in decimal base, find the sum of digits in any base B
Given a number N in decimal base, the task is to find the sum of digits of the number in any base B. Examples: Input: N = 100, B = 8 Output: 9 Explanation: (100)8 = 144 Sum(144) = 1 + 4 + 4 = 9 Input: N = 50, B = 2 Output: 3 Explanation: (50)2 = 110010 Sum(110010) = 1 + 1 + 0 + 0 + 1 + 0 = 3 Approach: Find unit digit by performing modulo operation
6 min read
Find the Nth digit from right in base B of the given number in Decimal base
Given a number A in decimal base, the task is to find the Nth digit from last in base BExamples: Input: A = 100, N = 3, B = 4 Output: 2 Explanation: (100)4 = 1210 3rd digit from last is 2 Input: A = 50, N = 3, B = 5 Output: 2 Explanation: (50)5 = 200 3rd digit from last is 2 Naive Approach: The basic idea is to first convert the decimal number A in
7 min read
Check if a given number can be represented in given a no. of digits in any base
Given a number and no. of digits to represent the number, find if the given number can be represented in given no. of digits in any base from 2 to 32.Examples : Input: 8 4 Output: Yes Possible in base 2 as 8 in base 2 is 1000 Input: 8 2 Output: Yes Possible in base 3 as 8 in base 3 is 22 Input: 8 3 Output: No Not possible in any base We strongly re
12 min read
Convert from any base to decimal and vice versa
Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. The value of A is 10, the value of B is 11 and so on. Write a function to convert the number to decimal. Examples: Input number is given as string and output is an integer.Input: str = "1100", base
15+ min read
Find a N-digit number such that it is not divisible by any of its digits
Given an integer N, the task is to find an N-digit number such that it is not divisible by any of its digits.Note: There can be multiple answers for each value of N. Examples: Input: N = 4 Output: 6789 Explanation: As the number 6789 is not divisible by any of its digits, it is 6, 7, 8 and 9 and it is also a four-digit number. Hence, it can be the
5 min read
Find a N-digit number such that it is not divisible by any of its digits
Given an integer N, the task is to find any N-digit positive number (except for zeros) such that it is not divisible by any of its digits. If it is not possible to find any such number then print -1.Note: There can be more than one such number for the same N-digit. Examples: Input: N = 2 Output: 2323 is not divisible by 2 or 3 Input: N = 3Output: 2
7 min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Program to check if a number is divisible by any of its digits
Given an integer N where [Tex]1 \leq n \leq 10^{18} [/Tex]. The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by both 1 and 5.So print YES.Input : 27Output : NOExp
10 min read
Check if the sum of digits of number is divisible by all of its digits
Given an integer N, the task is to check whether the sum of digits of the given number is divisible by all of its digits or not. If divisible then print Yes else print No. Examples: Input: N = 12 Output: No Sum of digits = 1 + 2 = 3 3 is divisible by 1 but not 2. Input: N = 123 Output: Yes Approach: First find the sum of the digits of the number th
14 min read
Smallest N digit number with none of its digits as its divisor
Given an integer N. The task is to find the smallest N digit number S, such that S is not divisible by any of its digits. Print -1 if no such number is possible.Examples: Input: N = 2 Output: 23 Explanation: 23 is the smallest two-digit number that is not divisible by any of its digits.Input: N = 1 Output: -1 Explanation: Every single digit number
10 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg