CSES Solutions – Counting Divisors
Last Updated :
10 Apr, 2024
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)
Share your thoughts in the comments
Please Login to comment...