Data Structures & AlgorithmsModerateTCS NQT · number theory
Problem: What is the sum of all primes up to 50? Run the code.
Problem: What is the sum of all primes up to 50? Run the code.
print(sum(p for p in range(2, 51) if all(p % d for d in range(2, int(p ** 0.5) + 1))))What is printed?
- A. 275
- B. 304
- C. 330
- D. 328
Correct answer: D. 328
The primes up to 50 are 2, 3, 5, ..., 47, and their sum is 328. Including 1 or excluding 2 gives the nearby wrong totals.
More Coding Interview Problems questions
- Problem: What is C(20, 10) mod 1000? Compute it with Pascal's triangle reduced mod 1000. Run the code. python n, k = 20, 10 C = [[0] (k +…
- Problem: How many distinct palindromes can be formed by rearranging the letters of 'aabb' (distinct strings, all letters used)? Run the…
- Problem: What is the minimum number of deletions to make the string 'abcca' a palindrome? Use the palindromic-subsequence DP and print…
- Problem: Find the next lexicographic permutation of the digits '1243' (the smallest permutation strictly greater than it). Run the code…
- Problem: Find the length of the longest substring of 'AABABBA' that can be made all one letter after at most k = 1 replacement. Run the…
- Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code. python A, B = 29, 15 print(bin(A ^ B).count('1')) What…