Sum of All Factors

Calculate sum of all factors of a number.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 7 of 13
sum-of-all-factors.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Sum factors
5factor_sum = 0
6for i in range(1, num + 1):
7 if num % i == 0:
8 factor_sum += i
9
10print(f"Sum of factors: {factor_sum}")

Output

Enter a number: 12
Sum of factors: 28

What's going on

Find all factors and sum them.

Key Concepts:

Find all factors
Add each factor to sum
Print total sum