''' ((a - 1) ** n + (a + 1) ** n) % (a ** 2) = 2an % (a ** 2), if n is odd; 2, if n is oven ''' def pre_odd(q): n = int(q) if n == q: return n - (1 + n % 2) else: return n - (1 - n % 2) def r_max(a): return max(map(lambda n: 2 * a * n % (a ** 2), map(lambda x: pre_odd(x), [a / 2, a, 3 * a / 2, 2 * a]))) print(sum(map(lambda x: r_max(x), range(3, 1001))))