# euler1 (v3)

## Setup

``````/*Generate a list of all permutations*/
function permutations(list){
var combinations = []; //All combinations
var combination = [];  //Single combination
var quantity = (1 << list.length);
for (var i = 0; i < quantity ; i++){
combination = [];
for (var j=0;j<list.length;j++) {
if ((i & (1 << j))){
combination.push(list[j]);
}
}
if (combination.length !== 0) {
combinations.push(combination);
}
}
return combinations;
}

/*Generate a product from a list of numbers*/
function listProduct(list){
var product=1;
for (var i=0;i<list.length;i++){
product *=list[i];
}
return product;
}

/*Return the arithmetic sum*/
function arithmeticSum (a, n){
return (1/2)*n*(a+a*n);
}

/*Generate the arithmetic sum of a series based on a multiple*/
function arithmeticSumFromSeriesMultiple (series, multiple){
var a = multiple;
var n = Math.floor(series/multiple);
return arithmeticSum(a, n);
}

function sumMultiples(range, multiples){
var sum= 0;
var subsetSums = [];

var multiplesCombination=permutations(multiples);
for (var i=0;i<multiplesCombination.length;i++){

//Generate product from combinations of multiples
//and
//Find individual sums of all combinations.
subsetSums.push(
arithmeticSumFromSeriesMultiple(
range,
listProduct(multiplesCombination[i])
)
);
}

for (var i=1; i< subsetSums.length + 1;i++){
//Check if i is an even base 2.
if ((i & (i - 1)) == 0){
console.log(">" + subsetSums[i-1])
sum += subsetSums[i-1];
} else {
console.log("<" + subsetSums[i-1])
sum -= subsetSums[i-1];
}
}
return sum;
}

function sumMultiples2(n, multiples){
var sum= 0;
var isMultiple = function(n, multiple){
return ((n % multiple) === 0);
};

var commonMultiples = function (n,multiples){
var isCommon = false;
for (var i=0;i<multiples.length;i++){
isCommon = (isCommon || isMultiple(n, multiples[i]));
}
return isCommon;
};
for (var i=1;i<n;i++) {
if (commonMultiples (i,multiples)) {
sum += i;
}
}
return sum;
}``````

## Test runner

Testing in
TestOps/sec
nomath small
``sumMultiples2(1000, [3,5])``
math small
``sumMultiples(999, [3,5])``
nomath big
``````//This function crashes the VM when larger than this
sumMultiples2(1000000000, [3,5,11,101])``````
``sumMultiples(1000000000-1, [3,5,11,101])``
``````//Doesn't work with nomath.