freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/rosetta-code/abundant-deficient-and-perfect-number-classifications.md
Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

1.8 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
594810f028c0303b75339acd 丰富,不足和完善的数字分类 5 abundant-deficient-and-perfect-number-classifications

--description--

它们根据适当的除数定义了三个正整数分类。

设$ Pn$是n的适当除数的总和其中适当的除数都是n本身以外的正整数。

如果P(n) < n那么n被归类为“缺陷”

如果P(n) === n那么n被归类为“完美”

如果P(n) > n则n被归类为“丰富”

例:

6具有1,2和3的适当除数。

1 + 2 + 3 = 6因此6被归类为完美数字。

实现一个函数计算三个类中每个类中1到20,000包括的整数。以下列格式将结果输出为数组[deficient, perfect, abundant]

--hints--

getDPA是一个功能。

assert(typeof getDPA === 'function');

getDPA应该返回一个数组。

assert(Array.isArray(getDPA(100)));

getDPA返回值的长度应为3。

assert(getDPA(100).length === 3);

getDPA(20000)应该等于[15043,4,4953]

assert.deepEqual(getDPA(20000), solution);

--seed--

--after-user-code--

const solution = [15043, 4, 4953];

--seed-contents--

function getDPA(num) {

}

--solutions--

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;
}