-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5238 from KevinED11/main
reto #39 - python
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Retos/Reto #39 - TRIPLES PITAGÓRICOS [Media]/python/KevinED11.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |