Files
freeCodeCamp/curriculum/challenges/russian/08-coding-interview-prep/rosetta-code/abundant-deficient-and-perfect-number-classifications.russian.md

3.1 KiB

title, id, challengeType, forumTopicId, localeTitle
title id challengeType forumTopicId localeTitle
Abundant, deficient and perfect number classifications 594810f028c0303b75339acd 5 302221 Обильные, неполные и совершенные классификации номеров

Description

Они определяют три классификации положительных целых чисел на основе их правильных делителей .

Пусть $ P (n) $ - сумма собственных делителей n, где собственные делители - все натуральные 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] .

Instructions

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].

Tests

tests:
  - text: <code>getDPA</code> is a function.
    testString: assert(typeof getDPA === 'function');
  - text: <code>getDPA</code> should return an array.
    testString: assert(Array.isArray(getDPA(100)));
  - text: <code>getDPA</code> return value should have a length of 3.
    testString: assert(getDPA(100).length === 3);
  - text: <code>getDPA(20000)</code> should equal [15043, 4, 4953]
    testString: assert.deepEqual(getDPA(20000), solution);

Challenge Seed

function getDPA(num) {
  // Good luck!
}

After Tests

const solution = [15043, 4, 4953];

Solution

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