gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
ind all Pythagorean triples whose short sides are numbers smaller than 10. use filter…
- User
ind all Pythagorean triples whose short sides are numbers smaller than 10. use
filter and comprehension.
- Assistant
To find all Pythagorean triples with short sides smaller than 10, we can use a list comprehension with a filter to check if the sum of the squares of the sides is a perfect square. Here's the code:
from math import sqrt pythagorean_triples = [(a, b, int(sqrt(a**2 + b**2))) for a in range(1, 10) for b in range(a, 10) if sqrt(a**2 + b**2).is_integer()] print(pythagorean_triples)Output:
[(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]