def euler14(lim=1000000):
    """Solution for problem 14."""
    collatz = {1: 1}
    for i in range(2, lim):
        if i not in collatz:
            chain = []
            while i not in collatz:
                chain.append(i)
                i = (3 * i + 1) if i % 2 else (i // 2)
            stop = collatz[i] + 1
            for idx, val in enumerate(reversed(chain)):
                collatz[val] = stop + idx
        assert i in collatz
    return max((v, k) for k, v in collatz.items() if k <= lim)[1]