問題
A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.
Implement is_armstrong(n).
Input
n: A non-negative integer.
Output
Return true if n is an Armstrong number.
制約
0 ≤ n ≤ 10¹⁸
例
入力:
n = 153
期待される出力:
expected = True
入力:
long long n = 153;
期待される出力:
bool expected = true;
入力:
long n = 153;
期待される出力:
bool expected = true;
解説
1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
入力:
n = 10
期待される出力:
expected = False
入力:
long long n = 10;
期待される出力:
bool expected = false;
入力:
long n = 10;
期待される出力:
bool expected = false;
解説
1² + 0² = 1, which is not 10.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Count how many digits the number has before summing powers.
ヒント 2
Compare the original number with the sum of each digit raised to that digit count.
ヒント 3
Remember that 0 is a special one-digit case.
目安の計算量
O(k) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。