Three Divisors

The problem is asking to check if the given integer n has exactly three positive divisors.

Let’s solve this problem step by step:

  1. If an integer n has exactly three divisors, it must be a square of a prime number. This is because a prime number p only has two positive divisors (1 and itself), and p^2 will add one more divisor which is p, so it has three positive divisors in total.

  2. So, we first need to check if the given number n is a perfect square.

  3. Then, we need to check if the square root of n is a prime number.

Here’s the Python code implementing the above steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def isThree(self, n: int) -> bool:
        p2 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}

        # Calculate the square root of 'n'
        square_root = int(n ** 0.5)

        # Check if 'n' is a perfect square
        if square_root ** 2 != n:
            return False

        # Check if square root of 'n' is a prime number
        if square_root not in p2:
            return False

        return True

This code first calculates the square root of n and checks if n is a perfect square. If n is not a perfect square, it returns False. Then it checks if the square root of n is a prime number. If the square root of n is not a prime number, it returns False. If it passed both checks, the function returns True.