Check if count of divisors is even or odd

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

Examples :

Input  : n = 10      
Output : Even

Input:  n = 100
Output: Odd

Input:  n = 125
Output: Even

We strongly recommend that you click here and practice it, before moving on to the solution.

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))

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Code
int main()
{
    printf("The count of divisor: ");
    countDivisors(10);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Naive Solution to find if count 
// of divisors is even or odd
  
import java.io.*;
import java.math.*;
  
class GFG 
{
  
    // Function to count
    // the divisors
    static void countDivisors(int n)
    {
        // Initialize count
        // of divisors
        int count = 0;
  
        // Note that this
        // loop runs till
        // square root
        for (int i = 1; i <= Math.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)
            System.out.println("Even");
  
        else
            System.out.println("Odd");
    }
  
    // Driver Code
    public static void main(String args[])
    {
        System.out.print("The count of divisor: ");
        countDivisors(10);
    }
}
// This code is contributed by Nikita Tiwari

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Naive Solution to find if count  
# of divisors is even or odd 
import math
  
# Function to count 
# the divisors
def countDivisors(n) :
      
    # Initialize count 
    # of divisors
    count = 0
  
    # Note that this loop 
    # runs till square 
    # root
    for i in range(1, (int)(math.sqrt(n)) + 2) :
        if (n % i == 0) :
              
            # If divisors are
            # equal, increment
            # count by one
            # Otherwise increment
            # count by 2
            if( n // i == i) :
                count = count + 1
            else :
                count = count + 2
  
    if (count % 2 == 0) :
        print("Even")
    else :
        print("Odd")
  
  
# Driver Code
print("The count of divisor: ")
countDivisors(10)
  
# This code is contributed by Nikita Tiwari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program using Naive
// Solution to find if
// count of divisors
// is even or odd
using System;
  
class GFG {
  
    // Function to count
    // the divisors
    static void countDivisors(int n)
    {
        // Initialize count
        // of divisors
        int count = 0;
  
        // Note that this
        // loop runs till
        // square root
        for (int i = 1; i <= Math.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)
            Console.Write("Even");
  
        else
            Console.Write("Odd");
    }
  
    // Driver code
    public static void Main()
    {
        Console.Write("The count of divisor: ");
        countDivisors(10);
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Naive Solution to 
// find if count of 
// divisors is even
// or odd
  
// Function to count
// the divisors
function countDivisors($n)
{
      
    // Initialize count 
    // of divisors
    $count = 0;
  
    // Note that this 
    // loop runs till
    // square root
    for ($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)
        echo "Even\n";
          
    else
        echo "Odd\n";
}
  
    // Driver Code
    echo "The count of divisor: ";
    countDivisors(10);
      
// This code is contributed by ajit.
?>

chevron_right



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.

Below is the implementation of above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Code
int main()
{
    cout << "The count of divisors"
         << " of 10 is: ";
  
    countDivisors(10);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for Efficient 
// Solution to find if count of 
// divisors is even or odd
import java.io.*;
import java.math.*;
  
class GFG
{
  
    // Function to find if count
    // of divisors is even or
    // odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.sqrt(n));
  
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            System.out.println("Odd");
  
        else
            System.out.println("Even");
    }
  
    // Driver code
    public static void main(String args[])
        throws IOException
    {
        System.out.print("The count of"
                    "divisors of 10 is: ");
  
        countDivisors(10);
    }
}
  
// This code is contributed by Nikita Tiwari

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program for
# Efficient Solution to find
# find if count of divisors
# is even or odd
  
def NumOfDivisor(n):
    if n < 1:
        return
    root_n = n**0.5
      
    # If n is a perfect square,
    # then it has odd divisors
    if root_n**2 == n:
        print('Odd')
    else:
        print('Even')
          
# Driver code     
if __name__ == '__main__':
    print("The count of divisor"+
          "of 10 is: ")
    NumOfDivisor(10)
      
# This code is contributed by Yt R    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for efficient
// solution to find of
// count of divisors is
// even or odd
using System;
  
class GFG {
  
    // Function to find if
    // count of divisors
    // is even or odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.Sqrt(n));
  
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            Console.WriteLine("Odd");
  
        else
            Console.WriteLine("Even");
    }
  
    // Driver code
    public static void Main()
    {
        Console.Write("The count of divisors : ");
  
        countDivisors(10);
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php program for Efficient
// Solution to find if count of 
// divisors is even or odd
  
// Function to find if count
// of divisors is even or
// odd
  
function countDivisors($n)
{
    $root_n = sqrt($n);
  
    // If n is a perfect square,
    // then it has odd divisors
    if ($root_n * $root_n == $n)
        echo "Odd\n";
    else
        echo "Even\n";
}
  
// Driver Code
echo "The count of divisors of 10 is: ";
  
countDivisors(10);
  
// This code is contributed by ajit
?>

chevron_right



Output :

The count of divisor: Even

This article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, jit_t



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.