Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many integers in [1, 100] have exactly two prime factors counted with multiplicity? Run the code.

Problem: How many integers in [1, 100] have exactly two prime factors counted with multiplicity? Run the code.
def omega(n):
    c = 0
    d = 2
    while d * d <= n:
        while n % d == 0:
            n //= d
            c += 1
        d += 1
    return c + (1 if n > 1 else 0)
print(sum(1 for x in range(2, 101) if omega(x) == 2))
What is printed?
  1. A. 56
  2. B. 34
  3. C. 30
  4. D. 74

Correct answer: B. 34

Ω(n) = 2 covers semiprimes p·q and squares p², 34 numbers up to 100. Counting two distinct prime factors gives 56, only squarefree semiprimes give 30, and Ω ≥ 2 gives 74.

More Coding Interview Problems questions

Exams that ask this topic