-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path026.go
72 lines (57 loc) · 1.33 KB
/
026.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
package main
import (
"fmt"
"math/big"
"strings"
)
func getRecurringLengthRat(x *big.Rat) int {
xs := strings.Trim(x.FloatString(3000), "0")
return getRecurringLength(xs)
}
func getRecurringLength(xs string) int {
lr := 0
// cut off leading decimal point
xs = xs[1:]
if len(xs) < 3000 {
return 0
}
for i := 1; i < len(xs)/3; i++ {
// test to see if a string of length i is repeated
J: for j := 0; j < len(xs)-3*i-1; j++ {
// if string of length i at position j is repeated at least twice,
// we have the largest recursion
start := j
end := start+i
xsi := xs[start:end]
start = end
end = start+i
for ; end < len(xs); {
xsii := xs[start:end]
if xsi != xsii {
continue J
}
start = end
end = start+i
}
lr = i
break
}
if lr != 0 {
break
}
}
return lr
}
func main() {
var d, ld int64
ll := 0
for d = 1; d < 1000; d++ {
r := big.NewRat(1,d)
l := getRecurringLengthRat(r)
if l > ll {
ld = d
ll = l
}
}
fmt.Printf("Result: %d\n", ld)
}