Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Created February 28, 2013 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save syntacticsugar/5059926 to your computer and use it in GitHub Desktop.
Save syntacticsugar/5059926 to your computer and use it in GitHub Desktop.
Euler #1, in bloated, x-rated, nasty, perverse, lecherous, spaghetti Javascript ! (evil cackle)
// Find the sum of all the multiples of 3 or 5 below 1000.
//
//
// ruby version: (inspired by Lfborjas' Ruby Fuckery)
//multi = ->(x) { (x % 3).zero? or (x % 5).zero? }
//puts (1...1000).select{ |x| multi[x] }.inject(:+).to_s
//233168
function arrayFromRange(lo, hi){
var collection = [];
for (var i = lo; i <= hi; i++) {
collection.push(i);
}
return collection;
};
function reduce(array, operation, initial){
var results = initial;
for (var i = 0; i < array.length; i++) {
results = operation(results, array[i]);
}
return results;
};
var hugeArray = arrayFromRange(1, 999);
function addStuff(x,y){
return x + y;
};
function multiMod(n){
if ((n % 5 === 0) || (n % 3 ===0)) {
return n;
};
};
function euler(array){
var collection = [];
for (var i = 0; i < array.length; i++){
if ((multiMod(array[i]) !== undefined)){
collection.push(array[i]);
};
}
return collection;
};
console.log(reduce( euler(hugeArray), addStuff, 0 ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment