import itertools SAMPLES = 100000 def triangle_num(n): return n * (n+1) / 2 def pentagonal_num(n): return n * (3 * n -1) / 2 def hexagonal_num(n): return n * (2 * n - 1) if __name__ == '__main__': tris = [triangle_num(x) for x in range(2, SAMPLES)] pents = [pentagonal_num(x) for x in range(2, SAMPLES)] hexs = [hexagonal_num(x) for x in range(2, SAMPLES)] triplets = itertools.product(tris, pents, hexs) print(set(tris) & set(pents) & set(hexs))