Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 100] are sums of two positive cubes? Run the code.

Problem: How many integers in [1, 100] are sums of two positive cubes? Run the code.
print(len({a ** 3 + b ** 3 for a in range(1, 5) for b in range(a, 5) if a ** 3 + b ** 3 <= 100}))
What is printed?
  1. A. 9
  2. B. 6
  3. C. 15
  4. D. 13

Correct answer: A. 9

Sums with a ≤ b ≤ 4 and value ≤ 100: 2, 9, 16, 28, 35, 54, 65, 72, 91, so 9. Requiring a < b gives 6, counting ordered pairs without the set gives 15, and allowing 0³ adds 4 more for 13.

More Coding Interview Problems questions

Exams that ask this topic