Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code.
Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code.
def prime(n):
return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(sum(1 for p in range(2, 51) if prime(p) and prime(100 - p)))What is printed?- A. 12
- B. 15
- C. 6
- D. 25
Correct answer: C. 6
Pairs: 3+97, 11+89, 17+83, 29+71, 41+59, 47+53, so 6. Counting ordered pairs doubles this to 12, while 15 is just the number of primes up to 50 and 25 the number of primes below 100, neither of which checks the partner 100 − p.
More Coding Interview Problems questions
- Problem: How many integer lattice points (x, y) lie on the circle x^2 + y^2 = 25? Run the code. python print(sum(1 for x in range(-5, 6)…
- Problem: How many integers in [1, 10000] have decimal digits summing to 12? Run the code. python print(sum(1 for x in range(1, 10001) if…
- Problem: What is the length of the longest palindromic subsequence of 'character'? Run the DP. python def lps(s): n = len(s) dp = [[0] n…
- Problem: What is the minimum number of characters to insert to make 'leetcode' a palindrome? Use length minus longest palindromic…
- Problem: How many binary strings of length 6 contain no three consecutive 1s? Run the code. python import itertools print(sum(1 for t in…
- Problem: How many derangements (permutations with no fixed point) of 1..6 exist? Run the code. python from itertools import permutations…