ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import math
def ndiv(n):
    c = 0
    for i in range(1, math.isqrt(n) + 1):
        if n % i == 0:
            c += 1 if i * i == n else 2
    return c
print(ndiv(36), ndiv(37), ndiv(1))
  1. A. 9 2 2
  2. B. 8 2 1
  3. C. 9 2 1
  4. D. 9 1 1

Correct answer: C. 9 2 1

Divisors come in pairs i and n // i, which is why the loop adds 2, except when i is the exact square root. 36 has nine divisors, 37 is prime with two, and 1 has one.

More Python Programming questions

Exams that ask this topic