-
Notifications
You must be signed in to change notification settings - Fork 0
/
p125.go
64 lines (50 loc) · 923 Bytes
/
p125.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
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"sort"
"strconv"
"time"
)
func IsPalindromes(x int) bool {
str := strconv.FormatInt(int64(x), 10)
for i := 0; i < len(str)/2; i++ {
if str[i] != str[len(str)-1-i] {
return false
}
}
return true
}
func main() {
start := time.Now()
limit := 100000000
square := make([]int, limit/2+1)
accu := make([]int, limit/2+1)
elem := []int{}
square[0] = 0
accu[0] = 0
for i := 1; i < limit/2+1; i++ {
square[i] = i * i
accu[i] = accu[i-1] + square[i]
}
for i := 0; i < limit/2+1; i++ {
for j := i + 2; j < limit/2+1; j++ {
curr := accu[j] - accu[i]
if curr >= limit {
break
}
if IsPalindromes(curr) {
elem = append(elem, curr)
}
}
}
sort.Ints(elem)
ans := elem[0]
for i := 1; i < len(elem); i++ {
if elem[i] != elem[i-1] {
ans += elem[i]
}
}
fmt.Println(ans)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}