Sum of all the factors of a number in Python
In this tutorial, we will learn to sum all the factors of a number in Python. Before start, we should now what is factors and how two find factors of a number.
Factors are numbers or algebraic expressions that divides another number with no remainder. For example, the factors of 30 are:-
Also, the sum of all factors (1+2+3+5+6+10+15+30) is 72. Now, we will find the sum of the factors with the help of Python. So let’s continue to see how we can do it…
To find the total of all the factors of a specific number in Python, we can define a function and using for loop and applying condition we can identify the sum.
def factor(num): factor =  for i in range(2,num+1): if num%i==0: factor.append(i) return sum(factor)
As you can see, I have defined a function named as a factor. The factor always starts from 1 and ends to a given number (input number).
Then using for loop and applying the condition, I have returned the sum of all the factors.
Now, we can call the function (factor) for any number. Let us find the sum of all the factors of 30.
Now if we run it, it will return the output that you can see below:
As we have seen above, the sum of the factors of 30 is 72. So it returns 72 as the output. All we need to do is just to pass the number in the function and it will return the sum of factors of that provided number.