-
Notifications
You must be signed in to change notification settings - Fork 0
/
eureka.go
54 lines (43 loc) · 819 Bytes
/
eureka.go
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
package kata
func SumDigPow(a, b uint64) []uint64 {
var result []uint64
for i := a; i <= b; i++ {
if isEurekaNumber(i) {
result = append(result, i)
}
}
return result
}
func isEurekaNumber(n uint64) bool {
digits := GetDigits(n)
var sum uint64
for i, v := range digits {
sum += customPow(v, uint64(i+1))
}
return sum == n
}
func GetDigits(n uint64) []uint64 {
var digits []uint64
for n > 0 {
digits = append(digits, n%10)
n /= 10
}
reverseSlice(digits)
return digits
}
func reverseSlice(slice []uint64) {
start := 0
end := len(slice) - 1
for start < end {
slice[start], slice[end] = slice[end], slice[start]
start++
end--
}
}
func customPow(base, exponent uint64) uint64 {
result := uint64(1)
for i := uint64(0); i < exponent; i++ {
result *= base
}
return result
}