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.
1# Take number2num = int(input("Enter a number: "))34# Sum factors5factor_sum = 06for i in range(1, num + 1):7 if num % i == 0:8 factor_sum += i910print(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
Initialize factor_sum to zero.
- 2
Add each candidate dividing num, including num.
- 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.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum