Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many integers x in [1, 500] have x^2 + x + 41 prime? Run the code.

Problem: How many integers x in [1, 500] have x^2 + x + 41 prime? 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(1, 501) if isp(x * x + x + 41)))
What is printed?
  1. A. 325
  2. B. 327
  3. C. 175
  4. D. 40

Correct answer: A. 325

The direct check finds 325 prime values for x = 1..500. A trial-division bound of int(√n) without + 1 misses squares of primes and gives 327, 175 counts the composites, and 40 is the famous run of primes for x = 0..39 only.

More Coding Interview Problems questions

Exams that ask this topic