Java Program to Check if count of divisors is even or odd
Last Updated :
16 Apr, 2024
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))
Java
// 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.*/
OutputThe 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
// 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. */
OutputThe 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:
- Perform prime factorization of the given number.
- If the prime factorization results in prime factors raised to even powers, then the count of divisors is odd because each divisor is formed by choosing a combination of these prime factors, and having even powers ensures that every divisor will have a pair to make the count odd.
- If any prime factor is raised to an odd power, then the count of divisors will be even because the divisors will not have pairs to form a complete set.
Java
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
OutputThe 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!
Similar Reads
Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7,
7 min read
Java Program to Check if a Given Number is Perfect Number A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to
5 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Java Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
3 min read
Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E
3 min read
Java Program to Store Even & Odd Elements of an Array into Separate Arrays Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers i
2 min read
Java Program to Find the Frequency of Odd & Even Numbers in the Matrix Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix. The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is
3 min read
Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java Given an array arr[] of size N, Convert the given array as even numbers in the array occurs at the odd index, odd numbers in the array occur at the even index, and whether the sum of the numbers in an array is divisible by the size of an array. If the array follows all the properties then an array i
3 min read
First element that appears even number of times in an array Given an array, find the first element that appears even number of times in the array. It returns the element if exists otherwise returns 0. Examples: Input : arr[] = {1, 5, 4, 7, 4, 1, 5, 7, 1, 5}; Output : 4 Explanation, 4 is the first element that appears even number of times. Input : arr[] = {2,
7 min read