Data Structures & AlgorithmsModerateTCS NQT · number theory

Problem: What is the product of the distinct prime factors of 360? Run the code.

Problem: What is the product of the distinct prime factors of 360? Run the code.
n = 360
p, prod = 2, 1
while p * p <= n:
    if n % p == 0:
        prod *= p
        while n % p == 0:
            n //= p
    p += 1
if n > 1:
    prod *= n
print(prod)
What is printed?
  1. A. 60
  2. B. 360
  3. C. 30
  4. D. 15

Correct answer: C. 30

The distinct primes of 360 = 2^3 * 3^2 * 5 are 2, 3 and 5, whose product is 30. Multiplying the full prime powers gives 360.

More Coding Interview Problems questions

Exams that ask this topic