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