- Binary
1111(4 ones)
Check Evil Number in Python
What you’ll learn
- What evil and odious numbers mean in binary.
- How to count
1bits and check parity. - Single check, range listing, and live preview.
Overview
An evil number has an even number of 1s in binary. If the count is odd, it is called odious.
Two programs
Single value and range [1, 10].
Live preview
Shows popcount and verdict quickly.
Zero case
0 is evil because it has 0 ones.
Prerequisites
Loops, integer division, modulo, and basic binary understanding.
- Use
n % 2andn // 2to read binary digits. - Optional: bitwise
&andbit_count().
What is an evil number?
Count how many 1 digits appear in binary. Even count means evil, odd count means odious.
Example: 15 is 1111, which has four ones, so 15 is evil.
Hamming weight
Hamming weight means number of 1-bits. Evil numbers are exactly those with even Hamming weight.
1515 = 1111, weight is 4 (even), so 15 is evil.
Intuition
- Binary
111(3 ones)
Takeaway: the parity of the count of ones is the only thing that matters.
Live preview
Nonnegative integers only for this definition.
Algorithm
Goal: check if popcount parity is even.
Count ones
Use bit loop or Python bit_count().
Check count parity
Even count means evil.
📜 Pseudocode
function is_evil(n): // n >= 0
ones = count_ones_in_binary(n)
return (ones mod 2) == 0 Single check with bit_count()
Python has built-in bit_count() for integers.
def is_evil(n: int) -> bool:
if n < 0:
return False
return n.bit_count() % 2 == 0
number = 15
if is_evil(number):
print(f"{number} is an Evil Number.")
else:
print(f"{number} is not an Evil Number.") Explanation
15.bit_count() is 4, and 4 is even.
Evil numbers in [1, 10]
Range output matches the classic sample.
def is_evil_nonneg(num: int) -> bool:
ones = 0
while num > 0:
if num % 2 == 1:
ones += 1
num //= 2
return ones % 2 == 0
print("Evil numbers in the range 1 to 10:")
for i in range(1, 11):
if is_evil_nonneg(i):
print(i, end=" ") Explanation
The loop counts ones in binary by repeatedly dividing by 2.
Optimization
Use bit_count(). It is fast and readable for Python integers.
Kernighan loop. n &= n - 1 removes one set bit per step.
Interview: define nonnegative scope and mention zero case.
❓ FAQ
🔄 Input / output examples
Try these values in either method.
| n | Binary | Ones | Verdict |
|---|---|---|---|
0 | 0 | 0 | Evil |
3 | 11 | 2 | Evil |
7 | 111 | 3 | Odious |
15 | 1111 | 4 | Evil |
Edge cases and pitfalls
Most definitions use nonnegative integers. Keep that policy clear in your code.
n = 0
Evil because ones count is 0 (even).
Policy needed
This page rejects negatives for clarity.
Binary form
Use canonical nonnegative binary representation.
Include bounds
Use inclusive loops when required by question.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single number popcount | O(bits) | O(1) |
| Kernighan method | O(popcount) | O(1) |
| Range scan | O(range * bits) | O(1) |
Summary
- Evil means even number of 1-bits.
- Use
bit_count()or manual bit loops. - Remember: 0 is evil, negatives need explicit policy.
The opposite of an evil number is an odious number. Evil means an even count of 1 bits; odious means odd.
8 people found this page helpful
