Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Solving 14 using go, completes < 3 seconds w/memoization
  • Loading branch information
coleifer committed Apr 22, 2012
1 parent 43526b0 commit c7350e7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions go/problem14.go
@@ -0,0 +1,38 @@
package main

import (
"fmt"
)

func next(n int64) int64 {
if n % 2 == 0 {
n = n / 2
} else {
n = (3*n) + 1
}
return n
}

func main() {
var largest, largest_i, i, ct, curr int64
seq_memo := make(map[int64] int64, 1000000)
for i = 1; i < 1e6; i++ {
curr = i
ct = 0
for ; curr != 1; {
if val, ok := seq_memo[curr]; ok {
ct += val
break
} else {
ct++
curr = next(curr)
}
}
seq_memo[i] = ct
if ct > largest {
largest = ct
largest_i = i
}
}
fmt.Printf("%v\n", largest_i)
}

0 comments on commit c7350e7

Please sign in to comment.