Open In App

Java Program to Check if count of divisors is even or odd

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. The time complexity for such a solution would be O(sqrt(n)) 

// 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 program
    public static void main(String args[])
    {
        System.out.println("The count of divisor:");
        countDivisors(10);
    }
}
/* This code is contributed by Nikita Tiwari.*/

Output
The count of divisor:
Even

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)

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. 

// 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 program to test above function */
    public static void main(String args[])
        throws IOException
    {
        System.out.println("The count of "
                           + "divisors of 10 is: ");

        countDivisors(10);
    }
}

/* This code is contributed by Nikita Tiwari. */

Output
The count of divisors of 10 is: 
Even

Time Complexity: O(log n), as the inbuilt sqrt() function is being used
Auxiliary Space: O(1), If we consider a recursive call stack then it would be O(log(n))

Prime Factorization Method:

import java.util.*;

public class Main {
    
    // Function to perform prime factorization
    static Map<Integer, Integer> primeFactors(int n) {
        Map<Integer, Integer> factors = new HashMap<>();
        for (int i = 2; i * i <= n; i++) {
            while (n % i == 0) {
                factors.put(i, factors.getOrDefault(i, 0) + 1);
                n /= i;
            }
        }
        if (n > 1) {
            factors.put(n, factors.getOrDefault(n, 0) + 1);
        }
        return factors;
    }
    
    // Function to check if count of divisors is even or odd
    static String countDivisors(int n) {
        Map<Integer, Integer> factors = primeFactors(n);
        int total = 1;
        for (int count : factors.values()) {
            total *= (count + 1);
        }
        return total % 2 == 0 ? "Even" : "Odd";
    }
    
    // Main method
    public static void main(String[] args) {
        System.out.println("The count of divisors of 10 is: " + countDivisors(10));
    }
}
// Nikunj Sonigara

Output
The count of divisors of 10 is: Even

Time Complexity: O(log n),

Auxiliary Space: O(1)

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

Article Tags :