Sha256: 6bed4bb3f0105c17a28695e1ec0623c03967b00559e19e197a5a972f61860d30

Contents?: true

Size: 1.58 KB

Versions: 30

Compression:

Stored size: 1.58 KB

Contents

export default class PerfectNumbers {

  /**
   * Calculate all the divisors for a given number.
   * @param {number} number - natural number.
   * @returns {array} array that contains the divisors for a given number NOT including the number itself.
   */
  getDivisors(number) {

    let i;
    let divs = new Array();

    // Accepts only natural numbers greater than 1.
    if (number <= 1) {
      return divs;
    }

    // 1 always divides everyone!
    divs.push(1);

    // Calculate the divisors up the the half of the number + 1
    for (i = 2; i <= number / 2; i++) {

      if (number % i === 0) {
        divs.push(i);
      }
    }

    return divs;
  }

  /**
   * Classify a given number as perfect, abundant or deficient.
   * @param {number} number - number to be classified. Note: if the number is equal or smaller than 0,
   * then returns an error: Classification is only possible for natural numbers.
   * @returns {string} - string that contains the number classification (perfect, abundant or deficient).
   */
  classify(number) {

    let sum, result;

    // Check if the input is valid
    if (number <= 0) {
      throw new Error('Classification is only possible for natural numbers.');
    }

    // Factorize the current number.
    const divsArray = this.getDivisors(number);

    // Sum the factors.
    sum = divsArray.reduce((sum, div) => sum += div, 0);

    // Check if the number is perfect.
    if (sum === number) {
      result = 'perfect';
    } else if (sum > number) {
      result = 'abundant';
    } else {
      result = 'deficient';
    }

    return result;
  }

}

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/perfect-numbers/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/perfect-numbers/example.js