Skip to main content

Three Divisors - Solution & Explanation

EasyMathEnumerationNumber Theory7 min readAsked at: Microsoft, Google, Bloomberg
Practice this problem

Problem Statement

Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.

An integer m is a divisor of n if there exists an integer k such that n = k * m.

 

Example 1:

Input: n = 2
Output: false
Explantion: 2 has only two divisors: 1 and 2.

Example 2:

Input: n = 4
Output: true
Explantion: 4 has three divisors: 1, 2, and 4.

 

Constraints:

  • 1 <= n <= 104

Approach Overview

Problem Overview: You are given an integer n. The task is to determine whether it has exactly three positive divisors. A number normally has at least two divisors (1 and itself). Only a very specific class of numbers has exactly three.

The key mathematical observation: a number has exactly three divisors if and only if it is the square of a prime number. For example, 4 = 2^2 has divisors 1, 2, 4. Similarly, 9 = 3^2 has 1, 3, 9. This turns the problem into checking whether n is a perfect square and whether its square root is prime.

Approach 1: Optimized Trial Division (O(sqrt(n)) time, O(1) space)

The direct way is to count divisors of n. Iterate from 1 to sqrt(n). For every divisor i, count both i and n / i. Stop early if the count exceeds three. This avoids scanning all numbers up to n and limits work to the square root range. Although the algorithm still checks divisibility repeatedly, it works well for small inputs and clearly demonstrates divisor enumeration using enumeration. Time complexity is O(sqrt(n)) with constant O(1) space.

Approach 2: Prime Square Divisors Check (O(sqrt(n)) time, O(1) space)

This approach uses the mathematical property directly. First compute root = sqrt(n). If root * root != n, the number cannot have exactly three divisors because it is not a perfect square. If it is a perfect square, check whether root is prime by running trial division up to sqrt(root). A prime square guarantees exactly three divisors: 1, the prime itself, and the square. This reduces the divisor counting work significantly and relies on concepts from math and number theory. The overall time complexity remains O(sqrt(n)), but the constant factor is much smaller because only the square root candidate is tested for primality.

Recommended for interviews: Interviewers expect the prime-square observation. Brute divisor counting shows you understand factors, but recognizing that exactly three divisors implies n = p^2 demonstrates stronger mathematical reasoning and pattern recognition. The prime-square check is the cleanest and most efficient solution.

Approach 1: Prime Square Divisors Check

The key observation here is that a number n will have exactly three positive divisors if and only if n is a perfect square of a prime number. If n = p^2, then its divisors are 1, p, and p^2, which makes three divisors. To solve this problem, first check if n is a perfect square. If it is, then check if the square root is a prime number.

First, we compute the integer square root of n. If squaring this value doesn't yield n, then n isn't a perfect square, and it cannot have exactly three divisors. Next, we check if this square root is a prime number by attempting to divide it with numbers up to its square root. If it is only divisible by itself and 1, it is prime.

Code

Python

C++

Complexity

Time Complexity: O(sqrt(sqrt(n))) due to the prime checking process.
Space Complexity: O(1) as only a constant amount of space is used.

Try this approach in the editor →

Approach 2: Optimized Trial Division

Directly counting divisors would involve iterating potentially through all numbers up to n. This can be optimized. We realize that every divisor less than sqrt(n) corresponds to a divisor greater than sqrt(n). Thus, we can iterate only up to sqrt(n) and manage a divisor count effectively.

We iterate through all numbers up to sqrt(n). If i divides n, then n is divisible by both i and n/i, unless i == n/i, in which case it counts as one divisor. We count the divisors and return true if the count equals three.

Code

Java

C

Complexity

Time Complexity: O(sqrt(n)) due to iterating only up to the square root of n.
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Prime Square Divisors Check

Time Complexity: O(sqrt(sqrt(n))) due to the prime checking process.
Space Complexity: O(1) as only a constant amount of space is used.

Optimized Trial Division

Time Complexity: O(sqrt(n)) due to iterating only up to the square root of n.
Space Complexity: O(1).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Optimized Trial DivisionO(sqrt(n))O(1)Useful when demonstrating divisor enumeration or when mathematical insight is not immediately obvious
Prime Square Divisors CheckO(sqrt(n))O(1)Best approach when using number theory insight that numbers with exactly three divisors are squares of primes

Video Solution

Three Divisors |LeetCode 1952(Easy)| English • CodeClips with Abhishek Ranjan • 3,495 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Three Divisors easy or hard?
Three Divisors is classified as an easy problem. The main challenge is recognizing the mathematical insight that only prime squares have exactly three divisors. Once that observation is made, the implementation becomes straightforward.
How to solve Three Divisors in O(n)?
A naive method could iterate from 1 to n and count divisors, which leads to O(n) time. This approach is inefficient because most checks are unnecessary. The optimized solution reduces work to O(sqrt(n)) by leveraging the fact that numbers with exactly three divisors must be prime squares.
What is the best approach for Three Divisors?
The best approach is checking whether n is a square of a prime number. Compute sqrt(n), verify that it is an integer, then check if the square root is prime. This works because only prime squares have exactly three divisors. The complexity is O(sqrt(n)) due to the primality check.
Is Three Divisors asked at Google/Amazon/Meta?
Three Divisors is a typical easy-level screening question used in coding interviews and online assessments. Variations involving divisor counts or prime properties appear at companies like Amazon, Google, and Microsoft to test basic number theory and reasoning skills.
What data structure is used in Three Divisors?
No special data structure is required. The problem relies on mathematical properties and simple arithmetic operations such as square root calculation and divisibility checks. It primarily tests number theory concepts rather than data structure usage.
What is the time complexity of Three Divisors?
The optimal solution runs in O(sqrt(n)) time and O(1) space. You compute the square root and check whether that value is prime using trial division up to sqrt(root). Brute divisor enumeration also takes O(sqrt(n)) but performs more unnecessary checks.
Three Divisors Python or Java solution approach?
Both Python and Java implementations follow the same logic: compute sqrt(n), check if n is a perfect square, then verify that the square root is prime using trial division up to sqrt(root). The algorithm runs in O(sqrt(n)) time and uses constant extra space.

Ready to solve this problem?

Practice Three Divisors with our built-in code editor and test cases.

Practice on FleetCode