Print All Perfect Numbers in a Given Range
Print every perfect number that lies within a given start-to-end range.
For each number in the range, add up its proper divisors (every divisor except the number itself). If that sum equals the number, it is perfect, so print it. Test divisibility only up to the square root and add both divisors of each pair to keep it fast.
Problem Statement
A perfect number is a positive integer that equals the sum of its proper
divisors — its divisors excluding itself. For example 6 = 1 + 2 + 3, so 6
is perfect.
Given an inclusive range [start, end], print every perfect number that falls
inside it, separated by spaces. If the range contains no perfect number, print
an empty line.
Input: Two integers, start and end, describing an inclusive range.
Output: The perfect numbers in the range, in ascending order, separated by spaces.
Examples
Input: start = 1, end = 30
Output: 6 286 = 1+2+3 and 28 = 1+2+4+7+14 are the only perfect numbers up to 30.
Input: start = 1, end = 10
Output: 66 is the only perfect number between 1 and 10.
Constraints
1 <= start <= end <= 100000Only proper divisors count (exclude the number itself)
Think Before You Code
Reveal the questions to ask yourself first
- What exactly is a proper divisor, and why is the number itself excluded?
- How can you collect the divisors of a number without looping all the way to n?
- When you find a divisor i, what is the matching second divisor for free?
- What should you print when no perfect number exists in the range?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Break the task into a per-number test, then sweep the range.
- Write
isPerfect(n): numbers below 2 are never perfect. - Start
sum = 1because 1 divides every n > 1 (and is a proper divisor). - Loop
ifrom 2 whilei * i <= n. Whenidividesn, addiand its partnern / i, but only add the partner if it differs fromi(avoids double-counting the square root). nis perfect whensum == n.- In the range sweep, call
isPerfectfor each value and collect the perfect ones into a space-separated line.
Dry Run
Walk through the example step by step
Testing whether n = 28 is perfect:
i | i*i | 28 % i | added divisors | sum
--+-----+--------+---------------------+----
start | 1
2 | 4 | 0 | 2 and 28/2 = 14 | 17
3 | 9 | 1 | - | 17
4 | 16 | 0 | 4 and 28/4 = 7 | 28
5 | 25 | 3 | - | 28
6 | 36 > 28, loop stops | 28
sum (28) == n (28) -> 28 is perfect
Solution
Reveal the full Java solution
public class PerfectNumbersInRange {
public static boolean isPerfect(int n) {
if (n < 2) {
return false;
}
int sum = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
int partner = n / i;
if (partner != i) {
sum += partner;
}
}
}
return sum == n;
}
public static String perfectNumbersInRange(int start, int end) {
StringBuilder sb = new StringBuilder();
for (int n = start; n <= end; n++) {
if (isPerfect(n)) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(n);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(perfectNumbersInRange(1, 30)); // 6 28
System.out.println(perfectNumbersInRange(1, 10)); // 6
}
}
Summing divisors up to the square root turns an O(n) divisor scan into O(sqrt n)
per number, because each small divisor i hands you its partner n / i for
free. Starting the sum at 1 accounts for the divisor every number shares, and
the partner != i guard stops perfect squares (like 16, where 4 * 4 = 16) from
counting their square root twice.
O((end - start) * sqrt(end))Space: O(1) beyond the outputCommon Mistakes
- Including n itself in the divisor sum, which makes no number look perfect.
- Looping i all the way to n instead of sqrt(n), which is needlessly slow for large ranges.
- Adding the square root twice for perfect squares, inflating the divisor sum.
Edge Cases to Test
- A range with no perfect number (like 7 to 27) should print an empty line.
- start == end where that single value is or is not perfect.
- 1 is not perfect because it has no proper divisor other than itself.
Interview Follow-Ups
- How would you print only the perfect numbers and their divisor lists?
- Perfect numbers are rare — how would you generate them directly using Mersenne primes instead of scanning?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →