Sha256: 46549fc2b03f880c6a92b9de118f35ff04db462dcf6be8baaea2d6d232a4623d

Contents?: true

Size: 1.31 KB

Versions: 68

Compression:

Stored size: 1.31 KB

Contents

'use strict';

var PerfectNumbers = function () {

};

/**
 * Calculate all the divisors for a given number and return them as an array.
 * Note: the actual number is not include in the returned array.
 * @param {number} number - a number input.
 * @returns {array} - the array of divisors
 */
PerfectNumbers.prototype.getDivisors = function (number) {
  var i;
  var divs = [];

  // Accepts only natura 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;
};

PerfectNumbers.prototype.classify = function (number) {
  var i;
  var sum;
  var result;

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

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

  // Sum the factors.
  sum = 0;
  for (i = 0; i < divsArray.length; i++) {
    sum = sum + divsArray[i];
  }

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

  return result;
};

module.exports = PerfectNumbers;

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.179 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.178 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.177 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.176 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.175 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.174 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.173 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.172 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.171 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.170 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.169 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.167 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.166 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.165 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.164 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.163 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.162 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.161 tracks/javascript/exercises/perfect-numbers/example.js
trackler-2.2.1.160 tracks/javascript/exercises/perfect-numbers/example.js