Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- func main() {
- var winner, winningCount uint
- var i uint
- cache := make(map[uint]uint)
- for i=1; i<1000000; i++ {
- count := getSeqLength(i, cache)
- if count > winningCount {
- winningCount = count
- winner = i
- }
- }
- fmt.Println(winner)
- }
- func getSeqLength(start uint, cache map[uint]uint) uint {
- var count uint = 1
- countValues := make(map[uint]uint)
- value := start
- for value > 1 {
- if cachedValue := cache[value]; cachedValue != 0 {
- count += (cachedValue - 1)
- break
- } else if value % 2 == 0 {
- value = value >> 1
- count++
- } else {
- countValues[count] = value
- value = value * 3 + 1
- count++
- }
- }
- for k, v := range countValues {
- cache[v] = count - k + 1
- }
- return count
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement