π‘ Problem Formulation: The task is to write a Python program that finds the sum of all even factors of a given positive integer. For example, if the input number is 10, the expected output is 8 because the even factors of 10 are 2 and 10, and their sum is 12.
Method 1: Loop Through and Test Divisibility
This traditional approach requires iterating through all numbers from 1 up to the given number. For each number that evenly divides the given number, we check if it is even, and if so, we add it to the sum. The method is straightforward and makes use of basic control flow constructs such as loops and conditional statements.
Here’s an example:
def sum_even_factors(number): total = 0 for i in range(1, number + 1): if number % i == 0 and i % 2 == 0: total += i return total print(sum_even_factors(10))
Output: 12
This code defines a function sum_even_factors()
that loops from 1 to the number. It checks each value i: if i is a factor of the number and is even, it adds i to the total. When all factors are checked, it returns the sum of the even factors.
Method 2: Optimized Loop with Half Range
A small optimization to the first method is to iterate only up to half the value of the given number because a number cannot have factors larger than its half (excluding itself). This cuts down the number of iterations and checks required.
Here’s an example:
def sum_even_factors_optimized(number): total = 0 for i in range(2, number // 2 + 1, 2): if number % i == 0: total += i if number % 2 == 0: total += number return total print(sum_even_factors_optimized(10))
Output: 12
This code starts its loop at 2 and increments by 2 to only consider even numbers up to half of the given number, reducing the amount of unnecessary checks. It separately adds the number itself if it is even, before returning the sum.
Method 3: Using List Comprehension and the sum
Function
List comprehension in Python allows us to create lists in a single line of code. By combining list comprehension with the built-in sum()
function, we can find the sum of even factors in an efficient and pythonic way.
Here’s an example:
def sum_even_factors_lc(number): return sum(i for i in range(2, number+1, 2) if number % i == 0) print(sum_even_factors_lc(10))
Output: 12
The sum_even_factors_lc()
function uses list comprehension to generate even numbers between 2 and the number. The if
clause filters out only the even factors of the number, which the sum()
function then accumulates to give the final result.
Method 4: Using the filter
Function
Python’s filter()
function can be used to filter out elements from a list based on a condition. When used with a lambda function and combined with the sum()
function, we can effectively find the sum of even factors of a number.
Here’s an example:
def sum_even_factors_filter(number): return sum(filter(lambda i: i % 2 == 0 and number % i == 0, range(1, number+1))) print(sum_even_factors_filter(10))
Output: 12
In sum_even_factors_filter()
, the filter()
function takes a lambda that checks if an integer is an even factor of the number. Only elements passing this test are summed up, leading to the desired result.
Bonus One-Liner Method 5: Using Generator Expression and sum
This method is a more concise version of Method 3. Instead of a list comprehension, we use a generator expression within the sum()
function for a compact one-liner.
Here’s an example:
sum_even_factors_one_liner = lambda number: sum(i for i in range(2, number+1, 2) if number % i == 0) print(sum_even_factors_one_liner(10))
Output: 12
A lambda function sum_even_factors_one_liner()
is defined that leverages a generator expression to iterate through the even numbers, summing up the even factors of the input number directly.
Summary/Discussion
- Method 1: Loop and Test. Strengths: Easy to understand. Weaknesses: Potentially slow for large numbers due to going through every number.
- Method 2: Half Range Optimization. Strengths: More efficient than method 1 for large numbers. Weaknesses: Slightly more complex than method 1.
- Method 3: List Comprehension. Strengths: Clean and idiomatic Python code. Weaknesses: Generates an entire list in memory, which can be inefficient for large numbers.
- Method 4: Filter Function. Strengths: Functional approach, easy to combine conditions. Weaknesses: May be less intuitive for beginners.
- Method 5: One-Liner Generator. Strengths: Very concise, memory-efficient. Weaknesses: Code conciseness can impact readability for some.