Skip to content

Instantly share code, notes, and snippets.

@jungcome7
Last active July 3, 2020 06:11
Show Gist options
  • Save jungcome7/20ea722d5039b0eff7960196450fbe6d to your computer and use it in GitHub Desktop.
Save jungcome7/20ea722d5039b0eff7960196450fbe6d to your computer and use it in GitHub Desktop.
JS
// Alpha
function Alpha(number = 0) {
this.number = number;
}
Alpha.prototype.isFactor = function(potentialFactor) {
return this.number % potentialFactor === 0;
}
Alpha.prototype.factors = function() {
let factorSet = [];
for (let pod = 1; pod <= Math.sqrt(this.number); pod++ ) {
if (this.isFactor(pod)) {
factorSet.push(pod);
factorSet.push(this.number / pod);
}
}
return factorSet;
}
function ClassifierAlpha (number) {
Alpha.call(this, number);
}
ClassifierAlpha.prototype = Object.create(Alpha.prototype);
ClassifierAlpha.prototype.constructor = ClassifierAlpha;
ClassifierAlpha.prototype.isPerfect = function() {
let currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) === this.number
}
ClassifierAlpha.prototype.isAbundant = function() {
let currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) > this.number
}
ClassifierAlpha.prototype.isDeficient = function() {
let currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) > this.number
}
ClassifierAlpha.prototype.sum = function(factors) {
let total = 0;
factors.forEach( function(factor) {
total += factor;
});
return total;
}
// PrimeAlpha
function PrimeAlpha (number) {
Alpha.call(this, number);
}
PrimeAlpha.prototype = Object.create(Alpha.prototype);
PrimeAlpha.prototype.constructor = PrimeAlpha;
PrimeAlpha.prototype.equalSet = function(aset, bset) {
if (aset.length!== bset.length) return false;
for (let a of aset) if (!bset.includes(a)) return false;
return true;
}
PrimeAlpha.prototype.isPrime = function() {
let primeSet = [1, this.number];
return this.number > 1 && this.equalSet(this.factors(), primeSet);
}
// Output
for(let i = 2; i <= 10; i++) {
let output = '';
if(new ClassifierAlpha(i).isPerfect()) {
output += 'perfect, ';
} else {
output += 'deficient, ';
}
if(new PrimeAlpha(i).isPrime()) {
output += 'prime';
} else {
output += '';
}
console.log(`${i} : ${output}`);
output = '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment