Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 26 Mar, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 N
void 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 Code
int main()
{
    lli N = 36;
    checkFactors(N);
    return 0;
}

Java




// Java implementation of the
// above approach
import java.util.*;
 
class GFG{
 
// Function to find the count
// of even and odd factors of N
static 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 Code
public 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 N
def 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 Code
if __name__ == '__main__':
     
    N = 36;
 
    checkFactors(N);
 
# This code is contributed by Princi Singh

C#




// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to find the count
// of even and odd factors of N
static 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 Code
public 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 N
function 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>
Output: 
Yes

 

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 square
bool 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 odd
void checkFactors(lli N)
{
    if (isPerfectSquare(N))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    lli N = 36;
 
    checkFactors(N);
    return 0;
}

Java




// Java implementation of the above approach
class GFG{
     
// Function to check if the
// number is a perfect square
static 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 code
public static void main(String[] args)
{
    int N = 36;
 
    checkFactors(N);
}
}
 
// This code is contributed by dewantipandeydp

Python3




# Python3 implementation of the above approach
import math
 
# Function to check if the
# number is a perfect square
def 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 odd
def checkFactors(x):
    if (isPerfectSquare(x)):
        print("Yes");
    else:
        print("No");
 
# Driver code
if __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 square
static 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 code
public 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 square
function 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 odd
function 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>
Output: 
Yes

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!