def euler_122(): upper = 201 m_k = {k: 0 for k in range(2, upper)} sets = [[1]] for _ in range(11): new_sets = [] for s in sets: for i in range(len(s)): new_elem = s[i] + s[-1] if new_elem in m_k and m_k[new_elem] == 0: m_k[new_elem] = len(s) new_sets.append(s + [new_elem]) # For better performance, we would have to prune here. sets = new_sets r = 0 for k in range(2, upper): assert m_k[k] != 0 r += m_k[k] return r if __name__ == "__main__": solution = euler_122() print("e122.py: " + str(solution)) assert solution == 1582