From a8ad2d10b426f28a1c04ee8d53f43a9f9f968851 Mon Sep 17 00:00:00 2001 From: Utkarsh Chaudhary Date: Tue, 6 Oct 2020 08:41:15 +0530 Subject: [PATCH] Add Project Euler 120 solution (#2887) --- project_euler/problem_120/__init__.py | 0 project_euler/problem_120/sol1.py | 32 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 project_euler/problem_120/__init__.py create mode 100644 project_euler/problem_120/sol1.py diff --git a/project_euler/problem_120/__init__.py b/project_euler/problem_120/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_120/sol1.py b/project_euler/problem_120/sol1.py new file mode 100644 index 000000000..0e6821214 --- /dev/null +++ b/project_euler/problem_120/sol1.py @@ -0,0 +1,32 @@ +""" +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) +""" + + +def solution(n: int = 1000) -> int: + """ + Returns ∑ r_max for 3 <= a <= n as explained above + >>> solution(10) + 300 + >>> solution(100) + 330750 + >>> solution(1000) + 333082500 + """ + return sum(2 * a * ((a - 1) // 2) for a in range(3, n + 1)) + + +if __name__ == "__main__": + print(solution())