
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if count of divisors is even or odd in Python
Suppose we have a number n, we have to find its total number of divisors are even or odd.
So, if the input is like n = 75, then the output will be Even, as the divisors are [1, 3, 5, 15, 25, 75].
To solve this we shall follow one simple and efficient approach. We have observed that when a number is perfect square then only it has odd number of divisors. So if the number is not perfect square then it will have even divisors. So here we will only check whether the number is perfect square or not and based on this we can return "odd" or "even" as output.
To solve this, we will follow these steps −
- if n < 1 is non-zero, then
- return
- sqrt := square root of n
- if sqrt*sqrt is same as n, then
- return 'Odd'
- otherwise,
- return 'Even'
Let us see the following implementation to get better understanding −
Example
def solve(n): if n < 1: return sqrt = n**0.5 if sqrt*sqrt == n: return 'Odd' else: return 'Even' n = 75 print(solve(n))
Input
75
Output
Even
- Related Articles
- Python Program for Check if the count of divisors is even or odd
- C Program to Check if count of divisors is even or odd?
- Java Program to check if count of divisors is even or odd
- PHP program to check if the total number of divisors of a number is even or odd
- How to Check if a Number is Odd or Even using Python?
- Check whether given floating point number is even or odd in Python
- Check whether product of 'n' numbers is even or odd in Python
- Check whether the length of given linked list is Even or Odd in Python
- Check if the n-th term is odd or even in a Fibonacci like sequence
- C++ Program to Check Whether Number is Even or Odd
- Check if product of digits of a number at even and odd places is equal in Python
- Java Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Even or Odd
- Check Average of Odd Elements or Even Elements are Greater in Java
- 8085 program to check whether the given number is even or odd

Advertisements