Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Sum of All Factors

Add every positive factor of a number with a Python loop. Students use a remainder filter, an accumulator, and an inclusive bound that counts the number itself.

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

In this one

  • Accumulate all factors
  • Distinguish proper-factor sums
  • Use an inclusive stop

How it works

  1. 1

    Initialize factor_sum to zero.

  2. 2

    Add each candidate dividing num, including num.

  3. 3

    Print the completed factor total.

Edge cases

What is the result for one?

What is the result for one?

One, because one is its own factor.

Are negative factors included?

Are negative factors included?

No. Only positive candidates are visited.

What's going on

factor_sum begins at zero. Each candidate that divides num exactly is added rather than printed individually.

For six, one, two, three, and six total twelve. This differs from a perfect-number check, which excludes the number itself and would total six. The range includes num through the stop num + 1 and assumes a positive input.

What is the result for one? One, because one is its own factor.

Are negative factors included? No. Only positive candidates are visited.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How does this differ from a proper-divisor sum?

Pick an answer to check it.

Question 2

What is the result for one?

Pick an answer to check it.

Questions people ask

What is the result for one?

One, because one is its own factor.

Are negative factors included?

No. Only positive candidates are visited.

How does this differ from a proper-divisor sum?

It includes num itself.