From d35117e4170b75f5ab8dc2ab3e8bcf5c4ac25800 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Thu, 9 Sep 2021 15:58:18 +0200 Subject: [PATCH] Solution to problem 39 --- src/Python/Problem039.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Python/Problem039.py diff --git a/src/Python/Problem039.py b/src/Python/Problem039.py new file mode 100644 index 0000000..f2d979d --- /dev/null +++ b/src/Python/Problem039.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +""" +Created on 05 Jun 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for problem 39 of Project Euler +https://projecteuler.net/problem=39 +""" + +from utils import timeit + + +@timeit("Problem 39") +def compute(): + """ + If p is the perimeter of a right angle triangle with integral length sides, + {a,b,c}, there are exactly three solutions for p = 120: + + {20,48,52}, {24,45,51}, {30,40,50} + + For which value of p ≤ 1000, is the number of solutions maximised? + """ + ans, val = 0, 0 + for p in range(2, 1001, 2): + sol = 0 + for a in range(1, p): + for b in range(a+1, p-2*a): + c = p - (a + b) + if a**2 + b**2 == c**2: + sol += 1 + elif a**2 + b**2 > c**2: + # As we continue our innermost loop, the left side + # gets bigger, right gets smaller, so we're done here + break + if sol > ans: + ans, val = sol, p + + return val + +if __name__ == "__main__": + + print(f"Result for Problem 39: {compute()}")