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

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.

AdvancedPhase 6 - Category 1: Number LogicExample 3 of 10
perfect-numbers-in-range.py
1# Find perfect numbers
2print("Perfect numbers 1-1000:")
3
4for num in range(1, 1001):
5 divisor_sum = 0
6 for i in range(1, num):
7 if num % i == 0:
8 divisor_sum += i
9
10 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. 1

    Visit each candidate through one thousand.

  2. 2

    Sum divisors smaller than that candidate.

  3. 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.