Let r be the remainder when is divided by . For example, if a = 7 and n = 3, then r = 42: 63 + 83 = 728 ≡ 42 mod 49. And as n varies, so too will r, but for a = 7 it turns out that rmax = 42. For 3 ≤ a ≤ 1000, find ∑ rmax.
If n is even, then r = 2; if n is odd, we have
To maximise r, we need to have 2n < a
which gives n <= (a - 1) // 2
def p120():
return sum(2 * ((a - 1) // 2) * a for a in range(3, 1001))