Python Program for Check if count of divisors is even or odd Last Updated : 08 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 = 100Output: Odd Input: n = 125Output: 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 # Naive Solution to find if count # of divisors is even or odd import math # Function to count # the divisors def countDivisors(n) : # Initialize count # of divisors count = 0 # Note that this loop # runs till square # root for i in range(1, (int)(math.sqrt(n)) + 2) : if (n % i == 0) : # If divisors are # equal, increment # count by one # Otherwise increment # count by 2 if( n // i == i) : count = count + 1 else : count = count + 2 if (count % 2 == 0) : print("Even") else : print("Odd") # Driver Code print("The count of divisor: ") countDivisors(10) # This code is contributed by Nikita Tiwari OutputThe 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 # Python program for # Efficient Solution to find # find if count of divisors # is even or odd import math def NumOfDivisor(n): if n < 1: return root_n = int(math.sqrt(n)) # If n is a perfect square, # then it has odd divisors if root_n**2 == n: print('Odd') else: print('Even') # Driver code if __name__ == '__main__': print("The count of divisor is:") NumOfDivisor(14) # This code is contributed by Yt R OutputThe count of divisor is: Even Time Complexity: O(logn) as it is using inbuilt sqrt functionAuxiliary 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 # Added by ~Nikunj Sonigara 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)) # Output: Even print(count_divisors(100)) # Output: Odd print(count_divisors(125)) # Output: Even OutputEven Odd Even 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! Comment More infoAdvertise with us Next Article Python Program for Check if count of divisors is even or odd K kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python Program for Common Divisors of Two Numbers Given two integer numbers, the task is to find the count of all common divisors of given numbers? Input : a = 12, b = 24Output: 6Explanation: all common divisors are 1, 2, 3, 4, 6 and 12Input : a = 3, b = 17Output: 1Explanation: all common divisors are 1Input : a = 20, b = 36Output: 3Explanation: al 1 min read Python Program for Number of elements with odd factors in given range Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output : 2 min read Program to check if a number is Positive, Negative, Odd, Even, Zero Given a number n, the task is to check whether the given number is positive, negative, odd, even, or zero. Method 1 : A number is positive if it is greater than zero. We check this in the expression of if.If it is False, the number will either be zero or negative.This is also tested in subsequent ex 15 min read Python | sympy.divisor_count() method With the help of sympy.divisor_count() method, we can count the number of divisors of the given integer. Syntax: divisor_count(n, modulus=1) Parameter: n - It denotes an integer. modulus - It is set to 1 by default. If modulus is not 1 then only those that are divisible by modulus are counted. Retur 1 min read Sum Even and Odd Values with One For-Loop and No If-Condition Using Python Summing even and odd numbers in a list is a common programming task. Typically, we'd use an if condition to check whether a number is even or odd. However, we can achieve this in Python efficiently using arithmetic operators and list slicing, without relying on if conditions. In this article, we'll 4 min read Like