def gen_num(start, limit, iter_func): value = iter_func(start) while value < limit: yield value start += 1 value = iter_func(start) def init_nums(): funcs = (lambda n: n * (n + 1) // 2, lambda n: n * n, lambda n: n * (3 * n - 1) // 2, lambda n: n * (2 * n - 1), lambda n: n * (5 * n - 3) // 2, lambda n: n * (3 * n - 2)) for f in funcs: yield list(filter(lambda x: x > 1000, gen_num(1, 10000, f))) def find(platform, scan, result): if not scan: if result[-1] % 100 == result[0] // 100: return result else: for shape in scan: for num in platform[int(shape)]: if num // 100 == result[-1] % 100: get = find(platform, scan.replace(shape, ''), result + [num]) if get: return get def search(): platform = list(init_nums()) for first in platform[-1]: result = find(platform[:-1], '01234', [first]) if result: return result print(sum(search()))