# Check if count of even divisors of N is equal to count of odd divisors

• Last Updated : 22 Mar, 2021

Given a positive integer N, the task is to check whether the count of even divisors and odd divisors of N are equal or not. If they are same then print “YES” else print “NO”.
Examples :

Input: N = 6
Output: YES
Explanation:
Number 6 has four factors:
1, 2, 3, 6,
count of even divisors = 2 (2 and 6)
count of odd divisors = 2 (1 and 3)
Input: N = 9
Output: NO
Explanation:
count of even divisors = 0
count of odd divisors = 3 (1, 3 and 9)

Naive Approach: The naive approach is to find all the divisors of the given number and count the even divisors and odd divisors and check whether they are equal or not. If they are same then print “YES”, else print “NO”.
Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include  using namespace std;   // Function to check if count of even // and odd divisors are equal bool divisorsSame(int n) {     // To store the count of even     // factors and odd factors     int even_div = 0, odd_div = 0;       // Loop till [1, sqrt(N)]     for (int i = 1; i <= sqrt(n); i++) {         if (n % i == 0) {               // If divisors are equal             // add only one             if (n / i == i) {                   // Check for even                 // divisor                 if (i % 2 == 0) {                     even_div++;                 }                   // Odd divisor                 else {                     odd_div++;                 }             }               // Check for both divisor             // i.e., i and N/i             else {                   // Check if i is odd                 // or even                 if (i % 2 == 0) {                     even_div++;                 }                 else {                     odd_div++;                 }                   // Check if N/i is odd                 // or even                 if (n / i % 2 == 0) {                     even_div++;                 }                 else {                     odd_div++;                 }             }         }     }       // Return true if count of even_div     // and odd_div are equals     return (even_div == odd_div); }   // Driver Code int main() {     // Given Number     int N = 6;       // Function Call     if (divisorsSame(N)) {         cout << "Yes";     }     else {         cout << "No";     }     return 0; }

## Java

 // Java code for the above program import java.util.*;   class GFG{   // Function to check if count of  // even and odd divisors are equal static boolean divisorsSame(int n) {           // To store the count of even     // factors and odd factors     int even_div = 0, odd_div = 0;       // Loop till [1, sqrt(N)]     for(int i = 1; i <= Math.sqrt(n); i++)     {        if (n % i == 0)        {                         // If divisors are equal            // add only one            if (n / i == i)            {                                 // Check for even                // divisor                if (i % 2 == 0)                {                    even_div++;                }                                 // Odd divisor                else                {                    odd_div++;                }            }                         // Check for both divisor            // i.e., i and N/i            else            {                                 // Check if i is odd                // or even                if (i % 2 == 0)                {                    even_div++;                }                else                {                    odd_div++;                }                                 // Check if N/i is odd                // or even                if (n / i % 2 == 0)                {                    even_div++;                }                else                {                    odd_div++;                }            }        }     }           // Return true if count of even_div     // and odd_div are equals     return (even_div == odd_div); }   // Driver code public static void main(String[] args) {     // Given number     int N = 6;       // Function call     if (divisorsSame(N))     {         System.out.println("Yes");     }     else     {         System.out.println("No");     } } }   // This code is contributed by offbeat

## Python3

 # Python3 program for the above approach import math   # Function to check if count of even # and odd divisors are equal def divisorsSame(n):       # To store the count of even     # factors and odd factors     even_div = 0; odd_div = 0;       # Loop till [1, sqrt(N)]     for i in range(1, int(math.sqrt(n))):                if (n % i == 0):               # If divisors are equal             # add only one             if (n // i == i):                   # Check for even                 # divisor                 if (i % 2 == 0):                     even_div += 1;                                   # Odd divisor                 else:                     odd_div += 1;               # Check for both divisor             # i.e., i and N/i             else:                   # Check if i is odd                 # or even                 if (i % 2 == 0):                     even_div += 1;                                   else:                     odd_div += 1;                                   # Check if N/i is odd                 # or even                 if (n // (i % 2) == 0):                     even_div += 1;                                   else:                     odd_div += 1;                       # Return true if count of even_div     # and odd_div are equals     return (even_div == odd_div);   # Driver Code   # Given Number N = 6;   # Function Call if (divisorsSame(N) == 0):     print("Yes");   else:     print("No");   # This code is contributed by Code_Mech

