Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many two-digit numbers have a digit sum that is a prime number? Run the code.

Problem: How many two-digit numbers have a digit sum that is a prime number? Run the code.
def isp(n):
    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(sum(1 for x in range(10, 100) if isp(sum(map(int, str(x))))))
What is printed?
  1. A. 37
  2. B. 21
  3. C. 34
  4. D. 33

Correct answer: D. 33

Two-digit numbers with digit sum 2, 3, 5, 7, 11, 13 or 17 number 33. Including 1..9 adds 4 for 37, 21 counts two-digit primes, and treating digit sum 1 (the number 10) as prime gives 34.

More Coding Interview Problems questions

Exams that ask this topic