Created
December 20, 2011 14:42
-
-
Save Incognito/1501785 to your computer and use it in GitHub Desktop.
Euler 001
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ | \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