Python Program to Find the Sum of Even Factors of a Number
Introduction
Factors of a number are the positive integers that divide the number completely. In this Python program, we will focus on finding the sum of even factors of a given number. The program will take a positive integer as input, calculate its factors, identify the even factors, and then compute their sum.
Understanding the Algorithm
The algorithm for finding the sum of even factors of a number involves the following steps:
- Input: Accept a positive integer as input.
- Calculate Factors: Identify all the factors of the given number.
- Filter Even Factors: Select only the even factors from the list.
- Compute Sum: Sum the selected even factors.
- Display Result: Print or display the final sum.
Python Program for Finding Sum of Even Factors
def sum_of_even_factors(number):
# Initialize variables
factors = []
even_factors = []
# Find factors of the number
for i in range(1, number + 1):
if number % i == 0:
factors.append(i)
# Filter even factors
even_factors = [factor for factor in factors if factor % 2 == 0]
# Compute the sum of even factors
sum_even_factors = sum(even_factors)
return sum_even_factors
# Example: Find sum of even factors of the number 12
input_number_example = 12
# Calculate sum of even factors
sum_even_factors_example = sum_of_even_factors(input_number_example)
# Display the result
print(f"Sum of even factors of {input_number_example}: {sum_even_factors_example}")
Output Example
Example: Find Sum of Even Factors of the Number 12
Sum of even factors of 12: 16
Explanation
The Python program defines a function sum_of_even_factors
that takes a positive integer as input. It calculates all the factors of the number, filters out the even factors, and computes their sum.
In the example provided, the program calculates the sum of even factors for the number 12.
Conclusion
Finding the sum of even factors is a common mathematical operation. This Python program provides a simple and efficient way to accomplish this task. Feel free to test the program with different input numbers and explore its functionality.
If you have any questions or need further clarification, please don’t hesitate to ask!