Count numbers with exactly 3 divisors
Last Updated :
03 Oct, 2025
Given a number n, print count of numbers in the range from 1 to n having exactly 3 divisors.
Examples:
Input: n = 16
Output: 2
Explanation: Only 4 and 9 have exactly three divisors.
Input: n = 100
Output: 4
Explanation: 4, 9, 25 and 49 have exactly three divisors.
[Naive Approach] Nested Loop Method O(n^2) and Space O(1)
For each number, it counts how many divisors it has by checking all possible divisors from 1 to that number. If a number has exactly 3 divisors, it is printed
C++
#include <iostream>
#include <vector>
using namespace std;
// function to count number of divisors of n
int exactly3Divisors(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
// number n is divisible by i
if (n % i == 0) {
count++;
}
}
return count;
}
// function to count numbers with
// exactly 3 divisors up to n
int countDivisors(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
if (exactly3Divisors(i) == 3) {
total++;
}
}
return total;
}
int main() {
int n = 100;
int ans = countDivisors(n);
cout << ans << '\n';
return 0;
}
C
#include <stdio.h>
// function to count number of divisors of n
int exactly3Divisors(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
// number n is divisible by i
if (n % i == 0) {
count++;
}
}
return count;
}
// function to count numbers with
// exactly 3 divisors up to n
int countDivisors(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
if (exactly3Divisors(i) == 3) {
total++;
}
}
return total;
}
int main() {
int n = 100;
int ans = countDivisors(n);
printf("%d\n", ans);
return 0;
}
Java
public class GFG{
// function to count number of divisors of n
static int exactly3Divisors(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
// number n is divisible by i
if (n % i == 0) {
count++;
}
}
return count;
}
// function to count numbers with
// exactly 3 divisors up to n
static int countDivisors(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
if (exactly3Divisors(i) == 3) {
total++;
}
}
return total;
}
public static void main(String[] args) {
int n = 100;
int ans = countDivisors(n);
System.out.println(ans);
}
}
Python
# function to count number of divisors of n
def exactly3Divisors(n):
count = 0
for i in range(1, n+1):
# number n is divisible by i
if n % i == 0:
count += 1
return count
# function to count numbers with
# exactly 3 divisors up to n
def countDivisors(n):
total = 0
for i in range(1, n+1):
if exactly3Divisors(i) == 3:
total += 1
return total
if __name__ == "__main__":
n = 100
ans = countDivisors(n)
print(ans)
C#
using System;
class GFG
{
// function to count number of divisors of n
static int exactly3Divisors(int n)
{
int count = 0;
for (int i = 1; i <= n; i++)
{
// number n is divisible by i
if (n % i == 0)
{
count++;
}
}
return count;
}
// function to count numbers with
// exactly 3 divisors up to n
static int countDivisors(int n)
{
int total = 0;
for (int i = 1; i <= n; i++)
{
if (exactly3Divisors(i) == 3)
{
total++;
}
}
return total;
}
static void Main()
{
int n = 100;
int ans = countDivisors(n);
Console.WriteLine(ans);
}
}
JavaScript
// function to count number of divisors of n
function exactly3Divisors(n) {
let count = 0;
for (let i = 1; i <= n; i++) {
// number n is divisible by i
if (n % i === 0) {
count++;
}
}
return count;
}
// function to count numbers with
// exactly 3 divisors up to n
function countDivisors(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
if (exactly3Divisors(i) == 3) {
total++;
}
}
return total;
}
//Driver Code
let n = 100;
let ans = countDivisors(n);
console.log(ans);
[Expected Approach-1] Mathematical Approach
The number having exactly 3 divisors must be a perfect square of a prime number.
- A number with divisors is a number that can be evenly divided by 1, itself, and other integers in between.
- For a number to have exactly 3 divisors, the divisors must be:
- 1 (every number is divisible by 1)
- The number itself (a number is always divisible by itself)
- One other divisor between 1 and the number.
For this to happen, the number must be the square of a prime.
If p is a prime number, then the divisors of p2 (the square of p) will be:
1 (as it's divisible by 1)
p (as it is divisible by p)
p2 (as it is divisible by itself)
Therefore, the only way for a number to have exactly 3 divisors is if it is the square of a prime number. This is because, for any non-square number, there are more than 3 divisors, and for a perfect square of a non-prime number, it would have more than 3 divisors as well. Thus, the number must be the square of a prime, and that's why we specifically look at perfect squares of primes.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<bool> exactly3Divisors(int n)
{
vector<bool> prime(n + 1, true);
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// function to count numbers with
// exactly 3 divisors up to n
int countDivisors(int n ){
int total =0;
vector<bool> prime = exactly3Divisors(n);
for (int i = 2 ; i * i<= n ; i++ ){
if (prime[i] ){
total++;
}
}
return total ;
}
int main(){
int n = 100;
int ans = countDivisors(n);
cout << ans <<'\n';
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
// function to calculate primes up to n
void exactly3Divisors(int n, bool prime[])
{
for (int i = 0; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
}
// function to count numbers with
// exactly 3 divisors up to n
int countDivisors(int n)
{
int total = 0;
bool prime[n + 1];
exactly3Divisors(n, prime);
for (int i = 2; i * i <= n; i++) {
if (prime[i])
total++;
}
return total;
}
int main()
{
int n = 100;
int ans = countDivisors(n);
printf("%d\n", ans);
return 0;
}
Java
import java.util.*;
public class GFG {
// function to calculate primes up to n
static boolean[] exactly3Divisors(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// function to count numbers with
// exactly 3 divisors up to n
static int countDivisors(int n) {
int total = 0;
boolean[] prime = exactly3Divisors(n);
for (int i = 2; i * i <= n; i++) {
if (prime[i])
total++;
}
return total;
}
public static void main(String[] args) {
int n = 100;
int ans = countDivisors(n);
System.out.println(ans);
}
}
Python
# function to calculate primes up to n
def exactly3Divisors(n):
prime = [True] * (n + 1)
prime[0] = prime[1] = False
for p in range(2, int(n**0.5) + 1):
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
return prime
# function to count numbers with
# exactly 3 divisors up to n
def countDivisors(n):
total = 0
prime = exactly3Divisors(n)
for i in range(2, int(n**0.5) + 1):
if prime[i]:
total += 1
return total
if __name__ == "__main__":
n = 100
ans = countDivisors(n)
print(ans)
C#
using System;
using System.Collections.Generic;
class GFG
{
// function to calculate primes up to n
static bool[] exactly3Divisors(int n)
{
bool[] prime = new bool[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++)
{
if (prime[p])
{
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// function to return List of numbers with
// exactly 3 divisors up to n
static int countDivisors(int n)
{
int total = 0;
bool[] prime = exactly3Divisors(n);
for (int i = 2; i * i <= n; i++)
{
if (prime[i])
total++;
}
return total;
}
static void Main()
{
int n = 100;
int ans = countDivisors(n);
Console.Write(ans);
}
}
JavaScript
// function to calculate primes up to n
function exactly3Divisors(n) {
let prime = Array(n + 1).fill(true);
prime[0] = prime[1] = false;
for (let p = 2; p * p <= n; p++) {
if (prime[p]) {
for (let i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// function to count numbers with
// exactly 3 divisors up to n
function countDivisors(n) {
let total = 0;
let prime = exactly3Divisors(n);
for (let i = 2; i * i <= n; i++) {
if (prime[i])
total++;
}
return total;
}
// Driver Code
let n = 100;
let ans = countDivisors(n);
console.log(ans);
PHP
<?php
// function to calculate primes up to n
function exactly3Divisors($n) {
$prime = array_fill(0, $n + 1, true);
$prime[0] = $prime[1] = false;
for ($p = 2; $p * $p <= $n; $p++) {
if ($prime[$p]) {
for ($i = $p * 2; $i <= $n; $i += $p)
$prime[$i] = false;
}
}
return $prime;
}
// function to count numbers with
// exactly 3 divisors up to n
function countDivisors($n) {
$total = 0;
$prime = exactly3Divisors($n);
for ($i = 2; $i * $i <= $n; $i++) {
if ($prime[$i])
$total++;
}
return $total;
}
$n = 100;
$ans = countDivisors($n);
echo $ans . "\n";
?>
Time Complexity: O(n*log(log(n)))
Auxiliary Space: O(n)
[Expected Approach-2] Using Constant Space
In this method, we iterate through all numbers up to sqrt(n) and check if each is prime. If a number is prime, its square will have exactly three divisors (1, the prime itself, and its square). We count such primes whose squares are ≤ n. This approach avoids using extra space like a sieve, but has a time complexity of approximately O(n^(3/4)), because we perform a primality check (which takes up to O(√i) time) for each number up to √n.
Run a loop from 2 to sqrt(n) and check if the current element is prime or not, if it is so then print that number, but this method will increase the time complexity of the solution
Step for implementation:
- Start a loop for integer i from 2 to n.
- Check if i is prime or not, which can be done easily using the isPrime(n) method.
- If i is prime, check if its square is less than or equal to the given number. This will be reviewed only for squares of prime numbers, therefore reducing the number of checks.
- If the above condition is satisfied, the number will be printed and the loop will continue till i <= n.
C++
#include <iostream>
#include <vector>
using namespace std;
// Checks if n is prime
bool isPrime(int n)
{
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Generates all numbers with 3 divisors and returns them
int exactly3Divisors(int n)
{
int total = 0;
for (int i = 2; i * i <= n; i++) {
// Check prime
if (isPrime(i)) {
total++;
}
}
return total;
}
int main()
{
int n = 100;
cout << exactly3Divisors(n) <<'\n';
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
// Checks if n is prime
bool isPrime(int n)
{
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Generates all numbers with 3 divisors and returns count
int exactly3Divisors(int n)
{
int total = 0;
for (int i = 2; i * i <= n; i++) {
// Check prime
if (isPrime(i)) {
total++;
}
}
return total;
}
int main()
{
int n = 100;
printf("%d\n", exactly3Divisors(n));
return 0;
}
Java
public class GFG {
// Checks if n is prime
static boolean isPrime(int n)
{
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Generates all numbers with 3 divisors and returns count
static int exactly3Divisors(int n)
{
int total = 0;
for (int i = 2; i * i <= n; i++) {
// Check prime
if (isPrime(i)) {
total++;
}
}
return total;
}
public static void main(String[] args)
{
int n = 100;
System.out.println(exactly3Divisors(n));
}
}
Python
# Checks if n is prime
def isPrime(n):
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
# Generates all numbers with 3
# divisors and returns count
def exactly3Divisors(n):
total = 0
for i in range(2, int(n**0.5)+1):
# Check prime
if isPrime(i):
total += 1
return total
if __name__ =="__main__":
n = 100
print(exactly3Divisors(n))
C#
using System;
class GFG
{
// Checks if n is prime
static bool IsPrime(int n)
{
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Generates all numbers with 3 divisors and returns count
static int Exactly3Divisors(int n)
{
int total = 0;
for (int i = 2; i * i <= n; i++) {
// Check prime
if (IsPrime(i)) {
total++;
}
}
return total;
}
static void Main()
{
int n = 100;
Console.WriteLine(Exactly3Divisors(n));
}
}
JavaScript
// Checks if n is prime
function isPrime(n) {
for (let i = 2; i * i <= n; i++) {
if (n % i === 0)
return false;
}
return true;
}
// Generates all numbers with 3 divisors and returns count
function exactly3Divisors(n) {
let total = 0;
for (let i = 2; i * i <= n; i++) {
// Check prime
if (isPrime(i)) {
total++;
}
}
return total;
}
//Driver Code
let n = 100;
console.log(exactly3Divisors(n));
Time Complexity: O(n3/4) because we call isPrime √n times, and for each i, isPrime(i) takes O(√i) time. Therefore, total time is the sum √1 + √2 + ... + √√n, which integrates to O(n3/4)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem