# Count of the non-prime divisors of a given number

Given a number **N**, the task is to find the count of non-prime divisors of the given number N.

**Examples:**

Input:N = 8

Output:3

Explanation:

Divisors of 8 are – {1, 2, 4, 8}

Non-Prime Divisors – {1, 4, 8}

Input:N = 20

Output:4

Explanation:

Divisors of 20 are – {1, 2, 4, 5, 10, 20}

Non-Prime Divisors – {1, 4, 10, 20}

**Approach:** The key observation in the problem is that any number can be written as product of its prime factors as where is the count of the prime factors of the given number. Using permutation and combination we can find that the total count of the prime factors that is .

Therefore, the count of the non-prime factors will be:

Below is the implementation of the above approach:

## C++

`// C++ program to find count of ` `// non-prime divisors of given number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to factors of the given ` `// number ` `vector<` `int` `> getFactorization(` `int` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` `vector<` `int` `> v; ` ` ` ` ` `// Loop to find the divisors of ` ` ` `// the number 2 ` ` ` `while` `(x % 2 == 0) { ` ` ` `count++; ` ` ` `x = x / 2; ` ` ` `} ` ` ` `if` `(count != 0) ` ` ` `v.push_back(count); ` ` ` ` ` `// Loop to find the divisors of the ` ` ` `// given number upto SQRT(N) ` ` ` `for` `(` `int` `i = 3; i <= ` `sqrt` `(x); i += 2) { ` ` ` `count = 0; ` ` ` `while` `(x % i == 0) { ` ` ` `count++; ` ` ` `x /= i; ` ` ` `} ` ` ` `if` `(count != 0) ` ` ` `v.push_back(count); ` ` ` `} ` ` ` ` ` `// Condition to check if the rest ` ` ` `// number is also a prime number ` ` ` `if` `(x > 1) { ` ` ` `v.push_back(1); ` ` ` `} ` ` ` `return` `v; ` `} ` ` ` `// Function to find the non-prime ` `// divisors of the given number ` `int` `nonPrimeDivisors(` `int` `N) ` `{ ` ` ` `vector<` `int` `> v = getFactorization(N); ` ` ` `int` `ret = 1; ` ` ` ` ` `// Loop to count the number of ` ` ` `// the total divisors of given number ` ` ` `for` `(` `int` `i = 0; i < v.size(); i++) ` ` ` `ret = ret * (v[i] + 1); ` ` ` ` ` `ret = ret - v.size(); ` ` ` `return` `ret; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `// Function Call ` ` ` `cout << nonPrimeDivisors(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find count of ` `# non-prime divisors of given number ` `from` `math ` `import` `sqrt ` ` ` `# Function to factors of the given ` `# number ` `def` `getFactorization(x): ` ` ` ` ` `count ` `=` `0` ` ` `v ` `=` `[] ` ` ` ` ` `# Loop to find the divisors of ` ` ` `# the number 2 ` ` ` `while` `(x ` `%` `2` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` `x ` `=` `x ` `/` `/` `2` ` ` ` ` `if` `(count !` `=` `0` `): ` ` ` `v.append(count) ` ` ` ` ` `# Loop to find the divisors of the ` ` ` `# given number upto SQRT(N) ` ` ` `for` `i ` `in` `range` `(` `3` `, ` `int` `(sqrt(x)) ` `+` `12` `): ` ` ` `count ` `=` `0` ` ` ` ` `while` `(x ` `%` `i ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` `x ` `/` `/` `=` `i ` ` ` ` ` `if` `(count !` `=` `0` `): ` ` ` `v.append(count) ` ` ` ` ` `# Condition to check if the rest ` ` ` `# number is also a prime number ` ` ` `if` `(x > ` `1` `): ` ` ` `v.append(` `1` `) ` ` ` ` ` `return` `v ` ` ` `# Function to find the non-prime ` `# divisors of the given number ` `def` `nonPrimeDivisors(N): ` ` ` ` ` `v ` `=` `getFactorization(N) ` ` ` `ret ` `=` `1` ` ` ` ` `# Loop to count the number of ` ` ` `# the total divisors of given number ` ` ` `for` `i ` `in` `range` `(` `len` `(v)): ` ` ` `ret ` `=` `ret ` `*` `(v[i] ` `+` `1` `) ` ` ` `ret ` `=` `ret ` `-` `len` `(v) ` ` ` ` ` `return` `ret ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `8` ` ` ` ` `# Function Call ` ` ` `print` `(nonPrimeDivisors(N)) ` ` ` `# This code is contributed by Samarth ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Count of Fibonacci divisors of a given number
- Count all perfect divisors of a number
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count the numbers < N which have equal number of divisors as K
- Count of square free divisors of a given number
- Check if a number has prime count of divisors
- Count all pairs of divisors of a number N whose sum is coprime with N
- Check if count of even divisors of N is equal to count of odd divisors
- Count elements in the given range which have maximum number of divisors
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Program to find count of numbers having odd number of divisors in given range
- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Count Divisors of n in O(n^1/3)
- Check if count of divisors is even or odd
- Count Divisors of Factorial
- Count divisors of n that have at-least one digit common with n
- C Program to Check if count of divisors is even or odd
- Count divisors of array multiplication
- Count total divisors of A or B in a given range

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.