Day 15 - Python

Problem Statement: Pythagorean triplets

You are given a range of integers [start, end]. Your task is to find all Pythagorean triplets (a, b, c) within this range where start <= a <= b <= c <= end. A Pythagorean triplet is a set of three positive integers (a, b, c) such that a^2 + b^2 = c^2.

Write a Python program that takes two integers start and end as input and outputs the count of Pythagorean triplets along with the list of those triplets, using list comprehension.

Write a Python function with the following signature to solve this problem:

def pythagorean_triplets(start: int, end: int) -> list:

    """

    Finds all Pythagorean triplets within the range [start, end].

    Args:

    - start (int): The start of the range.

    - end (int): The end of the range.


    Returns:

    - list: A list containing tuples of Pythagorean triplets.

    """

Input:

The input consists of a single line containing two integers separated by space: start and end (1 <= start <= end <= 1000).

Output:

The output should consist of two parts:

The first line should contain a single integer representing the count of Pythagorean triplets within the range [start, end].

The subsequent lines should contain the Pythagorean triplets separated by space.

Sample Input:

3 10

Sample Output:

2

(3, 4, 5)

(6, 8, 10)

Explanation:

For the given input range [3, 10], there are two Pythagorean triplets:

(3, 4, 5): 3^2 + 4^2 = 5^2

(6, 8, 10): 6^2 + 8^2 = 10^2

Note: The output format represents the count of Pythagorean triplets followed by each triplet in a separate line. Use list comprehension to solve this problem.

Test Cases

1 30

11

(3, 4, 5)

(5, 12, 13)

(6, 8, 10)

(7, 24, 25)

(8, 15, 17)

(9, 12, 15)

(10, 24, 26)

(12, 16, 20)

(15, 20, 25)

(18, 24, 30)

(20, 21, 29)


7 18

2

(8, 15, 17)

(9, 12, 15)

SOLUTION:

def pythagorean_triplets(start, end):

    return [(a, b, c) for a in range(start, end + 1)

            for b in range(a, end + 1)

            for c in range(b, end + 1)

            if a ** 2 + b ** 2 == c ** 2]

val = input()

s, e = map(int, val.split())

result = pythagorean_triplets(s, e)

print(len(result))

for i in result:

  print(i)

Insights:

  • The function generates Pythagorean triplets within the range [start, end], where start and end are inclusive.
  • It uses list comprehension to generate all possible combinations of a, b, and c within the given range.
  • The list comprehension involves three nested loops iterating over a, b, and c, respectively. This generates all combinations of values for the triplets.
  • The ranges of b and c are restricted to start from the current value of a. This ensures that b and c are greater than or equal to a. It applies a condition within the list comprehension to check if the current combination (a, b, c) satisfies the Pythagorean theorem a^2 + b^2 = c^2.
  • For each valid combination satisfying the Pythagorean theorem, a tuple (a, b, c) is constructed and added to the resulting list.
Enjoy Pythagoras Theorem! 
Happy Coding! :)


Comments