2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 594810f028c0303b75339acd
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 丰富,不足和完善的数字分类
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: abundant-deficient-and-perfect-number-classifications
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
<p>它们根据<a href='http://rosettacode.org/wiki/Proper divisors' title='适当的除数'>适当的除数</a>定义了三个正整数分类。 </p><p>设$ P(n)$是n的适当除数的总和,其中适当的除数都是n本身以外的正整数。 </p><p>如果<code>P(n) < n</code>那么n被归类为“缺陷” </p><p>如果<code>P(n) === n</code>那么n被归类为“完美” </p><p>如果<code>P(n) > n</code>则n被归类为“丰富” </p><p>例: </p><p> 6具有1,2和3的适当除数。 </p><p> 1 + 2 + 3 = 6,因此6被归类为完美数字。 </p><p>实现一个函数,计算三个类中每个类中1到20,000(包括)的整数。以下列格式将结果输出为数组<code>[deficient, perfect, abundant]</code> 。 </p>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`getDPA`是一个功能。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof getDPA === 'function');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`getDPA`应该返回一个数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(Array.isArray(getDPA(100)));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`getDPA`返回值的长度应为3。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(getDPA(100).length === 3);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`getDPA(20000)`应该等于[15043,4,4953]
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.deepEqual(getDPA(20000), solution);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const solution = [15043, 4, 4953];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function getDPA(num) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function getDPA(num) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|