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)?

  1. A. 223
  2. B. 129
  3. C. 172
  4. 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

Exams that ask this topic