Check Perfect Number in C#
What you’ll learn
- What a perfect number is: sum of proper divisors equals the number itself.
- How to check
28using the sqrt divisor-pair optimization from the reference. - How to list perfect numbers from
1to50with a simple loop ton / 2. - Live preview showing perfect, abundant, or deficient verdicts.
What is a perfect number?
A positive integer is perfect when the sum of its proper divisors (all divisors except itself) equals the number. Example: divisors of 6 are 1, 2, 3 and 1 + 2 + 3 = 6.
Classic example
28 = 1 + 2 + 4 + 7 + 14 — perfect.
Live preview
See divisor sum and perfect / abundant / deficient label.
Range scan
From 1 to 50 only 6 and 28 qualify.
Prerequisites
Divisors, modulo (%), loops, and optional Math.Sqrt.
- You know that
n % i == 0meansidividesn. - You can accumulate a running
suminside aforloop.
The idea
Add every proper divisor of n. If the total equals n, the number is perfect. If the total is greater, it is abundant; if less, deficient.
Definition
Let s(n) be the sum of proper divisors of n. Then n is perfect when s(n) = n. Proper divisors exclude n itself.
28s(28) = 1 + 2 + 4 + 7 + 14 = 28, so 28 is perfect.
Intuition examples
6 → 1+2+3=6 (perfect). 12 → 1+2+3+4+6=16 (abundant). 10 → 1+2+5=8 (deficient).
Live preview
Type a positive integer and see proper divisors, their sum, and whether the number is perfect, abundant, or deficient.
Algorithm
Goal: decide whether n is perfect.
Reject small values
If n <= 1, return false — 1 is not perfect.
Sum proper divisors
Loop divisors and add them (simple: 1..n/2; faster: pairs up to sqrt(n)).
Compare
Return true when sum == n.
📜 Pseudocode
function isPerfect(n):
if n <= 1:
return false
sum = 0
for i from 1 to floor(n / 2):
if n mod i == 0:
sum = sum + i
return sum == n Check one number (sqrt optimization)
Reference approach: start sum = 1, loop to Math.Sqrt(number), add divisor pairs. Tests 28.
using System;
class Program
{
static bool IsPerfectNumber(int number)
{
if (number <= 1)
return false;
int sum = 1;
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
if (i == number / i)
sum += i;
else
sum += i + (number / i);
}
}
return sum == number;
}
static void Main()
{
int number = 28;
if (IsPerfectNumber(number))
Console.WriteLine($"{number} is a perfect number.");
else
Console.WriteLine($"{number} is not a perfect number.");
}
} How the program works
1is always counted first as a proper divisor.- When
idividesnumber, addiandnumber/ias a pair (or justiwhen it is a square root). - Compare the accumulated sum to the original — equal means perfect.
Perfect numbers from 1 to 50
Beginner-friendly loop to n / 2 — same logic as the reference range program. Only 6 and 28 appear in this interval.
using System;
class Program
{
static bool IsPerfectNumber(int num)
{
if (num <= 1)
return false;
int sum = 0;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
sum += i;
}
return sum == num;
}
static void Main()
{
Console.WriteLine("Perfect Numbers in the range 1 to 50:");
for (int i = 1; i <= 50; i++)
{
if (IsPerfectNumber(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization notes
Sqrt pairs. Example 1 visits about O(√n) candidates instead of O(n) — ideal for single checks in interviews.
Use long for sums. When scanning large ranges, divisor sums can exceed int.MaxValue.
❓ FAQ
🔄 Input / output examples
n | Proper divisor sum | Verdict |
|---|---|---|
6 | 1+2+3=6 | Perfect |
28 | 1+2+4+7+14=28 | Perfect |
10 | 1+2+5=8 | Deficient |
12 | 1+2+3+4+6=16 | Abundant |
Edge cases and pitfalls
Not perfect
Without the n <= 1 guard, the sqrt method wrongly treats 1 as perfect.
Perfect squares
When i == number / i, add i only once — not twice.
Large n
Use long for sum when testing bigger values.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
Loop to n / 2 | O(n) | O(1) |
| Sqrt divisor pairs | O(√n) | O(1) |
Range 1..R | O(R × cost per check) | O(1) |
Summary
- Perfect means proper-divisor sum equals
n. - Simple loop to
n/2is easy; sqrt pairs are faster. - Guard
n <= 1and watch sum overflow on large inputs.
The first four perfect numbers are 6, 28, 496, and 8128. Only about 51 perfect numbers are known — they are extremely rare.
8 people found this page helpful
