# programming challenge – Euler Project Problem # 14 largest Collatz sequence written in Python Problem Statement:
The following iterative sequence is defined for the set of positive integers:

n → n / 2 (n is even)
n → 3n + 1 (n is odd)

Using the previous rule and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
You can see that this sequence (starting at 13 and ending at 1) contains 10 terms. Although it does not have
still tested (Collatz problem), it is thought that all the initial numbers end in 1.

What initial number, less than one million, produces the longest chain?

NOTE: Once the string starts, the terms can go over a million.

``````from the time of import
from the operator import itemgetter

def collatz_count (n, count = {1: 1}):
"" "use cache (account) to speed up the search, return the sequence
length for the given number "" "
try:
return count[n]
except KeyError:
if n% 2 == 0:
tell[n] = collatz_count (n / 2) + 1
plus:
tell[n] = collatz_count (n * 3 + 1) + 1
return count[n]

def run_test ():
"" "uses the previous function, returns the number generated by the
Major sequence "" "
time1 = time ()
articles = ordered ([(x, collatz_count(x)) for x in range(1, 1000000)],
key = itemgetter (1), reverse = True)
maximum = articles
print (f & # 39; Initial number: {maximum}  n sequence length:
{maximum}  n & # 39;
f & # 39; Calculated in: {time () - time1} seconds. & # 39;)

yes __name__ == & # 39; __ main __ & # 39;
run_test ()
`````` 