Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # key is starting number, value is total hops
- cache = {}
- def solve(n):
- # key is total, value is starting number
- winner = 0
- winning_count = 0
- for x in xrange(1,n):
- count = get_seq_length(x)
- if count > winning_count:
- winner = x
- winning_count = count
- return winner
- def get_seq_length(start):
- count = 1
- count_values = {}
- value = start
- while value > 1:
- if value in cache:
- count += (cache[value] - 1)
- break
- elif value % 2 == 0:
- value = value / 2
- count += 1
- else:
- count_values[count] = value
- value = value * 3 + 1
- count += 1
- for x in count_values:
- cache[count_values[x]] = count - x + 1
- return count
- print solve(1000000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement