VOOZH about

URL: https://www.geeksforgeeks.org/dsa/python-program-for-find-sum-of-even-factors-of-a-number/

⇱ Python Program to Find Sum of Even Factors of a Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Program to Find Sum of Even Factors of a Number

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.


Output
26

Explanation:

  • math.sqrt(n) iterates only up to the square root for efficiency.
  • while temp % i == 0: counts the power of each prime factor.
  • Ignores 2⁰ = 1 to remove odd contribution.
  • Multiplies all remaining sums to get the total even factor sum.

Using List Comprehension

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


Output
26

Explanation:

  • [i for i in range(1, n + 1)] iterates from 1 to n.
  • if n % i == 0 filters only factors of n and i % 2 == 0 ensures only even ones are taken.
  • sum(res) adds them all together.

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().


Output
26

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.


Output
26

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!

Comment
Article Tags:
Article Tags: