Skip to content

Commit

Permalink
Merge pull request #5238 from KevinED11/main
Browse files Browse the repository at this point in the history
reto #39 - python
  • Loading branch information
kontroldev authored Oct 6, 2023
2 parents f0d9070 + 825cfe5 commit cfa66b9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Retos/Reto #39 - TRIPLES PITAGÓRICOS [Media]/python/KevinED11.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit cfa66b9

Please sign in to comment.