Skip to content

Instantly share code, notes, and snippets.

@trys
Created October 29, 2015 23:07
Show Gist options
  • Save trys/06db6845cf026b38d880 to your computer and use it in GitHub Desktop.
Save trys/06db6845cf026b38d880 to your computer and use it in GitHub Desktop.
Problem Twenty Three
var divisors = function( a ) {
var divisorList = [];
for (var i = 1; i <= a / 2; i++) {
if ( a % i === 0 ) {
divisorList.push( i );
}
}
return divisorList;
},
isAbundant = function ( n, divisorList ) {
return n < sum( divisorList );
},
sum = function( numbers ) {
var product = 0;
for (var i = 0; i < numbers.length; i++) {
product += numbers[i];
}
return product;
},
abundantIntegers = [],
abundantIntegersCount = 0,
canBeWritten,
currentInt,
total = 0;
for (var i = 1; i <= 28123; i++) {
canBeWritten = false;
if ( isAbundant( i, divisors( i ) ) ) {
abundantIntegers.push( i );
}
for (var j = 0; j < abundantIntegers.length; j++) {
currentInt = abundantIntegers[ j ];
if ( currentInt > i ) {
break;
}
if ( abundantIntegers.indexOf( i - currentInt ) > -1 ) {
canBeWritten = true;
break;
}
}
if ( ! canBeWritten ) {
total += i;
}
}
document.body.innerHTML = total;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment