Test for Exactly Three Positive Divisors

Quick Overview

Test for Exactly Three Positive Divisors. Given positive integer n, return whether exactly three positive integers divide it without remainder. Count 1 and n themselves as divisors.

Test for Exactly Three Positive Divisors

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return whether the positive integer `n` has exactly three distinct positive divisors. A divisor divides `n` without remainder; count both 1 and `n` when they are distinct. ```cpp class Solution { public: bool isThree(int n); }; ``` ### Examples **Example 1:** ```text n = 9 Output: true Explanation: The divisors are 1, 3, and 9. ``` **Example 2:** ```text n = 16 Output: false Explanation: The divisors are 1, 2, 4, 8, and 16. ``` ### Constraints - `1 <= n <= 10^4`. ### Clarifying Questions Only positive divisors count; 1 has just one divisor. ```hint Inspect the structure How can divisor pairs be counted without visiting all integers up to n? ``` ### Reasoning checkpoints - Establish the exact number of distinct positive divisors without double-counting any divisor. - Handle `n=1` and account correctly for coinciding divisor-pair members. ### Optional extensions Can you characterize all positive integers that have exactly three positive divisors?

Overview: Test for Exactly Three Positive Divisors. Given positive integer n, return whether exactly three positive integers divide it without remainder. Count 1 and n themselves as divisors.

Bloomberg logo
Bloomberg
May 28, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether the positive integer n has exactly three distinct positive divisors. A divisor divides n without remainder; count both 1 and n when they are distinct.

class Solution {
public:
    bool isThree(int n);
};

Examples

Example 1:

n = 9
Output: true
Explanation: The divisors are 1, 3, and 9.

Example 2:

n = 16
Output: false
Explanation: The divisors are 1, 2, 4, 8, and 16.

Constraints

  • 1 <= n <= 10^4 .

Clarifying Questions Guidance

Only positive divisors count; 1 has just one divisor.

Reasoning checkpoints

  • Establish the exact number of distinct positive divisors without double-counting any divisor.
  • Handle n=1 and account correctly for coinciding divisor-pair members.

Optional extensions

Can you characterize all positive integers that have exactly three positive divisors?