# Count divisors of array multiplication

Given an array with N elements, task is to find the count of factors of a number X which is product of all array elements.

Examples:

```Input : 5 5
Output : 3
5 * 5 = 25, the factors of 25 are 1, 5, 25
whose count is 3

Input : 3 5 7
Output : 8
3 * 5 * 7 = 105, the factors of 105 are 1,
3, 5, 7, 15, 21, 35, 105 whose count is 8
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Simple but causes overflow)
1. Multiply all the elements of the array.
2. Count divisors in the number obtained after multiplication.

 `// A simple C++ program to count divisors ` `// in array multiplication. ` `#include ` `using` `namespace` `std; ` ` `  `// To count number of factors in a number ` `int` `counDivisors(``int` `X) ` `{ ` `    ``// Initialize count with 0 ` `    ``int` `count = 0; ` `    ``// Increment count for every factor ` `    ``// of the given number X. ` `    ``for` `(``int` `i = 1; i <= X; ++i) { ` `        ``if` `(X % i == 0) { ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// Return number of factors ` `    ``return` `count; ` `} ` ` `  `// Returns number of divisors in array ` `// multiplication ` `int` `countDivisorsMult(``int` `arr[], ``int` `n) ` `{ ` `    ``// Multipliying all elements of ` `    ``// the given array. ` `    ``int` `mul = 1; ` `    ``for` `(``int` `i = 0; i < n; ++i)  ` `        ``mul *= arr[i]; ` `     `  `    ``// Calling function which count number of factors ` `    ``// of the number ` `    ``return` `counDivisors(mul); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 4, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``cout << countDivisorsMult(arr, n) << endl; ` `    ``return` `0; ` `} `

 `// A simple Java program to count divisors ` `// in array multiplication. ` ` `  `class` `GFG ` `{ ` `    ``// To count number of factors in a number ` `    ``static` `int` `counDivisors(``int` `X) ` `    ``{ ` `        ``// Initialize count with 0 ` `        ``int` `count = ``0``; ` `         `  `        ``// Increment count for every factor ` `        ``// of the given number X. ` `        ``for` `(``int` `i = ``1``; i <= X; ++i)  ` `        ``{ ` `            ``if` `(X % i == ``0``) { ` `                ``count++; ` `            ``} ` `        ``} ` `     `  `        ``// Return number of factors ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Returns number of divisors in array ` `    ``// multiplication ` `    ``static` `int` `countDivisorsMult(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// Multipliying all elements of ` `        ``// the given array. ` `        ``int` `mul = ``1``; ` `        ``for` `(``int` `i = ``0``; i < n; ++i)  ` `            ``mul *= arr[i]; ` `         `  `        ``// Calling function which count  ` `        ``// number of factors of the number ` `        ``return` `counDivisors(mul); ` `    ``} ` `     `  `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `arr[] = { ``2``, ``4``, ``6` `}; ` `        ``int` `n = arr.length; ` `        ``System.out.println(countDivisorsMult(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

 `# A simple Python program ` `# to count divisors ` `# in array multiplication. ` ` `  `# To count number of ` `# factors in a number ` `def` `counDivisors(X): ` ` `  `    ``# Initialize count with 0 ` `    ``count ``=` `0` `    ``# Increment count for ` `    ``# every factor ` `    ``# of the given number X. ` `    ``for` `i ``in` `range``(``1``, X ``+` `1``): ` `        ``if` `(X ``%` `i ``=``=` `0``):  ` `            ``count ``+``=` `1` `  `  `    ``# Return number of factors ` `    ``return` `count ` `  `  `# Returns number of ` `# divisors in array ` `# multiplication ` `def` `countDivisorsMult(arr, n): ` ` `  `    ``# Multipliying all elements of ` `    ``# the given array. ` `    ``mul ``=` `1` `    ``for` `i ``in` `range``(n):  ` `        ``mul ``*``=` `arr[i] ` `      `  `    ``# Calling function which ` `    ``# count number of factors ` `    ``# of the number ` `    ``return` `counDivisors(mul) ` ` `  `# Driver code ` ` `  `arr ``=` `[ ``2``, ``4``, ``6` `] ` `n ``=``len``(arr) ` ` `  `print``(countDivisorsMult(arr, n)) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

 `// C# program to count divisors ` `// in array multiplication. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// To count number of factors  ` `    ``// in a number ` `    ``static` `int` `counDivisors(``int` `X) ` `    ``{ ` `         `  `        ``// Initialize count with 0 ` `        ``int` `count = 0; ` `         `  `        ``// Increment count for every ` `        ``// factor of the given  ` `        ``// number X. ` `        ``for` `(``int` `i = 1; i <= X; ++i)  ` `        ``{ ` `            ``if` `(X % i == 0) { ` `                ``count++; ` `            ``} ` `        ``} ` `     `  `        ``// Return number of factors ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Returns number of divisors in ` `    ``// array multiplication ` `    ``static` `int` `countDivisorsMult( ` `                    ``int` `[]arr, ``int` `n) ` `    ``{ ` `         `  `        ``// Multipliying all elements  ` `        ``// of the given array. ` `        ``int` `mul = 1; ` `         `  `        ``for` `(``int` `i = 0; i < n; ++i)  ` `            ``mul *= arr[i]; ` `         `  `        ``// Calling function which  ` `        ``// count number of factors  ` `        ``// of the number ` `        ``return` `counDivisors(mul); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `         `  `        ``int` `[]arr = { 2, 4, 6 }; ` `        ``int` `n = arr.Length; ` `         `  `        ``Console.Write( ` `         ``countDivisorsMult(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// nitin mittal. `

 ` `

Output:

```10
```

Method 2 (Avoids overflow)
1. Find maximum element in array
1. Find prime numbers smaller than the maximum element
3. Find the number of overall occurrences of each prime factor in whole array by traversing all array elements and finding their prime factors. We use hashing to count occurrences.
4. Let the counts of occurrences of prime factors be a1, a2, …aK, if we have K distinct prime factors, then the answer will be: (a1+1)(a2+1)(…)*(aK+1).

 `// C++ program to count divisors in array multiplication. ` `#include ` `using` `namespace` `std; ` ` `  ` `  `void` `SieveOfEratosthenes(``int` `largest, vector<``int``> &prime) ` `{ ` `    ``// Create a boolean array "isPrime[0..n]" and initialize ` `    ``// all entries it as true. A value in isPrime[i] will ` `    ``// finally be false if i is Not a isPrime, else true. ` `    ``bool` `isPrime[largest+1]; ` `    ``memset``(isPrime, ``true``, ``sizeof``(isPrime)); ` ` `  `    ``for` `(``int` `p=2; p*p<=largest; p++) ` `    ``{ ` `        ``// If isPrime[p] is not changed, then it is a isPrime ` `        ``if` `(isPrime[p] == ``true``) ` `        ``{ ` `            ``// Update all multiples of p ` `            ``for` `(``int` `i=p*2; i<=largest; i += p) ` `                ``isPrime[i] = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// Print all isPrime numbers ` `    ``for` `(``int` `p=2; p<=largest; p++) ` `        ``if` `(isPrime[p]) ` `            ``prime.push_back(p); ` `} ` ` `  `// Returns number of divisors in array ` `// multiplication ` `int` `countDivisorsMult(``int` `arr[], ``int` `n) ` `{ ` `    ``// Find all prime numbers smaller than ` `    ``// the largest element. ` `    ``int` `largest = *max_element(arr, arr+n); ` `    ``vector<``int``> prime; ` `    ``SieveOfEratosthenes(largest, prime); ` ` `  `    ``// Find counts of occurrences of each prime ` `    ``// factor ` `    ``unordered_map<``int``, ``int``> mp; ` `    ``for` `(``int` `i=0; i 1 && arr[i]%prime[j] ==0 ) ` `            ``{ ` `                ``arr[i] /= prime[j]; ` `                ``mp[prime[j]]++; ` `            ``} ` `        ``} ` `        ``if` `(arr[i] != 1) ` `            ``mp[arr[i]]++; ` `    ``} ` ` `  `    ``// Compute count of all divisors using counts ` `    ``// prime factors. ` `    ``long` `long` `int` `res = 1; ` `    ``for` `(``auto` `it : mp) ` `       ``res *= (it.second + 1L); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 4, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``cout << countDivisorsMult(arr, n) << endl; ` `    ``return` `0; ` `} `

Output:

```10
```

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.