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

Perfect Number Check

Test whether a positive integer equals its proper-divisor sum. A lesson on excluding the number itself, accumulating factors, and perfect-number boundaries.

IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 6 of 14
perfect-number-check.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Find sum of proper divisors
5divisor_sum = 0
6for i in range(1, num):
7 if num % i == 0:
8 divisor_sum += i
9
10# Check perfect number
11if num > 0 and divisor_sum == num:
12 print("Perfect number")
13else:
14 print("Not a perfect number")

Output

Enter a number: 6
Perfect number

Enter a number: 10
Not a perfect number

In this one

  • Find proper divisors
  • Exclude the number itself
  • Require a positive domain

How it works

  1. 1

    Scan divisors from one below num.

  2. 2

    Add candidates with remainder zero.

  3. 3

    Require positive num and a matching divisor sum.

Edge cases

Is one perfect?

Is one perfect?

No. Its proper-divisor sum is zero.

Is zero perfect?

Is zero perfect?

No. The final decision needs a positive-domain guard rather than accepting an empty sum.

What's going on

A proper divisor is a positive factor smaller than the number. range(1, num) deliberately excludes num while the remainder test selects its divisors.

For six, the selected factors one, two, and three sum to six. One has no proper positive divisors, so its total is zero and it fails. The mathematical domain is positive integers; zero must not pass just because an empty total is zero.

Is one perfect? No. Its proper-divisor sum is zero.

Is zero perfect? No. The final decision needs a positive-domain guard rather than accepting an empty sum.

Quiz

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

Question 1

Which factor is excluded?

Pick an answer to check it.

Question 2

Is one perfect?

Pick an answer to check it.

Questions people ask

Is one perfect?

No. Its proper-divisor sum is zero.

Is zero perfect?

No. The final decision needs a positive-domain guard rather than accepting an empty sum.

Which factor is excluded?

The number itself.