Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find sum of even factors of a number in Python Program
Finding the sum of even factors of a number involves identifying all numbers that divide evenly into the given number, then summing only the even factors.
What are Factors?
Factors are numbers that completely divide a given number, leaving zero as remainder. For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12.
For even factors, we only consider factors that are divisible by 2. If the given number is odd, it will have no even factors (except possibly 1, which isn't even).
Example Analysis
Let's examine the factors of 60:
- All factors: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60
- Even factors: 2, 4, 6, 10, 12, 20, 30, 60
- Sum of even factors: 2 + 4 + 6 + 10 + 12 + 20 + 30 + 60 = 144
Method 1: Simple Approach
The straightforward method checks all numbers from 1 to N and finds even factors ?
def sum_even_factors_simple(n):
if n % 2 != 0: # Odd numbers have no even factors
return 0
even_sum = 0
for i in range(1, n + 1):
if n % i == 0 and i % 2 == 0: # Factor and even
even_sum += i
return even_sum
# Test with example
n = 60
result = sum_even_factors_simple(n)
print(f"Sum of even factors of {n}: {result}")
Sum of even factors of 60: 144
Method 2: Optimized Approach
We can optimize by checking only up to the square root of N ?
import math
def sum_even_factors_optimized(n):
if n % 2 != 0: # Odd numbers have no even factors
return 0
even_sum = 0
sqrt_n = int(math.sqrt(n))
for i in range(1, sqrt_n + 1):
if n % i == 0:
# Check if i is even
if i % 2 == 0:
even_sum += i
# Check the paired factor
paired_factor = n // i
if paired_factor != i and paired_factor % 2 == 0:
even_sum += paired_factor
return even_sum
# Test with examples
test_numbers = [60, 40, 55, 24]
for num in test_numbers:
result = sum_even_factors_optimized(num)
print(f"Sum of even factors of {num}: {result}")
Sum of even factors of 60: 144 Sum of even factors of 40: 90 Sum of even factors of 55: 0 Sum of even factors of 24: 60
Method 3: Using List Comprehension
A more Pythonic approach using list comprehension ?
def sum_even_factors_pythonic(n):
if n % 2 != 0:
return 0
factors = [i for i in range(1, n + 1) if n % i == 0]
even_factors = [f for f in factors if f % 2 == 0]
print(f"All factors of {n}: {factors}")
print(f"Even factors of {n}: {even_factors}")
return sum(even_factors)
# Test the function
n = 36
result = sum_even_factors_pythonic(n)
print(f"Sum of even factors: {result}")
All factors of 36: [1, 2, 3, 4, 6, 9, 12, 18, 36] Even factors of 36: [2, 4, 6, 12, 18, 36] Sum of even factors: 78
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Simple Approach | O(n) | O(1) | Small numbers, easy understanding |
| Optimized Approach | O(?n) | O(1) | Large numbers, efficiency |
| List Comprehension | O(n) | O(k) where k is factor count | Readable code, debugging |
Conclusion
Use the optimized approach for large numbers due to its O(?n) complexity. The simple method works well for educational purposes and small inputs. Remember that odd numbers will always return 0 as they have no even factors.
