Perfect Numbers in Range
List perfect numbers from one through one thousand with Python. Students reset proper-divisor totals per candidate and compare a completed sum with its number.
1# Find perfect numbers2print("Perfect numbers 1-1000:")34for num in range(1, 1001):5 divisor_sum = 06 for i in range(1, num):7 if num % i == 0:8 divisor_sum += i910 if divisor_sum == num:11 print(num, end=" ")12print()
Output
Perfect numbers 1-1000: 6 28 496
In this one
- Nest proper-divisor scans
- Reset per-candidate totals
- Identify perfect numbers
How it works
- 1
Visit each candidate through one thousand.
- 2
Sum divisors smaller than that candidate.
- 3
Print it when the completed sum equals it.
Edge cases
Is one included?
Is one included?
It is tested but does not qualify.
Why reset divisor_sum inside the outer loop?
Why reset divisor_sum inside the outer loop?
Each candidate requires its own independent sum.
What's going on
The outer loop selects each positive candidate. The inner range excludes num itself while adding every proper positive divisor.
When divisor_sum equals num, the number prints. Six, twenty-eight, and four hundred ninety-six qualify in this range. One fails because its sum is zero. This direct nested search is deliberately simple and repeats many divisibility tests rather than using a number-theory shortcut.
Is one included? It is tested but does not qualify.
Why reset divisor_sum inside the outer loop? Each candidate requires its own independent sum.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which divisor is omitted?
Pick an answer to check it.
Question 2
Is one included?
Pick an answer to check it.
Questions people ask
Is one included?
It is tested but does not qualify.
Why reset divisor_sum inside the outer loop?
Each candidate requires its own independent sum.
Which divisor is omitted?
The candidate itself.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum