One minute
Leetcode 1134
The idea of this solution is to just loop through every digit of n
then add the n^(lengthOfNumber)
to a variable called sum
, then we return sum == n
.
func isArmstrong(n int) bool {
lengthOfNumber := len(strconv.Itoa(n))
sum := 0
newN := n
for newN > 0 {
sum += powerOfK(lengthOfNumber, newN%10)
newN /= 10
}
return sum == n
}
func powerOfK(lengthOfNumber, n int) int {
res := 1
for i := 0; i < lengthOfNumber; i++ {
res *= n
}
return res
}
Read other posts