## C#

 // C# code for the above program using System; class GFG{   // Function to check if count of  // even and odd divisors are equal static bool divisorsSame(int n) {           // To store the count of even     // factors and odd factors     int even_div = 0, odd_div = 0;       // Loop till [1, sqrt(N)]     for(int i = 1; i <= Math.Sqrt(n); i++)     {         if (n % i == 0)         {                               // If divisors are equal             // add only one             if (n / i == i)             {                                       // Check for even                 // divisor                 if (i % 2 == 0)                 {                     even_div++;                 }                                       // Odd divisor                 else                 {                     odd_div++;                 }             }                               // Check for both divisor             // i.e., i and N/i             else             {                                       // Check if i is odd                 // or even                 if (i % 2 == 0)                 {                     even_div++;                 }                 else                 {                     odd_div++;                 }                                       // Check if N/i is odd                 // or even                 if (n / i % 2 == 0)                 {                     even_div++;                 }                 else                 {                     odd_div++;                 }             }         }     }           // Return true if count of even_div     // and odd_div are equals     return (even_div == odd_div); }   // Driver code public static void Main() {     // Given number     int N = 6;       // Function call     if (divisorsSame(N))     {         Console.Write("Yes");     }     else     {         Console.Write("No");     } } }   // This code is contributed by Akanksha_Rai

## Javascript

 

Output:

Yes

Time Complexity: O(âˆšN), where N is the given number
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe that the numbers whose count of even and odd divisors are equals forms the following Arithmetic Progression

2, 6, 10, 14, 18, 22, …

1. The Kth term of the above series is:

2. Now we have to check whether N is a term of the above series or not by the equation:

=>
=>

1.
2. If the value of K calculated using the above formula is an integer, then N is the number with equal count of even and odd divisors.
3. Else N is not the number with equal count of even and odd divisors.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include  using namespace std;   // Function to check if count of even // and odd divisors are equal bool divisorsSame(int n) {       // If (n-2)%4 is an integer, then     // return true else return false     return (n - 2) % 4 == 0; }   // Driver Code int main() {     // Given Number     int N = 6;       // Function Call     if (divisorsSame(N)) {         cout << "Yes";     }     else {         cout << "No";     }     return 0; }

## Java

 // Java code for the above program import java.util.*;   class GFG{       // Function to check if count of  // even and odd divisors are equal static boolean divisorsSame(int n) {           // If (n-2)%4 is an integer, then     // return true else return false     return (n - 2) % 4 == 0; }   // Driver code public static void main(String[] args) {           // Given number     int N = 6;       // Function call     if (divisorsSame(N))     {         System.out.println("Yes");     }     else     {         System.out.println("No");     }     } }   // This code is contributed by offbeat

## Python3

 # Python3 program for the above approach   # Function to check if count of even # and odd divisors are equal def divisorsSame(n):       # If (n-2)%4 is an integer, then     # return true else return false     return (n - 2) % 4 == 0;   # Driver Code   # Given Number N = 6;   # Function Call if (divisorsSame(N)):     print("Yes"); else:     print("No");   # This code is contributed by Nidhi_biet

## C#

 // C# code for the above program using System; class GFG{       // Function to check if count of  // even and odd divisors are equal static bool divisorsSame(int n) {           // If (n-2)%4 is an integer, then     // return true else return false     return (n - 2) % 4 == 0; }   // Driver code public static void Main() {           // Given number     int N = 6;       // Function call     if (divisorsSame(N))     {         Console.Write("Yes");     }     else     {         Console.Write("No");     }     } }   // This code is contributed by Code_Mech

## Javascript

 

Output:

Yes

Time complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles