def get_triplets_for_perimter(p): return [(a, b, p - a - b) for b in range(1, p // 2 + 1) for a in range(1, b) if a * a + b * b == (p - a - b) * (p - a - b)] def euler_039(): return max([(len(get_triplets_for_perimter(p)), p) for p in range(1, 1001)])[1] if __name__ == "__main__": assert(euler_039() == 840) print("e039.py: " + str(euler_039()))