Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIJava Program to Check if count of divisors is even or odd

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

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.*/


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




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

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

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?