-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1134-ArmstrongNumber.go
More file actions
56 lines (49 loc) · 1.37 KB
/
1134-ArmstrongNumber.go
File metadata and controls
56 lines (49 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
// 1134. Armstrong Number
// 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 kth power of each digit sums to n.
// Example 1:
// Input: n = 153
// Output: true
// Explanation: 153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.
// Example 2:
// Input: n = 123
// Output: false
// Explanation: 123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.
// Constraints:
// 1 <= n <= 10^8
import "fmt"
func isArmstrong(n int) bool {
tobits := func (n int) []int { // 拆成单数(0-9)数组
res := []int{}
for n > 0 {
res = append(res, n%10)
n /= 10
}
return res
}
sum, nbits := 0,tobits(n)
pow := func (b int, p int) int { // 求幂
res := 1
for i := 0; i < p; i++ {
res *= b
}
return res
}
for i, size := 0, len(nbits); i < size; i++ {
sum += pow(nbits[i], size)
}
return sum == n
}
func main() {
// Example 1:
// Input: n = 153
// Output: true
// Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.
fmt.Println(isArmstrong(153)) // true
// Example 2:
// Input: n = 123
// Output: false
// Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.
fmt.Println(isArmstrong(123)) // false
}