From 825cfe5f650bb9ea3466a9fbac3e1c25fcc0798f Mon Sep 17 00:00:00 2001 From: KevinED11 Date: Thu, 5 Oct 2023 08:17:56 -0700 Subject: [PATCH] reto #39 - python --- .../python/KevinED11.py" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "Retos/Reto #39 - TRIPLES PITAG\303\223RICOS [Media]/python/KevinED11.py" diff --git "a/Retos/Reto #39 - TRIPLES PITAG\303\223RICOS [Media]/python/KevinED11.py" "b/Retos/Reto #39 - TRIPLES PITAG\303\223RICOS [Media]/python/KevinED11.py" new file mode 100644 index 0000000000..c569da316d --- /dev/null +++ "b/Retos/Reto #39 - TRIPLES PITAG\303\223RICOS [Media]/python/KevinED11.py" @@ -0,0 +1,45 @@ +import abc +from typing import TypeAlias +import functools + + +PythagoreanTripleList: TypeAlias = list[tuple[int, int, int]] + + +class PythagoreanCalculator(abc.ABC): + @abc.abstractmethod + def calculate(self, max_number: int) -> PythagoreanTripleList: + pass + + +@functools.lru_cache +def calculate_pythagorean_triples(max_number: int) -> PythagoreanTripleList: + triples = [] + c, m = 0, 2 + while c < max_number: + for n in range(1, m): + a = m * m - n * n + b = 2 * m * n + c = m * m + n * n + if c > max_number: + break + triples += [(a, b, c)] + m += 1 + + return triples + + +class PythagoreanTriplesCalculator(PythagoreanCalculator): + def calculate(self, max_number: int) -> PythagoreanTripleList: + return calculate_pythagorean_triples(max_number=max_number) + + +def main(calculator: PythagoreanCalculator) -> None: + max_number = 10 + result = calculator.calculate(max_number) + print(f"Pythagorean triples up to {max_number}: {result}") + + +if __name__ == "__main__": + calculator = PythagoreanTriplesCalculator() + main(calculator=calculator)