Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 9 make a b a perfect square? Run the code.

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 9 make a * b a perfect square? Run the code.
print(sum(1 for a in range(1, 10) for b in range(1, 10) if int(round((a * b) ** 0.5)) ** 2 == a * b))
What is printed?
  1. A. 17
  2. B. 13
  3. C. 8
  4. D. 9

Correct answer: A. 17

Ordered digit pairs with a square product number 17 (e.g. (1,4), (2,8), (3,3), (4,9)). Unordered pairs give 13, excluding a = b gives 8, and requiring both digits to be squares gives 9.

More Coding Interview Problems questions

Exams that ask this topic