From 1d73bbe56b04b2d9c6acea3833689399fb786187 Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Sat, 10 Oct 2020 00:29:54 +0200 Subject: [PATCH 1/4] Added solution for Project Euler problem 91. Reference: #2695 --- project_euler/problem_91/__init__.py | 0 project_euler/problem_91/sol1.py | 51 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 project_euler/problem_91/__init__.py create mode 100644 project_euler/problem_91/sol1.py diff --git a/project_euler/problem_91/__init__.py b/project_euler/problem_91/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_91/sol1.py b/project_euler/problem_91/sol1.py new file mode 100644 index 000000000000..c382f418e98c --- /dev/null +++ b/project_euler/problem_91/sol1.py @@ -0,0 +1,51 @@ +""" +The points P (x1, y1) and Q (x2, y2) are plotted at integer coordinates and +are joined to the origin, O(0,0), to form ΔOPQ. + +There are exactly fourteen triangles containing a right angle that can be formed +when each coordinate lies between 0 and 2 inclusive; that is, +0 ≤ x1, y1, x2, y2 ≤ 2. + +Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed? +""" + + +from itertools import combinations, product + + +def is_right(x1: int, y1: int, x2: int, y2: int) -> bool: + """ + Check if the triangle described by P(x1,y1), Q(x2,y2) and O(0,0) is right-angled. + Note: this doesn't check if P and Q are equal, but that's handled by the use of + itertools.combinations in the solution function. + >>> is_right(0,1,2,0) + True + >>> is_right(1,0,2,2) + False + """ + if x1 == y1 == 0 or x2 == y2 == 0: + return False + a_square = x1 * x1 + y1 * y1 + b_square = x2 * x2 + y2 * y2 + c_square = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + return ( + a_square + b_square == c_square + or a_square + c_square == b_square + or b_square + c_square == a_square + ) + + +def solution(limit: int = 50) -> int: + """ + Return the number of right triangles OPQ that can be formed by two points P, Q + which have both x- and y- coordinates between 0 and limit inclusive. + """ + return sum( + 1 + for pt1, pt2 in combinations(product(range(limit + 1), repeat=2), 2) + if is_right(*pt1, *pt2) + ) + + +if __name__ == "__main__": + print(solution()) From 9035ad7cc0acca978affa2ad411fbef442979f0f Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Sat, 10 Oct 2020 10:48:43 +0200 Subject: [PATCH 2/4] Added doctest for solution() in project_euler/problem_91/sol1.py --- project_euler/problem_91/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_91/sol1.py b/project_euler/problem_91/sol1.py index c382f418e98c..6f32f14af0f8 100644 --- a/project_euler/problem_91/sol1.py +++ b/project_euler/problem_91/sol1.py @@ -39,6 +39,8 @@ def solution(limit: int = 50) -> int: """ Return the number of right triangles OPQ that can be formed by two points P, Q which have both x- and y- coordinates between 0 and limit inclusive. + >>> solution(2) + 14 """ return sum( 1 From 580c3f2152f95df44adb47f843cea9c37c5d67bd Mon Sep 17 00:00:00 2001 From: Freddy Pringle Date: Thu, 15 Oct 2020 11:11:30 +0200 Subject: [PATCH 3/4] Update docstring and 0-padding in directory name. Reference: #3256 --- project_euler/{problem_91 => problem_091}/__init__.py | 0 project_euler/{problem_91 => problem_091}/sol1.py | 4 +++- 2 files changed, 3 insertions(+), 1 deletion(-) rename project_euler/{problem_91 => problem_091}/__init__.py (100%) rename project_euler/{problem_91 => problem_091}/sol1.py (94%) diff --git a/project_euler/problem_91/__init__.py b/project_euler/problem_091/__init__.py similarity index 100% rename from project_euler/problem_91/__init__.py rename to project_euler/problem_091/__init__.py diff --git a/project_euler/problem_91/sol1.py b/project_euler/problem_091/sol1.py similarity index 94% rename from project_euler/problem_91/sol1.py rename to project_euler/problem_091/sol1.py index 6f32f14af0f8..d2c28b83e43c 100644 --- a/project_euler/problem_91/sol1.py +++ b/project_euler/problem_091/sol1.py @@ -1,4 +1,6 @@ """ +Project Euler Problem 91: https://projecteuler.net/problem=91 + The points P (x1, y1) and Q (x2, y2) are plotted at integer coordinates and are joined to the origin, O(0,0), to form ΔOPQ.  @@ -50,4 +52,4 @@ def solution(limit: int = 50) -> int: if __name__ == "__main__": - print(solution()) + print(f"{solution() = }") From 7ba6f3f3cb7a9b9d832b371761b4d980007f51af Mon Sep 17 00:00:00 2001 From: Dhruv Date: Fri, 16 Oct 2020 15:27:35 +0530 Subject: [PATCH 4/4] Update sol1.py --- project_euler/problem_091/sol1.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_091/sol1.py b/project_euler/problem_091/sol1.py index d2c28b83e43c..6c9aa3fa6c70 100644 --- a/project_euler/problem_091/sol1.py +++ b/project_euler/problem_091/sol1.py @@ -20,9 +20,10 @@ def is_right(x1: int, y1: int, x2: int, y2: int) -> bool: Check if the triangle described by P(x1,y1), Q(x2,y2) and O(0,0) is right-angled. Note: this doesn't check if P and Q are equal, but that's handled by the use of itertools.combinations in the solution function. - >>> is_right(0,1,2,0) + + >>> is_right(0, 1, 2, 0) True - >>> is_right(1,0,2,2) + >>> is_right(1, 0, 2, 2) False """ if x1 == y1 == 0 or x2 == y2 == 0: @@ -41,8 +42,11 @@ def solution(limit: int = 50) -> int: """ Return the number of right triangles OPQ that can be formed by two points P, Q which have both x- and y- coordinates between 0 and limit inclusive. + >>> solution(2) 14 + >>> solution(10) + 448 """ return sum( 1