CSES Solutions - Counting Divisors
Last Updated :
23 Jul, 2025
Given N integers as an array arr[], your task is to report for each integer the number of its divisors.
For example, if x=18, the correct answer is 6 because its divisors are 1,2,3,6,9,18.
Examples:
Input: N = 3, arr[] = {16, 17, 18}
Output:
5
2
6
Explanation:
- There are 5 divisors of 16: 1, 2, 4, 8 and 16.
- There are 2 divisors of 17: 1 and 17.
- There are 6 divisors of 18: 1, 2, 3, 6, 9 and 18.
Input: N = 3, arr[] = {5, 6, 7}
Output:
2
4
2
Explanation:
- There are 2 divisors of 5: 1 and 5.
- There are 4 divisors of 6: 1, 2, 3 and 6.
- There are 2 divisors of 7: 1 and 7.
Approach: To solve the problem, follow the below idea:
The problem can be solved by Precomputing the number of divisors for each number up to 1,000,004. Then, uses a nested loop to iterate over each number and its multiples, incrementing a counter for each multiple. This results in an array where the value at each index is the number of divisors for that index.
Step-by-step algorithm:
- Create an array numDivisors to store the number of divisors for each number up to 1000004.
- Iterate through each number i from 1 to 1000004.
- For each i, iterate through multiples of i from j = i to 1000004.
- Increment numDivisors[j] by 1 for each multiple.
Below is the implementation of the algorithm:
C++
#include <iostream>
using namespace std;
// Function to calculate the number of divisors for each
// number up to 1000004
void calculateDivisors(long long numDivisors[])
{
for (int i = 1; i < 1000005; i++) {
for (int j = i; j < 1000005; j += i) {
numDivisors[j]++;
}
}
}
int main()
{
// Array to store the number of divisors for each number
// up to 1000004
long long numDivisors[1000005] = {};
// Call the function to calculate the number of divisors
calculateDivisors(numDivisors);
// Read the number of integers
long long n = 3;
long long arr[] = {16, 17, 18};
for(int i=0; i < n; i++) {
cout << numDivisors[arr[i]] << "\n";
}
}
Java
public class Main {
// Function to calculate the number of divisors for each
// number up to 1000004
static void calculateDivisors(long[] numDivisors) {
for (int i = 1; i < 1000005; i++) {
for (int j = i; j < 1000005; j += i) {
numDivisors[j]++;
}
}
}
public static void main(String[] args) {
// Array to store the number of divisors for each number
// up to 1000004
long[] numDivisors = new long[1000005];
// Call the function to calculate the number of divisors
calculateDivisors(numDivisors);
// Read the number of integers
int n = 3;
long[] arr = {16, 17, 18};
for (int i = 0; i < n; i++) {
System.out.println(numDivisors[(int)arr[i]]);
}
}
}
Python3
def calculate_divisors(num_divisors):
for i in range(1, 1000005):
for j in range(i, 1000005, i):
num_divisors[j] += 1
def main():
# Array to store the number of divisors for each number
# up to 1000004
num_divisors = [0] * 1000005
# Call the function to calculate the number of divisors
calculate_divisors(num_divisors)
# Read the number of integers
n = 3
arr = [16, 17, 18]
for num in arr:
print(num_divisors[num])
if __name__ == "__main__":
main()
JavaScript
// Function to calculate the number of divisors for each
// number up to 1000004
function calculateDivisors(numDivisors) {
for (let i = 1; i < 1000005; i++) {
for (let j = i; j < 1000005; j += i) {
numDivisors[j]++;
}
}
}
// Array to store the number of divisors for each number
// up to 1000004
let numDivisors = new Array(1000005).fill(0);
// Call the function to calculate the number of divisors
calculateDivisors(numDivisors);
// Read the number of integers
let n = 3;
let arr = [16, 17, 18];
for (let i = 0; i < n; i++) {
console.log(numDivisors[arr[i]]);
}
Time complexity: O(N * log(max(arr[i]))), where N is the count of numbers given as input array arr[].
Auxiliary Space: O(N)
Similar Reads
Count numbers up to N having exactly 5 divisors Given a positive integer N, the task is to count the number of integers from the range [1, N] having exactly 5 divisors. Examples: Input: N = 18Output: 1Explanation:From all the integers over the range [1, 18], 16 is the only integer that has exactly 5 divisors, i.e. 1, 2, 8, 4 and 16.Therefore, the
15+ min read
POTD Solutions | 21 Octâ 23 | Sum of all divisors from 1 to n View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of mathematics but will also help you build up problem-sol
4 min read
Count of Multiples of A ,B or C less than or equal to N Given four integers N, A, B and C. The task is to find the count of integers from the range [1, N] which are divisible by either A, B or C. Examples: Input: A = 2, B = 3, C = 5, N = 10 Output: 8 2, 3, 4, 5, 6, 8, 9 and 10 are the only number from the range [1, 10] which are divisible by either 2, 3
7 min read
Check if a number exists with X divisors out of which Y are composite Given two integers X and Y representing the total number of divisors and the number of composite divisors respectively, the task is to check if there exists an integer N which has exactly X divisors and Y are composite numbers. Examples: Input: X = 6, Y = 3 Output: YES Explanation: N = 18 is such a
7 min read
Count of nodes having odd divisors in the given subtree for Q queries Given a N-ary Tree and Q queries where each query contains a node of the N-ary tree, the task is to count the number of nodes that have an odd number of divisors in the subtree for Q queries. Examples: Input: Output: 1 3 0 1 Explanation: Query 1: In the subtree rooted at node 100, there is only one
8 min read
Count number of integers less than or equal to N which has exactly 9 divisors Given a number n, find total number of integers less than equal to n which have exactly 9 divisors.Examples: Input: n = 100 Output: 2 Explanation: The two numbers which have exactly 9 divisors are 36 and 100. Input: n = 200Output: 3 Explanation: The numbers are 36, 100, 196.Table of Content[Naive Ap
9 min read