Given A Number n in a base 10, find the number of digits in its base b representation.
Constraints : Whole
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 representation of has digits if , which is the case if or .The number of digits in the base b representation of n is therefore
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.
The Number of digits of Number 1446 in base 7 is 4
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Check if a given number can be represented in given a no. of digits in any base
- Check if the number is even or odd whose digits and base (radix) is given
- Complement of a number with any base b
- Pandigital number in a given base
- Convert from any base to decimal and vice versa
- Check if a number N starts with 1 in b-base
- Check whether a number has consecutive 0's in the given base or not
- Number of trailing zeroes in base B representation of N!
- Check if a number is power of k using base changing method
- Number System and base conversions | Digital Electronics
- All possible numbers of N digits and base B without leading zeros
- Program to find the last digit of X in base Y
- Find the height of a right-angled triangle whose area is X times its base
- Double Base Palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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