Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: What is the sum of the smallest prime factors of the integers 2 through 30 (each integer contributes its smallest prime divisor)?
- A. 223
- B. 129
- C. 172
- D. 174
Correct answer: D. 174
A smallest-prime-factor sieve gives spf(n) for 2..30: 15 even numbers contribute 2 each, and the rest add 3s, 5s and the primes themselves, totalling 174. Summing the LARGEST prime factors gives 223, summing only the primes up to 30 gives 129, and stopping at 29 drops spf(30) = 2 (172).
More Coding Interview Problems questions
- Problem: How many positive divisors does the number 2^10 × 3^4 have?
- Problem: What are the last two decimal digits of 3^200?
- Problem: What is the maximum value of a XOR b over all pairs (a, b) taken from the array [3, 10, 5, 25, 2, 8] (a and b may be equal)?
- Problem: The minimum number of characters to delete from "(()))(" to make the parentheses balanced (every ( has a matching ) after it) is…
- Problem: Selection sort is applied to [4, 3, 2, 1] and the number of element moves (assignments into the array) is counted as one per…
- Problem: In a 3-row Pascal-like grid, a cell holds the number of ways to reach it moving only right or down from (0,0), with a blocked…