2018-09-30 23:01:58 +01:00
---
title: 'Abundant, deficient and perfect number classifications'
id: 594810f028c0303b75339acd
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302221
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-05-22 23:30:29 +09:00
These define three classifications of positive integers based on their < a href = 'https://rosettacode.org/wiki/Proper divisors' title = 'Proper divisors' target = '_blank' > proper divisors< / a > .
2019-06-14 20:04:16 +09:00
Let $P(n)$ be the sum of the proper divisors of < code > n< / code > where proper divisors are all positive integers < code > n< / code > other than < code > n< / code > itself.
2019-05-22 23:30:29 +09:00
If < code > P(n) < n < / code > then < code > n< / code > is classed as < code > deficient< / code >
If < code > P(n) === n< / code > then < code > n< / code > is classed as < code > perfect< / code >
If < code > P(n) > n< / code > then < code > n< / code > is classed as < code > abundant< / code >
< strong > Example< / strong > :
2019-06-14 20:04:16 +09:00
< code > 6< / code > has proper divisors of < code > 1< / code > , < code > 2< / code > , and < code > 3< / code > .
< code > 1 + 2 + 3 = 6< / code > , so < code > 6< / code > is classed as a perfect number.
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-06-14 20:04:16 +09:00
Implement a function that calculates how many of the integers from < code > 1< / code > to < code > 20,000< / code > (inclusive) are in each of the three classes. Output the result as an array in the following format < code > [deficient, perfect, abundant]< / code > .
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 07:01:31 -08:00
- text: < code > getDPA</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof getDPA === 'function');
2018-10-04 14:37:37 +01:00
- text: < code > getDPA</ code > should return an array.
2019-07-26 05:24:52 -07:00
testString: assert(Array.isArray(getDPA(100)));
2018-10-04 14:37:37 +01:00
- text: < code > getDPA</ code > return value should have a length of 3.
2019-07-26 05:24:52 -07:00
testString: assert(getDPA(100).length === 3);
2018-10-20 21:02:47 +03:00
- text: < code > getDPA(20000)</ code > should equal [15043, 4, 4953]
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(getDPA(20000), solution);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-02-24 19:04:29 +09:00
function getDPA(num) {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const solution = [15043, 4, 4953];
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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;
}
```
< / section >