Python Program for Check if count of divisors is even or odd
Last Updated :
08 Mar, 2024
Write a Python program for a given number “n”, the task is to find its total number of divisors that are even or odd.
Examples:
Input : n = 10
Output: Even
Input: n = 100
Output: Odd
Input: n = 125
Output: Even
Python Program for Check if count of divisors is even or odd using Naive Approach:
A naive approach would be to find all the divisors and then see if the total number of divisors is even or odd.
Below is the implementation of the approach:
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 )
|
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 the above approach:
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 )
|
Output
The count of divisor is:
Even
Time Complexity: O(logn) as it is using inbuilt sqrt function
Auxiliary Space: O(1)
Python Program for Check if count of divisors is even or odd using factorization:
- 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:
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 ))
|
Time Complexity: O(sqrt(n)), where n is the input number.|
Auxiliary space: O(1)
Please refer complete article on Check if count of divisors is even or odd for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...