-
Notifications
You must be signed in to change notification settings - Fork 0
/
p55.go
78 lines (61 loc) · 1020 Bytes
/
p55.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"strconv"
"time"
)
func runeReverse(x []rune) []rune {
y := make([]rune, len(x))
for i := 0; i < len(x); i++ {
y[i] = x[len(x)-1-i]
}
return y
}
func runeAdd(x, y []rune) []rune {
z := make([]rune, len(x)+1)
for i := len(x) - 1; i > -1; i-- {
z[i+1] = x[i] + y[i] - '0'
}
z[0] = 0
for i := len(z) - 1; i > 0; i-- {
if z[i] > '9' {
z[i] -= 10
z[i-1]++
}
}
if z[0] == 0 {
return z[1:]
} else {
z[0] += '0'
return z
}
}
func IsPalindrome(x []rune) bool {
for i := 0; i < len(x)/2; i++ {
if x[i] != x[len(x)-1-i] {
return false
}
}
return true
}
func IsLychrel(x []rune) bool {
for i := 0; i < 50; i++ {
x = runeAdd(x, runeReverse(x))
if IsPalindrome(x) {
return false
}
}
return true
}
func main() {
start := time.Now()
ans := 0
for i := int64(10); i < 10000; i++ {
if IsLychrel([]rune(strconv.FormatInt(i, 10))) {
ans++
}
}
fmt.Println(ans)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}