Python Program to Find Sum of Even Factors of a Number (original) (raw)

Last Updated : 30 Oct, 2025

Given a number N, the task is to find the sum of all even factors of that number. A factor of a number divides it completely without leaving a remainder, and even factors are those divisible by 2. **For Example:

**Input: 30
**Output: 48
**Even factors: 2 + 6 + 10 + 30 = 48

Let’s explore different Python methods to find the sum of even factors efficiently.

Formula-Based Mathematical Approach

This method uses prime factorization and the sum of divisors formula to directly compute the sum of even factors. If the number is even, it ignores the odd part and multiplies only the even factor contributions to get the sum efficiently.

python `

import math n = 18

if n % 2 != 0: print(0) else: res = 1 temp = n for i in range(2, int(math.sqrt(temp)) + 1): count = 0 curr_sum = 1 curr_term = 1 while temp % i == 0: count += 1 temp //= i if i == 2 and count == 1: curr_sum = 0 curr_term *= i curr_sum += curr_term res *= curr_sum if temp >= 2: res *= (1 + temp) print(res)

`

**Explanation:

Using List Comprehension

This method uses Python’s list comprehension to collect all even factors in one line, then sums them using sum().

Python `

n = 18 res = [i for i in range(1, n + 1) if n % i == 0 and i % 2 == 0] print(sum(res))

`

**Explanation:

Using Lambda Function and Filter

This method first finds all factors of the number, then filters only even ones using a lambda function and filter().

Python `

n = 18 fac = [i for i in range(1, n + 1) if n % i == 0] res = list(filter(lambda x: x % 2 == 0, fac)) print(sum(res))

`

**Explanation: filter(lambda x: x % 2 == 0, fac) keeps only even factors and sum() calculates the total sum of filtered even factors.

Using Enumerate and List Conversion

This method converts numbers to strings and back using list comprehensions and enumerate, though less efficient, it still achieves the result cleanly.

Python `

n = 18 nums = [str(i) for i in range(1, n + 1)] fac = [int(i) for i in nums if n % int(i) == 0 and int(i) % 2 == 0] print(sum(fac))

`

**Explanation: Converts numbers to strings for iteration (str(i)), then back to int(i) for divisibility checks. Filters even factors and sums them up.

Please refer complete article on Find sum of even factors of a number for more details!