Skip to content

1134. Armstrong Number 👍

  • Time: $O(\log n)$
  • Space: $O(\log n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  bool isArmstrong(int n) {
    const string s = to_string(n);
    const int k = s.length();
    for (const char c : s)
      n -= pow(c - '0', k);
    return n == 0;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public boolean isArmstrong(int n) {
    final String s = String.valueOf(n);
    final int k = s.length();
    for (final char c : s.toCharArray())
      n -= Math.pow(c - '0', k);
    return n == 0;
  }
}
1
2
3
4
5
class Solution:
  def isArmstrong(self, n: int) -> bool:
    s = str(n)
    k = len(s)
    return sum(pow(int(c), k) for c in s) == n