Question

Formatted question description: https://leetcode.ca/all/1134.html

 1134. Armstrong Number

 The k-digit number N is an Armstrong number if and only if the k-th power of each digit sums to N.

 Given a positive integer N, return true if and only if it is an Armstrong number.


 Example 1:

 Input: 153
 Output: true
 Explanation:
 153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.


 Example 2:

 Input: 123
 Output: false
 Explanation:
 123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.


 Note:

 1 <= N <= 10^8

Algorithm

Just follow problem description.

Code

Java

public class Armstrong_Number {

    class Solution {
        public boolean isArmstrong(int N) {
            int sum = 0;
            int tmp = N;
            int length = Integer.toString(N).length();
            while (tmp > 0){
                sum += power(tmp % 10, length);
                tmp /= 10;
            }
            return sum == N;
        }

        private int power(int a, int b){
            int res = 1;
            for (int i = 0; i < b; i++){
                res *= a;
            }
            return res;
        }
    }

}

All Problems

All Solutions