Given a number “n”, find its total number of divisors is even or odd.

Examples :

Input : n = 10 Output : Even Input: n = 100 Output: Odd Input: n = 125 Output: Even

A **naive approach** would be to find all the divisors and then see if the total number of divisors is even or odd.

Time complexity for such a solution would be O(sqrt(n))

`// Naive Solution to ` `// find if count of ` `// divisors is even ` `// or odd ` `#include <math.h> ` `#include <stdio.h> ` ` ` `// Function to count ` `// the divisors ` `void` `countDivisors(` `int` `n) ` `{ ` ` ` `// Initialize count ` ` ` `// of divisors ` ` ` `int` `count = 0; ` ` ` ` ` `// Note that this ` ` ` `// loop runs till ` ` ` `// square root ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(n) + 1; i++) { ` ` ` `if` `(n % i == 0) ` ` ` ` ` `// If divisors are ` ` ` `// equal increment ` ` ` `// count by one ` ` ` `// Otherwise increment ` ` ` `// count by 2 ` ` ` `count += (n / i == i) ? 1 : 2; ` ` ` `} ` ` ` ` ` `if` `(count % 2 == 0) ` ` ` `printf` `(` `"Even\n"` `); ` ` ` ` ` `else` ` ` `printf` `(` `"Odd\n"` `); ` `} ` ` ` `/* Driver program to test above function */` `int` `main() ` `{ ` ` ` `printf` `(` `"The count of divisor: "` `); ` ` ` `countDivisors(10); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The count of divisor: Even

**Efficient Solution: **

We can observe that the number of divisors is odd only in case of perfect squares. Hence the best solution would be to check if the given number is perfect square or not. If it’s a perfect square, then the number of divisors would be odd, else it’d be even.

`// C++ program for ` `// Efficient Solution to find ` `// if count of divisors is ` `// even or odd ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find if count ` `// of divisors is even or ` `// odd ` `void` `countDivisors(` `int` `n) ` `{ ` ` ` `int` `root_n = ` `sqrt` `(n); ` ` ` ` ` `// If n is a perfect square, ` ` ` `// then it has odd divisors ` ` ` `if` `(root_n * root_n == n) ` ` ` `printf` `(` `"Odd\n"` `); ` ` ` `else` ` ` `printf` `(` `"Even\n"` `); ` `} ` ` ` `/* Driver program to test above function */` `int` `main() ` `{ ` ` ` `cout << ` `"The count of divisors of 10 is: \n"` `; ` ` ` ` ` `countDivisors(10); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The count of divisors of 10 is: Even

Please refer complete article on Check if count of divisors is even or odd for more details!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a number has an odd count of odd divisors and even count of even divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Java Program to Check if count of divisors is even or odd
- Check if count of divisors is even or odd
- Count of integers in a range which have even number of odd digits and odd number of even digits
- 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
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Count of nodes having odd divisors in the given subtree for Q queries
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Check whether count of odd and even factors of a number are equal
- Lex program to check whether input number is odd or even
- C program to check whether a given number is even or odd
- Find numbers with K odd divisors in a given range
- Minimum cost to convert M to N by repeated addition of its even divisors
- Minimum repeated addition of even divisors of N required to convert N to M
- Check whether a given number is even or odd