Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the sum of all distinct prime factors of 2025? Run the code.

Problem: What is the sum of all distinct prime factors of 2025? Run the code.
n = 2025
f = 2
ps = set()
while f * f <= n:
    while n % f == 0:
        ps.add(f)
        n //= f
    f += 1
if n > 1:
    ps.add(n)
print(sum(ps))
What is printed?
  1. A. 22
  2. B. 15
  3. C. 6
  4. D. 8

Correct answer: D. 8

2025 = 3⁴ · 5², so the distinct primes are 3 and 5, sum 8. Adding each prime with multiplicity gives 22, multiplying them gives 15, and adding the exponents gives 6.

More Coding Interview Problems questions

Exams that ask this topic