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?- A. 325
- B. 327
- C. 175
- 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
- Problem: What is the length of the longest subarray of [2, 4, 6, 3, 8, 10, 12] whose elements are all even? Run the code. python a = [2…
- Problem: How many distinct values does floor(100 / i) take for i = 1 to 100? Run the code. python print(len({100 // i for i in range(1…
- Problem: What is the number of ways to write 20 as an ordered sum of the parts 1 and 2 (compositions)? Run the DP. python f = [1, 1] for i…
- Problem: How many 4-digit numbers have all digits equal to or greater than the digit before them (non-decreasing)? Run the code. python…
- Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code. python a = [3, 10, 5, 25…
- Problem: How many subarrays of [1, -1, 1, -1, 1] have sum 0? Run the prefix-count code. python from collections import Counter a = [1, -1…