Given two positive integers a and b, task is to find the number of digits in a^b (a raised to the power b).
Input: a = 2 b = 5 Output: no. of digits = 2 Explanation: 2^5 = 32 Hence, no. of digits = 2 Input: a = 2 b = 100 Output: no. of digits = 31 Explanation: 2^100 = 1.2676506e+30 Hence, no. of digits = 31
The number of digits in a^b can be calculated using the formula:
Number of Digits = 1 + b * (log10a)
When a number is divided by 10, it is reduced by 1 digit.
554 / 10 = 55, 55 / 10 = 5
Notice, 554 initially has 3 digits but after division there are 2 digits 55 and after further division there is only 1 digit 5. So it can be concluded that to count number of digits, how many times a number is divided by 10 to reach 1 needs to be calculated.
log base 10 of a number is the number of times a number needs to be divided by 10 to reach 1 but as 1 itself is not included in log base 10, 1 is added to get the number of digits.
Note: Floor value of b * (log10a) is taken.
Below is the implementation to calculate the number of digits in a^b.
no.of digits = 31
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest number with given sum of digits and sum of square of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the average of k digits from the beginning and l digits from the end of the given number
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers from given range having odd digits at odd places and even digits at even places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.