From 899870be4cc759941751477b75e744de5a180a30 Mon Sep 17 00:00:00 2001 From: Edward Nuno Date: Thu, 8 Oct 2020 04:21:32 -0700 Subject: [PATCH] Add style improvements to Project Euler problem 9 (#3046) --- project_euler/problem_09/sol1.py | 7 ++++--- project_euler/problem_09/sol2.py | 15 ++++++++------- project_euler/problem_09/sol3.py | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index caba6b1b1..1ab3376ca 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -1,5 +1,6 @@ """ -Problem Statement: +Problem 9: https://projecteuler.net/problem=9 + A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. @@ -9,7 +10,7 @@ Find the product abc. """ -def solution(): +def solution() -> int: """ Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: @@ -29,7 +30,7 @@ def solution(): return a * b * c -def solution_fast(): +def solution_fast() -> int: """ Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py index d16835ca0..e22ed45e8 100644 --- a/project_euler/problem_09/sol2.py +++ b/project_euler/problem_09/sol2.py @@ -1,5 +1,6 @@ """ -Problem Statement: +Problem 9: https://projecteuler.net/problem=9 + A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. @@ -9,27 +10,27 @@ Find the product abc. """ -def solution(n): +def solution(n: int = 1000) -> int: """ Return the product of a,b,c which are Pythagorean Triplet that satisfies the following: 1. a < b < c 2. a**2 + b**2 = c**2 - 3. a + b + c = 1000 + 3. a + b + c = n >>> solution(1000) 31875000 """ product = -1 - d = 0 + candidate = 0 for a in range(1, n // 3): """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c""" b = (n * n - 2 * a * n) // (2 * n - 2 * a) c = n - a - b if c * c == (a * a + b * b): - d = a * b * c - if d >= product: - product = d + candidate = a * b * c + if candidate >= product: + product = candidate return product diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index ed27f089b..0900a76e6 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -1,5 +1,5 @@ """ -Problem Statement: +Problem 9: https://projecteuler.net/problem=9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -12,7 +12,7 @@ Find the product abc. """ -def solution(): +def solution() -> int: """ Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: