Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the largest prime below 1000? Run the code.

Problem: What is the largest prime below 1000? Run the code.
def isp(n):
    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(max(p for p in range(2, 1000) if isp(p)))
What is printed?
  1. A. 999
  2. B. 989
  3. C. 997
  4. D. 1009

Correct answer: C. 997

997 has no divisor up to 31. 999 = 27 · 37, 989 = 23 · 43 (it slips past a check that stops at 19), and 1009 is the first prime above 1000.

More Coding Interview Problems questions

Exams that ask this topic