freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/rosetta-code/abundant-deficient-and-perf...

2.0 KiB

id title challengeType forumTopicId dashedName
594810f028c0303b75339acd 余剰数、不足数、完全数の分類 5 302221 abundant-deficient-and-perfect-number-classifications

--description--

真の約数に基づいて、正の整数を3つに分類します。

P(n) は、真の約数が n 自身以外の正の整数 n である場合の n の真の約数の総和です。

P(n) < n の場合、ndeficient に分類されます。

P(n) === n の場合、nperfect に分類されます。

P(n) > n の場合 nabundantに分類されます。

: 6 の真の約数は、1, 2, 3です。 1 + 2 + 3 = 6 なので、6 は完全数に分類されます。

--instructions--

3つの分類に、1 から num までの整数が、それぞれいくつ含まれるかを計算する関数を作成します。 [deficient, perfect, abundant] の形式で、結果を配列として出力します。

--hints--

getDPA という関数です。

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

getDPA(5000) は配列を返します。

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

getDPA(5000) は長さ3の配列を返します。

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

getDPA(5000)[3758, 3, 1239]を返します。

assert.deepEqual(getDPA(5000), [3758, 3, 1239]);

getDPA(10000)[7508, 4, 2488]を返します。

assert.deepEqual(getDPA(10000), [7508, 4, 2488]);

getDPA(20000)[15043, 4, 4953]を返します。

assert.deepEqual(getDPA(20000), [15043, 4, 4953]);

--seed--

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