- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Number of divisors of product of N numbers
Divisor of a number is which divides it exactly without any remainders. In other words, a divisor of a number n is the one which results in n when multiplied by any other integer. It can also be called as a factor of a number.
Dividend ÷ Divisor = Quotient.
For example, if we divide 60 with 5 we will get 12 and vice versa therefore, 12 and 60 can be considered as divisors of 60.
Number of Divisors of Product of N Numbers
Given task is to find the number of divisors of the product of the given numbers. Let us understand this with the help of an example.
Suppose we have given the numbers 6, 6 and, 10. The product of these numbers is 120 and the divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120. Therefore, the output should be 16.
Input: 6, 2, 10 Output: 16
Using Modulo Operator
One way to achieve this is to find the divisors using modulo (%) operator and count them by iterating from 1 to product.
The Modulo operator (%) operator is used to get the remainder of a division operation. If remainder of a division is zero, then it means that the dividend is divisible by the divisor. For example, (30 % 5) is 0, hence 30 is divisible by 5.
To count the number of divisors of the product of an array of numbers.
Multiply all the numbers of the array using multiplication operator and store the value in variable named product.
Using the modulo operator, from 1 to Product divide the product with each number and get the remainder.
Create a variable count and if the remainder is 0 increment the count variable.
Example
Following program counts the number of divisors of the product of the given numbers −
#include <iostream> using namespace std; // Define a function for finding the number int findNumberOfDivisors(int arr[], int N) { // Multiply all the numbers in the array int product = 1; for (int x = 0; x < N; x++) { product *= arr[x]; } // Count the divisors int count = 0; for (int x = 1; x <= product; x++) { if (product % x == 0) { count++; } } return count; } int main() { // Declaration of the numbers and N int numbers[] = { 12, 16, 40 }; int N = sizeof(numbers) / sizeof(numbers[0]); int divisors = findNumberOfDivisors(numbers, N); std::cout << "Number of divisors: " << divisors; return 0; }
Output
Number of divisors: 40
Note − This approach is highly inefficient for larger numbers. Due to the large numbers, the product will be large. This will result in large number of iterations, increasing the time complexity.
Using Prime Factorization
If N is a composite number such that
N = xa * yb * zc
Where a, b and c are prime factors, then number of divisors of N is given by
(a + 1)(b + 1)(c + 1)
We will use the above concept to find the number of divisors of product of N numbers.
Algorithm/Steps
Multitply all the N numbers and store it a variable named product.
Iterate a for loop from 2 to square root of the product.
Get the prime factors of the product. To do so, we use modulo operator to check if product is divisible by the current value of x. If so, x is stored as prime factor while the count is stored as power of the prime factor.
Store the prime factor and its exponent in vector containers- primeFactor and power by using the <vector> library and the push_back() function.
If there are any remaining prime factors, store them too.
Count the divisors by iterating from 0 to number of prime factors and using the above-mentioned formula.
Example
Following is the program to find the number of divisors of the product of given numbers using the prime factorization method −
#include <iostream> #include <vector> #include <cmath> // Multiply all the N numbers int findNumberOfDivisors(int arr[], int N) { int product = 1; for (int x = 0; x < N; x++) { product *= arr[x]; } std::vector<int> primeFactor; std::vector<int> power; // Check if x is divisor of product // Store the prime factor and exponent in the vector container for (int x = 2; x <= sqrt(product); x++) { if (product % x == 0) { int count = 0; while (product % x == 0) { product /= x; count++; } primeFactor.push_back(x); power.push_back(count); } } // Store the remaining prime factor (if present) if (product > 1) { primeFactor.push_back(product); power.push_back(1); } // Count the number of divisors int divisorsCount = 1; for (int x = 0; x < primeFactor.size(); x++) { divisorsCount *= (power[x] + 1); } return divisorsCount; } int main() { int numbers[] = {12, 16, 40}; // Calculate the number of elements in the array int N = sizeof(numbers) / sizeof(numbers[0]); int divisors = findNumberOfDivisors(numbers, N); std::cout << "Number of divisors: " << divisors << std::endl; return 0; }
Output
Number of divisors: 40
Using Nested Loops
We can also find the product of all the N numbers with the help of nested loops. In the outer loop we need to iterate all the numbers from 1 to product. In this range of numbers, we will find all the possible divisors. In the nested loop, we will count the number of divisors for each number and its multiples.
Example
#include <iostream> #include <vector> int findNumberOfDivisors(int arr[], int N) { std::vector<int> divisorsCount(11000, 0); // Multiply all the N numbers int product = 1; for (int x = 0; x < N; x++) { product *= arr[x]; } // Count of divisors for (int x = 1; x <= product; x++) { for (int y = x; y <= product; y += x) { divisorsCount[y]++; } } return divisorsCount[product]; } int main() { int numbers[] = {12, 16, 40}; int N = sizeof(numbers) / sizeof(numbers[0]); int divisors = findNumberOfDivisors(numbers, N); std::cout << "Number of divisors: " << divisors << std::endl; return 0; }
Output
Number of divisors: 40
Conclusion
We have discussed different methods for finding the number of divisors of a product of N numbers including using modulo operator, prime factorization, nested loops etc., We cannot use the modulo operator for larger numbers efficiently. For getting optimized results, we may use the prime factorization and nested loops approach.