project_euler.problem_120.sol1ΒΆ

Problem 120 Square remainders: https://projecteuler.net/problem=120

Description:

Let r be the remainder when (a-1)^n + (a+1)^n is divided by a^2. For example, if a = 7 and n = 3, then r = 42: 6^3 + 8^3 = 728 ≑ 42 mod 49. And as n varies, so too will r, but for a = 7 it turns out that r_max = 42. For 3 ≀ a ≀ 1000, find βˆ‘ r_max.

Solution:

On expanding the terms, we get 2 if n is even and 2an if n is odd. For maximizing the value, 2an < a*a => n <= (a - 1)/2 (integer division)

FunctionsΒΆ

solution(β†’Β int)

Returns βˆ‘ r_max for 3 <= a <= n as explained above

Module ContentsΒΆ

project_euler.problem_120.sol1.solution(n: int = 1000) intΒΆ

Returns βˆ‘ r_max for 3 <= a <= n as explained above >>> solution(10) 300 >>> solution(100) 330750 >>> solution(1000) 333082500