 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 