forked from sw0x2A/project-euler-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0014.go
39 lines (37 loc) · 652 Bytes
/
0014.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
// https://projecteuler.net/problem=14
package main
func p14(n int) int {
arr := make([]int, n)
steps, maxSteps, cur, max, notFound := 0, 0, 0, 0, true
for i := 1; i < len(arr); i++ {
notFound = true
cur = i
steps = 0
for notFound {
if cur == 1 {
notFound = false
arr[i] = steps
if steps > maxSteps {
maxSteps = steps
max = i
}
} else if cur < i {
notFound = false
steps = steps + arr[cur]
arr[i] = steps
if steps > maxSteps {
maxSteps = steps
max = i
}
} else {
if cur%2 == 0 {
cur /= 2
} else {
cur = 3*cur + 1
}
}
steps++
}
}
return max
}