Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 30 have a^2 + b^2 a perfect square? Run the code.

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 30 have a^2 + b^2 a perfect square? Run the code.
print(sum(1 for a in range(1, 31) for b in range(1, 31) if int(round((a * a + b * b) ** 0.5)) ** 2 == a * a + b * b))
What is printed?
  1. A. 26
  2. B. 24
  3. C. 28
  4. D. 30

Correct answer: A. 26

Pairs such as (3,4), (4,3), (6,8) and (5,12) give Pythagorean triples; the count of ordered pairs is 26.

More Coding Interview Problems questions

Exams that ask this topic