From 6fca661b7e4d3c80bbfbb0745ebfdbb1df76a317 Mon Sep 17 00:00:00 2001 From: upakarp Date: Mon, 22 Aug 2022 21:40:53 -0700 Subject: [PATCH 1/2] Pythagorean triplet in python --- challenges/pythagorean_triplet/solution.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 challenges/pythagorean_triplet/solution.py diff --git a/challenges/pythagorean_triplet/solution.py b/challenges/pythagorean_triplet/solution.py new file mode 100644 index 0000000..3abf7e8 --- /dev/null +++ b/challenges/pythagorean_triplet/solution.py @@ -0,0 +1,11 @@ +def pythagorean_triplet(num): + for a in range(1, num+1): + for b in range(a+1, num+1): + c = num - a - b + if a**2 + b**2 == c**2: + return (a, b, c) + return (0, 0, 0) + +print(pythagorean_triplet(1000)) +assert (200, 375, 425) == pythagorean_triplet(1000) + From dae40729411ce3b899c9346bb500e1d09ee36c98 Mon Sep 17 00:00:00 2001 From: upakarp Date: Mon, 22 Aug 2022 23:37:47 -0700 Subject: [PATCH 2/2] slight optimization --- challenges/pythagorean_triplet/solution.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenges/pythagorean_triplet/solution.py b/challenges/pythagorean_triplet/solution.py index 3abf7e8..880d6f6 100644 --- a/challenges/pythagorean_triplet/solution.py +++ b/challenges/pythagorean_triplet/solution.py @@ -1,6 +1,6 @@ def pythagorean_triplet(num): - for a in range(1, num+1): - for b in range(a+1, num+1): + for a in range(1, num // 3 + 1): + for b in range(a+1, num // 2 + 1): c = num - a - b if a**2 + b**2 == c**2: return (a, b, c)