2018-09-30 23:01:58 +01:00
---
id: 594810f028c0303b75339acd
2020-11-27 19:02:05 +01:00
title: 'Abundant, deficient and perfect number classifications'
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302221
2021-01-13 03:31:00 +01:00
dashedName: abundant-deficient-and-perfect-number-classifications
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-05-22 23:30:29 +09:00
2020-11-27 19:02:05 +01:00
These define three classifications of positive integers based on their [proper divisors ](<https://rosettacode.org/wiki/Proper divisors> "Proper divisors" ).
2019-05-22 23:30:29 +09:00
2020-11-27 19:02:05 +01:00
Let $P(n)$ be the sum of the proper divisors of `n` where proper divisors are all positive integers `n` other than `n` itself.
2019-05-22 23:30:29 +09:00
2020-11-27 19:02:05 +01:00
If `P(n) < n` then `n` is classed as `deficient`
2019-05-22 23:30:29 +09:00
2020-11-27 19:02:05 +01:00
If `P(n) === n` then `n` is classed as `perfect`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
If `P(n) > n` then `n` is classed as `abundant`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
**Example**: `6` has proper divisors of `1` , `2` , and `3` . `1 + 2 + 3 = 6` , so `6` is classed as a perfect number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Implement a function that calculates how many of the integers from `1` to `20,000` (inclusive) are in each of the three classes. Output the result as an array in the following format `[deficient, perfect, abundant]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`getDPA` should be a function.
```js
assert(typeof getDPA === 'function');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`getDPA` should return an array.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(Array.isArray(getDPA(100)));
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`getDPA` return value should have a length of 3.
```js
assert(getDPA(100).length === 3);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`getDPA(20000)` should equal [15043, 4, 4953]
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(getDPA(20000), solution);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
const solution = [15043, 4, 4953];
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function getDPA(num) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-02-26 17:07:07 +09:00
function getDPA(num) {
2018-09-30 23:01:58 +01:00
const dpa = [1, 0, 0];
for (let n = 2; n < = num; n += 1) {
let ds = 1;
const e = Math.sqrt(n);
for (let d = 2; d < e ; d + = 1 ) {
if (n % d === 0) {
ds += d + (n / d);
}
}
if (n % e === 0) {
ds += e;
}
dpa[ds < n ? 0 : ds = == n ? 1 : 2 ] + = 1 ;
}
return dpa;
}
```