from functools import cache def euler_092(): count = 0 square_dict = {str(i): i * i for i in range(10)} square_lookup = lambda s: square_dict[s] @cache def number_chain(n): if n == 1 or n == 89: return n squared_digits = map(square_lookup, str(n)) new_n = sum(squared_digits) n = number_chain(new_n) return n for n in range(1, 10**7 + 1): if number_chain(n) == 89: count += 1 return count if __name__ == "__main__": solution = euler_092() print("e092.py: " + str(solution)) assert(solution == 8581146)