Home >Backend Development >C++ >The number of factors of the product of N numbers
The divisor of a number is a number that can divide it without any remainder. In other words, the divisor of a number n is the number that gives n when multiplied by any other integer. It can also be called a factor of a number.
Dividend ÷ Divisor = Quotient.
For example, if we divide 5 by 60, we will get 12 and vice versa, therefore, 12 and 60 can be considered as divisors of 60.
The given task is to find the number of divisors of the product of given numbers. Let us understand this problem through an example.
Suppose we are 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
One way to achieve this is to find the divisors using the modulo (%) operator and count them by iterating from 1 to product.
The modulo operator (%) operator is used to obtain the remainder of a division operation. If the remainder of a division is zero, it means that the dividend is divisible by the divisor. For example, (30 % 5) is 0, so 30 is divisible by 5.
Calculate the number of divisors of the product of all numbers in an array.
Multiply all the numbers in the array using the Multiplication operator and store the result in a variable named product.
Use the modulo operator, from 1 to Product, divide Product by each number and get the remainder.
Create a variable count, and if the remainder is 0, increment the count variable.
The following program calculates the number of divisors of the product of a given number −
#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; }
Number of divisors: 40
Note−For larger numbers, this method is very inefficient. Since the numbers are large, the product will also be large. This will result in a large number of iterations, increasing time complexity.
If N is a composite number, then
N = x<sup>a</sup> * y<sup>b</sup> * z<sup>c</sup>
Where a, b and c are prime factors, then the number of divisors of N is given by the following formula
(a + 1)(b + 1)(c + 1)
We will use the above concepts to find the number of divisors of the product of N numbers.
Multiply all N numbers and store the result in a variable named product.
Iterate a for loop from 2 until the square root, product.
Get the prime factors of the product. To do this, we use the modulo operator to check if product is divisible by the current value of x. If possible, x is stored as a prime factor and count is stored as a power of the prime factor.
Use
If there are any remaining prime factors, store them as well.
Calculate the divisors by iterating from 0 to the number of prime factors and using the above formula.
The following is a program to find the number of factors of a given product of 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; }
Number of divisors: 40
We can also find the product of all N numbers through nested loops. In the outer loop, we need to iterate through all the numbers from 1 to product. Within this range of numbers we will find all possible divisors. In the nested loop, we will calculate the number of divisors for each number and its multiples.
The Chinese translation of#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; }
Number of divisors: 40
We have discussed different ways to find the number of divisors of a product of N numbers, including using the modulo operator, prime factorization, nested loops, and more. For larger numbers, we cannot use the modulo operator efficiently. In order to obtain optimized results, we can use prime factorization and nested loops.
The above is the detailed content of The number of factors of the product of N numbers. For more information, please follow other related articles on the PHP Chinese website!