Find sum of even factors of a number in Python Program

Let’s start our article with an explanation of how to find the sum of even factors of a number but what are factors? 

Factors are the numbers which completely divide the given number leaving zero as remainder or we can say factors are the multiples of the number.

Example 1 − If we are given a number 60

Factors of 60 (6*10=2*3*2*5) are 2, 5 and 3

But according to the question, we must find out the sum of even factors so, in the above given example, the even factor will be only 2.

Also, if the given number is an odd number, then the factors will not be even numbers.

Example 2 − 55 is the given number

Its factors are 5 and 11 we can see that both the factors are not even hence the odd number will not have an even factor.

Now as we know python is an advanced language hence it has modules designed for the purpose of solving the mathematics functions and as our question is dealing with a math problem we have to import and add such modules in our codes.

So before writing our codes let’s see what concepts we can apply in our code as per the question given to us. This method will help us find the most appropriate function and improve our approach towards the question.

What should be our approach and why?

Since our question is completely based on the mathematic tools hence our first approach should be finding out such tools which will help us completing the code. So, we will import math module which will allow us to use the functions.

If one continues to have a doubt as to what if the number is odd what we will do so to solve this problem our next approach should be to think about a statement which will help us make choice or we can say, make decision for us and according to our need our work can be made easy using the IF statement and then use FOR and WHILE statements as per use. Now let’s write our program.



import math def sum_EF(N) : if (N % 2 != 0) : return 0 ut = 1 for i in range(2, (int)(math.sqrt(N)) + 1) : num = 0 sum_n = 1 num_t = 1 while (N % i == 0) : num= num + 1 N = N // i if (i == 2and num == 1) : sum_n = 0 num_t = num_t * i sum_n = sum_n + num_t ut = ut * sum_n if (N >= 2) : ut = ut * (1 + N) return ut N = 40 print(sum_EF(N))



As we have written the codes for our question let’s understand some of the key points from the start. Very first thing which we have added to our code is that we have imported the MATH module.

Then the next important thing is finding out whether the asked number is odd or even. Since we know computer is not an intelligent system so we have to guide it that’s why first we have written code for checking the number whether it is odd or even.


if (N % 2 != 0) :
return 0

Here we have used IF – statement for checking so if the number will be odd it will return the code to zero otherwise continue.

Now moving further with the code let’s assume we’ll entered a even number, now our work will be to check the factors of the input number starting from 1.

Now for checking the factors we have created a range and as you can see we have written math.sqrt . this is an in-built function in python which helps us to return the square root of the number.


for i in range(2, (int)(math.sqrt(N)) + 1)

Next is to remove prime numbers (numbers which get divisible only by themselves example 1,3,5,7etc) from the factors of the number since all odd numbers are not prime numbers, but all prime numbers are odd numbers.


if (N >= 2) :
ut = ut * (1 + N)

Return it is used when there will be a prime number.

Next, we have used

if (i == 2 and num == 1) :
      num_sm = 0
   num_tm = num_tm * i
   num_sm = num_sm + num_tm
ut = ut * num_sm

for removing the 20 value which gives the value 1.

N=N//i − // is the floor division function used to remove integers from the output which we might get in between solving the problem.

In the end, we have given a value in the code with the purpose of finding out the sum of even factors.