1844. Base-B Digit Power Check
A number is self-powered in base b when it equals the sum of its digits in that base, each raised to the power of the number of digits. In base 10, 153 is self-powered because it has three digits and 1^3 + 5^3 + 3^3 = 153. Every single-digit number is self-powered in any base larger than itself, because x to the power 1 is x.
Given n and base, return true if n is self-powered in that base and false otherwise. Write n in the given base, count its digits, and add up the powers. You may stop early if the running total passes n. Aim for O(d^2) time, where d is the number of base-b digits of n.
Example 1
- Input:
- n = 153, base = 10
- Output:
- true
- Explanation:
153 has three digits and 1^3 + 5^3 + 3^3 = 153.
Example 2
- Input:
- n = 154, base = 10
- Output:
- false
- Explanation:
1 + 125 + 64 = 190, which is not 154.
Example 3
- Input:
- n = 5, base = 6
- Output:
- true
- Explanation:
A single digit raised to the power 1 is itself.
Constraints
1 ≤ n ≤ 1012
2 ≤ base ≤ 36
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(d^2)
- Space
- O(d)