Check if a number has an odd count of odd divisors and even count of even divisors

Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors.
Examples:
Input: N = 36
Output: Yes
Explanation:
Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36
Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even]Input: N = 28
Output: No
Naive Approach: The idea is to find the factors of the number N and count the odd factors of N and even factors of N. Finally, check if the count of odd factors is odd and count of even factors is even.
Below is the implementation of the above approach:
C++
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std;#define lli long long int// Function to find the count// of even and odd factors of Nvoid checkFactors(lli N){ lli ev_count = 0, od_count = 0; // Loop runs till square root for (lli i = 1; i <= sqrt(N) + 1; i++) { if (N % i == 0) { if (i == N / i) { if (i % 2 == 0) ev_count += 1; else od_count += 1; } else { if (i % 2 == 0) ev_count += 1; else od_count += 1; if ((N / i) % 2 == 0) ev_count += 1; else od_count += 1; } } } // Condition to check if the even // factors of the number N is // is even and count of // odd factors is odd if (ev_count % 2 == 0 && od_count % 2 == 1) cout << "Yes" << endl; else cout << "No" << endl;}// Driver Codeint main(){ lli N = 36; checkFactors(N); return 0;} |
Java
// Java implementation of the// above approachimport java.util.*;class GFG{// Function to find the count// of even and odd factors of Nstatic void checkFactors(long N){ long ev_count = 0, od_count = 0; // Loop runs till square root for(long i = 1; i <= Math.sqrt(N) + 1; i++) { if (N % i == 0) { if (i == N / i) { if (i % 2 == 0) ev_count += 1; else od_count += 1; } else { if (i % 2 == 0) ev_count += 1; else od_count += 1; if ((N / i) % 2 == 0) ev_count += 1; else od_count += 1; } } } // Condition to check if the even // factors of the number N is // is even and count of // odd factors is odd if (ev_count % 2 == 0 && od_count % 2 == 1) System.out.print("Yes" + "\n"); else System.out.print("No" + "\n");}// Driver Codepublic static void main(String[] args){ long N = 36; checkFactors(N);}}// This code is contributed by amal kumar choubey |
Python3
# Python3 implementation of the# above approach# Function to find the count# of even and odd factors of Ndef checkFactors(N): ev_count = 0; od_count = 0; # Loop runs till square root for i in range(1, int(pow(N, 1 / 2)) + 1): if (N % i == 0): if (i == N / i): if (i % 2 == 0): ev_count += 1; else: od_count += 1; else: if (i % 2 == 0): ev_count += 1; else: od_count += 1; if ((N / i) % 2 == 0): ev_count += 1; else: od_count += 1; # Condition to check if the even # factors of the number N is # is even and count of # odd factors is odd if (ev_count % 2 == 0 and od_count % 2 == 1): print("Yes" + ""); else: print("No" + "");# Driver Codeif __name__ == '__main__': N = 36; checkFactors(N);# This code is contributed by Princi Singh |
C#
// C# implementation of the// above approachusing System;class GFG{// Function to find the count// of even and odd factors of Nstatic void checkFactors(long N){ long ev_count = 0, od_count = 0; // Loop runs till square root for(long i = 1; i <= Math.Sqrt(N) + 1; i++) { if (N % i == 0) { if (i == N / i) { if (i % 2 == 0) ev_count += 1; else od_count += 1; } else { if (i % 2 == 0) ev_count += 1; else od_count += 1; if ((N / i) % 2 == 0) ev_count += 1; else od_count += 1; } } } // Condition to check if the even // factors of the number N is // is even and count of // odd factors is odd if (ev_count % 2 == 0 && od_count % 2 == 1) Console.Write("Yes" + "\n"); else Console.Write("No" + "\n");}// Driver Codepublic static void Main(String[] args){ long N = 36; checkFactors(N);}}// This code is contributed by Amit Katiyar |
Javascript
<script>// JavaScript implementation of the// above approach// Function to find the count// of even and odd factors of Nfunction checkFactors(N){ let ev_count = 0, od_count = 0; // Loop runs till square root for (let i = 1; i <= Math.sqrt(N) + 1; i++) { if (N % i == 0) { if (i == Math.floor(N / i)) { if (i % 2 == 0) ev_count += 1; else od_count += 1; } else { if (i % 2 == 0) ev_count += 1; else od_count += 1; if (Math.floor(N / i) % 2 == 0) ev_count += 1; else od_count += 1; } } } // Condition to check if the even // factors of the number N is // is even and count of // odd factors is odd if (ev_count % 2 == 0 && od_count % 2 == 1) document.write("Yes" + "<br>"); else document.write("No" + "<br>");}// Driver Code let N = 36; checkFactors(N);// This code is contributed by Surbhi Tyagi.</script> |
Yes
Time Complexity: O(N(1/2))
Auxiliary Space: O(1)
Efficient Approach: The key observation in the problem is that the number of odd divisors is odd and number of even divisors is even only in case of perfect squares. Hence, the best solution would be to check if the given number is a perfect square or not. If it’s a perfect square, then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std;#define lli long long int// Function to check if the// number is a perfect squarebool isPerfectSquare(long double x){ long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0);}// Function to check// if count of even divisors is even// and count of odd divisors is oddvoid checkFactors(lli N){ if (isPerfectSquare(N)) cout << "Yes" << endl; else cout << "No" << endl;}// Driver Codeint main(){ lli N = 36; checkFactors(N); return 0;} |
Java
// Java implementation of the above approach public class GFG{ // Function to check if the // number is a perfect squarestatic boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Function to check if count of// even divisors is even and count// of odd divisors is odd static void checkFactors(int x) { if (isPerfectSquare(x)) System.out.print("Yes"); else System.out.print("No"); } // Driver codepublic static void main(String[] args) { int N = 36; checkFactors(N); } }// This code is contributed by dewantipandeydp |
Python3
# Python3 implementation of the above approachimport math# Function to check if the# number is a perfect squaredef isPerfectSquare(x): # Find floating povalue of # square root of x. sr = pow(x, 1 / 2); # If square root is an integer return ((sr - math.floor(sr)) == 0);# Function to check if count of# even divisors is even and count# of odd divisors is odddef checkFactors(x): if (isPerfectSquare(x)): print("Yes"); else: print("No");# Driver codeif __name__ == '__main__': N = 36; checkFactors(N);# This code is contributed by sapnasingh4991 |
C#
// C# implementation of the above approach using System;class GFG{ // Function to check if the // number is a perfect squarestatic bool isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0); } // Function to check if count of// even divisors is even and count// of odd divisors is odd static void checkFactors(int x) { if (isPerfectSquare(x)) Console.Write("Yes"); else Console.Write("No"); } // Driver codepublic static void Main(String[] args) { int N = 36; checkFactors(N); } }// This code is contributed by Amit Katiyar |
Javascript
<script>// Javascript implementation of the// above approach// Function to check if the// number is a perfect squarefunction isPerfectSquare(x){ sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0);}// Function to check// if count of even divisors is even// and count of odd divisors is oddfunction checkFactors(N){ if (isPerfectSquare(N)) document.write("Yes" + "<br>"); else document.write("No" + "<br>");}// Driver Code N = 36; checkFactors(N);// This code is contributed by Mayank Tyagi</script> |
Yes
Time Complexity: O(log(N)) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



