-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path100.go
More file actions
46 lines (39 loc) · 663 Bytes
/
100.go
File metadata and controls
46 lines (39 loc) · 663 Bytes
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
// UVa 100 - The 3n + 1 problem
package main
import (
"fmt"
"os"
)
var cache = map[int]int{1: 1}
func calculate(i int) int {
if _, ok := cache[i]; !ok {
if i%2 == 0 {
cache[i] = 1 + calculate(i/2)
} else {
cache[i] = 1 + calculate(i*3+1)
}
}
return cache[i]
}
func solve(i, j int) int {
max := 0
for k := i; k <= j; k++ {
if m := calculate(k); m > max {
max = m
}
}
return max
}
func main() {
in, _ := os.Open("100.in")
defer in.Close()
out, _ := os.Create("100.out")
defer out.Close()
var i, j int
for {
if _, err := fmt.Fscanf(in, "%d%d", &i, &j); err != nil {
break
}
fmt.Fprintln(out, i, j, solve(i, j))
}
}