Armstrong Number

easy math digits

Problem

Given an integer n, return true if and only if it is an Armstrong number. The k-digit number n is an Armstrong number if and only if the sum of each of its digits raised to the power k equals n.

Inputn = 153
Outputtrue
153 has k = 3 digits, and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

def is_armstrong(n):
    k = len(str(n))
    total = 0
    x = n
    while x > 0:
        d = x % 10
        total += d ** k
        x //= 10
    return total == n
function isArmstrong(n) {
  const k = String(n).length;
  let total = 0;
  let x = n;
  while (x > 0) {
    const d = x % 10;
    total += Math.pow(d, k);
    x = Math.floor(x / 10);
  }
  return total === n;
}
class Solution {
    public boolean isArmstrong(int n) {
        int k = String.valueOf(n).length();
        int total = 0;
        int x = n;
        while (x > 0) {
            int d = x % 10;
            total += (int) Math.pow(d, k);
            x /= 10;
        }
        return total == n;
    }
}
bool isArmstrong(int n) {
    int k = to_string(n).size();
    int total = 0;
    int x = n;
    while (x > 0) {
        int d = x % 10;
        total += (int) pow(d, k);
        x /= 10;
    }
    return total == n;
}
Time: O(log n) Space: O(1)