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++
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
void checkFactors(lli N)
{
lli ev_count = 0, od_count = 0;
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;
}
}
}
if (ev_count % 2 == 0
&& od_count % 2 == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
lli N = 36;
checkFactors(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkFactors( long N)
{
long ev_count = 0 , od_count = 0 ;
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 ;
}
}
}
if (ev_count % 2 == 0 && od_count % 2 == 1 )
System.out.print( "Yes" + "\n" );
else
System.out.print( "No" + "\n" );
}
public static void main(String[] args)
{
long N = 36 ;
checkFactors(N);
}
}
|
Python3
def checkFactors(N):
ev_count = 0 ; od_count = 0 ;
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 ;
if (ev_count % 2 = = 0 and
od_count % 2 = = 1 ):
print ( "Yes" + "");
else :
print ( "No" + "");
if __name__ = = '__main__' :
N = 36 ;
checkFactors(N);
|
C#
using System;
class GFG{
static void checkFactors( long N)
{
long ev_count = 0, od_count = 0;
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;
}
}
}
if (ev_count % 2 == 0 && od_count % 2 == 1)
Console.Write( "Yes" + "\n" );
else
Console.Write( "No" + "\n" );
}
public static void Main(String[] args)
{
long N = 36;
checkFactors(N);
}
}
|
Javascript
<script>
function checkFactors(N)
{
let ev_count = 0, od_count = 0;
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;
}
}
}
if (ev_count % 2 == 0
&& od_count % 2 == 1)
document.write( "Yes" + "<br>" );
else
document.write( "No" + "<br>" );
}
let N = 36;
checkFactors(N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
void checkFactors(lli N)
{
if (isPerfectSquare(N))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
lli N = 36;
checkFactors(N);
return 0;
}
|
Java
public class GFG{
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static void checkFactors( int x)
{
if (isPerfectSquare(x))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int N = 36 ;
checkFactors(N);
}
}
|
Python3
import math
def isPerfectSquare(x):
sr = pow (x, 1 / 2 );
return ((sr - math.floor(sr)) = = 0 );
def checkFactors(x):
if (isPerfectSquare(x)):
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
N = 36 ;
checkFactors(N);
|
C#
using System;
class GFG{
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static void checkFactors( int x)
{
if (isPerfectSquare(x))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main(String[] args)
{
int N = 36;
checkFactors(N);
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function checkFactors(N)
{
if (isPerfectSquare(N))
document.write( "Yes" + "<br>" );
else
document.write( "No" + "<br>" );
}
N = 36;
checkFactors(N);
</script>
|
Time Complexity: O(log(N)) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
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 geeks!
Last Updated :
09 Dec, 2022
Like Article
Save Article