-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (45 loc) · 818 Bytes
/
main.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
package main
import (
"fmt"
"time"
)
func sum(numbers []int) int {
sum := 0
for _, num := range numbers {
sum += num
}
return sum
}
func IntPow(n, m int) int {
if m == 0 {
return 1
}
if m == 1 {
return n
}
result := n
for i := 2; i <= m; i++ {
result *= n
}
return result
}
func SumOfPowersOfDigits(number int, power int) int {
sum := 0
for number != 0 {
digit := number % 10
sum += IntPow(digit, power)
number = number / 10
}
return sum
}
func main() {
start := time.Now()
numbers := make([]int, 0)
for i := 2; i <= 1000000; i++ {
if i == SumOfPowersOfDigits(i, 5) {
numbers = append(numbers, i)
}
}
elapsed := time.Since(start)
fmt.Printf("all the numbers that can be written as the sum of fifth powers of their digits=%d (elapsed=%v)\n", sum(numbers), elapsed)
}