Avoid using floating point numbers. Python only uses 53 bits of precision for floating point numbers, but integers can grow arbitrarily large:
>>> int((3710402416420168191 + 3710402416420167681) / 2)
3710402416420167680
>>> (3710402416420168191 + 3710402416420167681) // 2
3710402416420167936
//
is floor division, so it'll return the integral part of the result without resorting to floats (which is what math.floor
returns).