Check if count of divisors is even or odd
Last Updated :
11 Mar, 2024
Given a number “n”, find its total number of divisors are even or odd.
Examples :
Input : n = 10
Output : Even
Input: n = 100
Output: Odd
Input: n = 125
Output: Even
We strongly recommend that you practice it, before moving on to the solution.
A naive approach would be to find all the divisors and then see if the total number of divisors is even or odd.
Time complexity for such a solution would be O(sqrt(n))
C++
#include <bits/stdc++.h>
using namespace std;
void countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= sqrt (n) + 1; i++)
{
if (n % i == 0)
count += (n / i == i) ? 1 : 2;
}
if (count % 2 == 0)
cout << "Even" << endl;
else
cout << "Odd" << endl;
}
int main()
{
cout << "The count of divisor: " ;
countDivisors(10);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
void countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= sqrt (n) + 1; i++)
{
if (n % i == 0)
count += (n / i == i) ? 1 : 2;
}
if (count % 2 == 0)
printf ( "Even\n" );
else
printf ( "Odd\n" );
}
int main()
{
printf ( "The count of divisor: " );
countDivisors(10);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG
{
static void countDivisors( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n) + 1 ; i++)
{
if (n % i == 0 )
count += (n / i == i) ? 1 : 2 ;
}
if (count % 2 == 0 )
System.out.println( "Even" );
else
System.out.println( "Odd" );
}
public static void main(String args[])
{
System.out.print( "The count of divisor: " );
countDivisors( 10 );
}
}
|
Python3
import math
def countDivisors(n) :
count = 0
for i in range ( 1 , ( int )(math.sqrt(n)) + 2 ) :
if (n % i = = 0 ) :
if ( n / / i = = i) :
count = count + 1
else :
count = count + 2
if (count % 2 = = 0 ) :
print ( "Even" )
else :
print ( "Odd" )
print ( "The count of divisor: " )
countDivisors( 10 )
|
C#
using System;
class GFG {
static void countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= Math.Sqrt(n)
+ 1;
i++) {
if (n % i == 0)
count += (n / i == i) ? 1 : 2;
}
if (count % 2 == 0)
Console.Write( "Even" );
else
Console.Write( "Odd" );
}
public static void Main()
{
Console.Write( "The count of divisor: " );
countDivisors(10);
}
}
|
Javascript
<script>
function countDivisors(n)
{
let count = 0;
for (let i = 1; i <= Math.sqrt(n) + 1; i++)
{
if (n % i == 0)
count += (Math.floor(n / i) == i) ? 1 : 2;
}
if (count % 2 == 0)
document.write( "Even" + "<br>" );
else
document.write( "Odd" + "<br>" );
}
document.write( "The count of divisor: " );
countDivisors(10);
</script>
|
PHP
<?php
function countDivisors( $n )
{
$count = 0;
for ( $i = 1; $i <= sqrt( $n ) + 1; $i ++)
{
if ( $n % $i == 0)
$count += ( $n / $i == $i )? 1 : 2;
}
if ( $count % 2 == 0)
echo "Even\n" ;
else
echo "Odd\n" ;
}
echo "The count of divisor: " ;
countDivisors(10);
?>
|
Output :
The count of divisor: Even
Time Complexity: O(?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.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
void countDivisors( int n)
{
int root_n = sqrt (n);
if (root_n * root_n == n)
printf ( "Odd\n" );
else
printf ( "Even\n" );
}
int main()
{
cout << "The count of divisors"
<< " of 10 is: " ;
countDivisors(14);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG
{
static void countDivisors( int n)
{
int root_n = ( int )(Math.sqrt(n));
if (root_n * root_n == n)
System.out.println( "Odd" );
else
System.out.println( "Even" );
}
public static void main(String args[])
throws IOException
{
System.out.print( "The count of" +
"divisors of 10 is: " );
countDivisors( 10 );
}
}
|
Python3
import math
def NumOfDivisor(n):
if n < 1 :
return
root_n = int (math.sqrt(n))
if root_n * * 2 = = n:
print ( 'Odd' )
else :
print ( 'Even' )
if __name__ = = '__main__' :
print ( "The count of divisor is:" )
NumOfDivisor( 14 )
|
C#
using System;
class GFG {
static void countDivisors( int n)
{
int root_n = ( int )(Math.Sqrt(n));
if (root_n * root_n == n)
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );
}
public static void Main()
{
Console.Write( "The count of divisors : " );
countDivisors(10);
}
}
|
Javascript
<script>
function countDivisors(n)
{
let root_n = Math.sqrt(n);
if (root_n * root_n == n)
document.write( "Odd" + "<br>" );
else
document.write( "Even" + "<br>" );
}
document.write( "The count of divisor: " );
countDivisors(10);
</script>
|
PHP
<?php
function countDivisors( $n )
{
$root_n = sqrt( $n );
if ( $root_n * $root_n == $n )
echo "Odd\n" ;
else
echo "Even\n" ;
}
echo "The count of divisors of 10 is: " ;
countDivisors(10);
?>
|
Output :
The count of divisor: Even
Time Complexity: O(logn) as it is using inbuilt sqrt function
Auxiliary Space: O(1)
Approach:
- Find the prime factorization of the given number n.
- Count the number of times each distinct prime factor appears in the factorization. This count is equal to the exponent of the prime factor in the factorization.
- Compute the product of (exponent + 1) for each distinct prime factor.
- If the product is even, then the number of divisors is even, otherwise it is odd.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
vector< int > primeFactors( int n)
{
vector< int > factors;
while (n % 2 == 0) {
factors.push_back(2);
n /= 2;
}
for ( int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 2) {
factors.push_back(n);
}
return factors;
}
string countDivisors( int n)
{
vector< int > factors = primeFactors(n);
int prev = factors[0], count = 1, product = 1;
for ( int i = 1; i < factors.size(); i++) {
if (factors[i] == prev) {
count++;
} else {
product *= (count + 1);
prev = factors[i];
count = 1;
}
}
product *= (count + 1);
if (product % 2 == 0) {
return "Even" ;
} else {
return "Odd" ;
}
}
int main()
{
cout << countDivisors(10) << endl;
cout << countDivisors(100) << endl;
cout << countDivisors(125) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static List<Integer> primeFactors( int n) {
List<Integer> factors = new ArrayList<>();
while (n % 2 == 0 ) {
factors.add( 2 );
n /= 2 ;
}
for ( int i = 3 ; i * i <= n; i += 2 ) {
while (n % i == 0 ) {
factors.add(i);
n /= i;
}
}
if (n > 2 ) {
factors.add(n);
}
return factors;
}
public static String countDivisors( int n) {
List<Integer> factors = primeFactors(n);
int prev = factors.get( 0 );
int count = 1 ;
int product = 1 ;
for ( int i = 1 ; i < factors.size(); i++) {
if (factors.get(i) == prev) {
count++;
} else {
product *= (count + 1 );
prev = factors.get(i);
count = 1 ;
}
}
product *= (count + 1 );
if (product % 2 == 0 ) {
return "Even" ;
} else {
return "Odd" ;
}
}
public static void main(String[] args) {
System.out.println(countDivisors( 10 ));
System.out.println(countDivisors( 100 ));
System.out.println(countDivisors( 125 ));
}
}
|
Python3
def prime_factors(n):
factors = []
while n % 2 = = 0 :
factors.append( 2 )
n / / = 2
for i in range ( 3 , int (n * * 0.5 ) + 1 , 2 ):
while n % i = = 0 :
factors.append(i)
n / / = i
if n > 2 :
factors.append(n)
return factors
def count_divisors(n):
factors = prime_factors(n)
prev = factors[ 0 ]
count = 1
product = 1
for i in range ( 1 , len (factors)):
if factors[i] = = prev:
count + = 1
else :
product * = (count + 1 )
prev = factors[i]
count = 1
product * = (count + 1 )
if product % 2 = = 0 :
return "Even"
else :
return "Odd"
if __name__ = = "__main__" :
print (count_divisors( 10 ))
print (count_divisors( 100 ))
print (count_divisors( 125 ))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static List< int > PrimeFactors( int n)
{
List< int > factors = new List< int >();
while (n % 2 == 0)
{
factors.Add(2);
n /= 2;
}
for ( int i = 3; i * i <= n; i += 2)
{
while (n % i == 0)
{
factors.Add(i);
n /= i;
}
}
if (n > 2)
{
factors.Add(n);
}
return factors;
}
static string CountDivisors( int n)
{
List< int > factors = PrimeFactors(n);
int prev = factors[0], count = 1, product = 1;
for ( int i = 1; i < factors.Count; i++)
{
if (factors[i] == prev)
{
count++;
}
else
{
product *= (count + 1);
prev = factors[i];
count = 1;
}
}
product *= (count + 1);
if (product % 2 == 0)
{
return "Even" ;
}
else
{
return "Odd" ;
}
}
static void Main()
{
Console.WriteLine(CountDivisors(10));
Console.WriteLine(CountDivisors(100));
Console.WriteLine(CountDivisors(125));
}
}
|
Javascript
function primeFactors(n) {
const factors = [];
while (n % 2 === 0) {
factors.push(2);
n /= 2;
}
for (let i = 3; i * i <= n; i += 2) {
while (n % i === 0) {
factors.push(i);
n /= i;
}
}
if (n > 2) {
factors.push(n);
}
return factors;
}
function countDivisors(n) {
const factors = primeFactors(n);
let prev = factors[0];
let count = 1;
let product = 1;
for (let i = 1; i < factors.length; i++) {
if (factors[i] === prev) {
count++;
} else {
product *= (count + 1);
prev = factors[i];
count = 1;
}
}
product *= (count + 1);
if (product % 2 === 0) {
return "Even" ;
} else {
return "Odd" ;
}
}
console.log(countDivisors(10));
console.log(countDivisors(100));
console.log(countDivisors(125));
|
Time Complexity: O(sqrt(n)), where n is the input number. This is because the primeFactors function uses trial division to find all the prime factors of the input number, which takes O(sqrt(n)) time.
Space Complexity: O(sqrt(n)), which is the size of the vector returned by the primeFactors function. In the worst case, the input number n could be a prime number, in which case the vector will have only one element, and the space complexity will be O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...