Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Euler 001
/*Generate a list of all Combinations*/
function getCombinations(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=getCombinations(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){
sum += subsetSums[i-1];
} else {
sum -= subsetSums[i-1];
}
}
return sum;
}
console.log(sumMultiples(999, [3,5]));
function sumMultiplesOfTwoOrThree(n){
var sum= 0;
var arithmeticSum = function(a, n){
return (1/2)*n*(a+a*n);
};
var arithmeticSumFromSeriesMultiple = function(series, multiple){
series -=1; //Euler problem #1 wants numbers less than 1000.
var a = multiple;
var n = Math.floor(series/multiple);
return arithmeticSum(a, n);
}
sum += arithmeticSumFromSeriesMultiple(1000, 3);
sum += arithmeticSumFromSeriesMultiple(1000, 5)
sum -= arithmeticSumFromSeriesMultiple(1000, 15);
return sum;
}
console.log(sumMultiplesOfTwoAndThree(1000))
function sumMultiples(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;
}
console.log(sumMultiples(1000, [3,5]))
function sumMultiplesOfTwoAndThree(n){
var sum= 0;
for (var i=1;i<n;i++) {
if (((i % 3) === 0) || ((i % 5) === 0)) {
sum += i;
}
}
return sum;
}
console.log(sumMultiplesOfTwoAndThree(1000))
function getCombinations(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;
}
function listProduct(list){
var product=1;
for (var i=0;i<list.length;i++){
product *=list[i];
}
return product;
}
var data=getCombinations([3,5,15,100]);
var output=[];
for (var i=0;i<data.length;i++){
output.push(listProduct(data[i]));
}
console.log(data);
console.log(output);
$ | \bigcup\limits_{i=1}^n A_i | = $
$ + ( \sum\limits_{i=0}^n | A_i | ) $
$ - ( \sum\limits_{i,j:1 \le i<j \le n} | A_i \cap A_j | ) $
$ + ( \sum\limits_{i,j,k:1 \le i < j < k \le n} | A_i \cap A_j \cap A_k | ) $
$ - ( \sum\limits_{i,j,k,l:1 \le i < j < k < l \le n} | A_i \cap A_j \cap A_k \cap A_l | ) $
$ \vdots $
$ + ( (-1)^{n-1} | A_1 \cap A_2 \cap \ldots \cap A_{n-1} \cap A_n |) $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment