Perfect, abundant and deficient numbers in JavaScript
Intermediate · 4 min read
Add up all the divisors of 6, leaving out 6 itself: 1 + 2 + 3 = 6. You get the number back. The ancient Greeks called numbers like that perfect. Do the same for 12 (1 + 2 + 3 + 4 + 6 = 16) and you overshoot, so 12 is abundant. For 8 (1 + 2 + 4 = 7) you fall short, so 8 is deficient.
In this tutorial you will write a function that sorts any number into one of those three groups, then use it to find every perfect number below 10000 and the smallest odd abundant number. It is a classic number puzzle, and good practice at building a small function from smaller helpers. You need to know filter, reduce and how to list divisors from the previous exercise.
Proper divisors and their sum
The proper divisors of a number are all its divisors except the number itself. For 28 they are 1, 2, 4, 7 and 14. Their sum has a name: the aliquot sum. Comparing that sum with the number gives the three groups:
- equal: perfect
- bigger: abundant
- smaller: deficient
One edge case: 1 has no proper divisors at all, since its only divisor is itself. Its aliquot sum is 0.
Here is the exercise. Try it yourself first if you like, then read on and we will build it together.
36 Proper divisors: perfect, abundant and deficient
fairSum the divisors of n excluding n itself, then classify the number as perfect, abundant or deficient. Find every perfect number below 10000, and the smallest odd abundant number.
Stuck? Three hints, smallest first
- 1. Proper divisors are all the divisors except
n. Note that 1 has none. - 2. Perfect means the sum equals
n, abundant means it exceedsn, deficient means it falls short. - 3. A prime has exactly one proper divisor, so every prime is deficient.
Step 1: reuse the divisor finder
Start from the pair-finding divisors function, which loops only to the square root and returns the divisors in order:
function divisors(n) {
const small = [];
const large = [];
for (let d = 1; d * d <= n; d++) {
if (n % d !== 0) continue;
small.push(d);
if (d !== n / d) large.push(n / d);
}
return [...small, ...large.reverse()];
}
console.log(divisors(28)); // [ 1, 2, 4, 7, 14, 28 ]
Step 2: drop the number itself
filter keeps only the items that pass a test. Keep every divisor that isn't n:
const properDivisors = (n) => divisors(n).filter((d) => d !== n);
console.log(properDivisors(28)); // [ 1, 2, 4, 7, 14 ]
console.log(properDivisors(1)); // []
You might think of dropping the last item instead, since n is always last here. That only works because divisors happens to return a sorted list. filter says what you mean, and keeps working even if the list order changes.
Step 3: add them up
reduce combines a list into one value. Here it adds, starting from 0:
const aliquot = (n) => properDivisors(n).reduce((a, b) => a + b, 0);
console.log(aliquot(28)); // 28
console.log(aliquot(1)); // 0
console.log(aliquot(97)); // 1
The starting value 0 matters for 1, whose list is empty. Without it, reduce on an empty list throws an error. A prime like 97 has only one proper divisor, 1, so its sum is 1. That means every prime is deficient.
Step 4: classify
function classify(n) {
const sum = aliquot(n);
if (sum === n) return 'perfect';
return sum > n ? 'abundant' : 'deficient';
}
console.log([12, 8, 28].map(classify)); // [ 'abundant', 'deficient', 'perfect' ]
console.log(classify(1)); // deficient
Work out the sum once and keep it in sum, so it isn't calculated twice.
Step 5: find every perfect number below 10000
const perfect = [];
for (let n = 1; n < 10000; n++) if (classify(n) === 'perfect') perfect.push(n);
console.log(perfect); // [ 6, 28, 496, 8128 ]
Only four. Perfect numbers are rare: the next one is 33,550,336.
Step 6: abundant numbers, odd and even
Abundant numbers are much more common. Count them below 100:
const abundantBelow100 = [];
for (let n = 1; n < 100; n++) if (classify(n) === 'abundant') abundantBelow100.push(n);
console.log(abundantBelow100.length, abundantBelow100.slice(0, 4)); // 21 [ 12, 18, 20, 24 ]
There are 21, and every one of them is even. So where is the first odd one? Step through odd numbers only, adding 2 each time, until one is abundant:
let firstOddAbundant = 0;
for (let n = 1; !firstOddAbundant; n += 2) if (classify(n) === 'abundant') firstOddAbundant = n;
console.log(firstOddAbundant); // 945
The loop runs while firstOddAbundant is still 0. The first odd abundant number is 945, far beyond all those even ones.
Step 7: a friendly pair
Two numbers whose aliquot sums point at each other are called amicable:
console.log(aliquot(220), aliquot(284)); // 284 220
console.log(classify(220)); // abundant
220's proper divisors add up to 284, and 284's add up to 220. Neither is perfect, though: 220 is abundant and 284 is deficient.
The full solution
The answer panel builds divisors, properDivisors, aliquot and classify, then finds the perfect numbers below 10000, the abundant ones below 100, the smallest odd abundant number and the amicable pair. The checks underneath confirm each result, including that 1 is deficient with a sum of 0 and that every prime tested has a sum of 1. Press Run to see the output.
function divisors(n) {
const small = [];
const large = [];
for (let d = 1; d * d <= n; d++) {
if (n % d !== 0) continue;
small.push(d);
if (d !== n / d) large.push(n / d);
}
return [...small, ...large.reverse()];
}
const properDivisors = (n) => divisors(n).filter((d) => d !== n);
const aliquot = (n) => properDivisors(n).reduce((a, b) => a + b, 0);
function classify(n) {
const sum = aliquot(n);
if (sum === n) return 'perfect';
return sum > n ? 'abundant' : 'deficient';
}
const perfect = [];
for (let n = 1; n < 10000; n++) if (classify(n) === 'perfect') perfect.push(n);
const abundantBelow100 = [];
for (let n = 1; n < 100; n++) if (classify(n) === 'abundant') abundantBelow100.push(n);
let firstOddAbundant = 0;
for (let n = 1; !firstOddAbundant; n += 2) if (classify(n) === 'abundant') firstOddAbundant = n;
console.log('proper divisors of 28 :', properDivisors(28), 'summing to', aliquot(28));
console.log('perfect below 10000 :', perfect);
console.log('abundant below 100 :', abundantBelow100.length, 'of them, starting', abundantBelow100.slice(0, 4));
console.log('smallest odd abundant :', firstOddAbundant);
console.log('220 and 284 :', aliquot(220), aliquot(284), '<- each other');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('28 is the sum of its proper divisors', [properDivisors(28), aliquot(28)], [[1, 2, 4, 7, 14], 28]);
test('so it is perfect, while 12 overshoots and 8 falls short', [12, 8, 28].map(classify), ['abundant', 'deficient', 'perfect']);
test('four perfect numbers below 10000', perfect, [6, 28, 496, 8128]);
test('1 has no proper divisors at all, so it is deficient', [aliquot(1), classify(1)], [0, 'deficient']);
test('every prime is deficient, with an aliquot sum of 1', [2, 3, 5, 97, 7919].every((p) => aliquot(p) === 1), true);
test('21 abundant numbers below 100, the first being 12', [abundantBelow100.length, abundantBelow100[0]], [21, 12]);
test('but the smallest odd one is 945', firstOddAbundant, 945);
test('220 and 284 sum to each other rather than to themselves', [aliquot(220), aliquot(284)], [284, 220]);
test('which makes them amicable, not perfect', classify(220), 'abundant');
Only four perfect numbers exist below ten thousand, and the fifth is 33,550,336 — they thin out fast, and every known one is even. Abundant numbers, by contrast, are common among the evens: 21 of the 99 numbers below 100, every one of them even, because the smallest odd abundant number is 945. That asymmetry is worth an assertion, because "abundant numbers are everywhere" and "odd abundant numbers are rare" are both true and easily confused.
Two edge cases the naive version gets wrong. aliquot(1) is 0, not 1, because 1's only divisor is itself and self is excluded. And filter(d => d !== n) is the right exclusion — dropping the last element is equivalent here only because the list is sorted, which is a fact about the previous exercise rather than about proper divisors.
Common mistakes
- Including
nin the sum. Every number would be abundant. Proper divisors leavenout. - Leaving out the
0inreduce. An empty list, such as the one for 1, throws an error without a starting value. - Calling 1 perfect or abundant. Its proper divisor list is empty, so its sum is 0, and it is deficient.
Try it yourself
- Count how many numbers from 1 to 1000 are deficient.
- Check that 1184 and 1210 are an amicable pair.
- Find the first five odd abundant numbers. How far apart are they?