Skip to content

Instantly share code, notes, and snippets.

@nefarioustim
Created June 5, 2011 12:37
Show Gist options
  • Save nefarioustim/1008926 to your computer and use it in GitHub Desktop.
Save nefarioustim/1008926 to your computer and use it in GitHub Desktop.
Find the sum of all the multiples of 3 or 5 below 1000.
// Do the maths
for(
var sum = 0, i = 1;
i < 1000;
!(i % 3 && i % 5) && (sum += i), i++
);
// Log the result
console.log(sum);
// Prep the array
for(
var arr = [], i = 1;
i < 1000;
!(i % 3 && i % 5) && arr.push(i), i++
);
// Sum the array
console.log(
arr.reduce(
function(prev, current) {
return prev + current
}
)
);
print sum([i for i in range(1000) if not (i % 3 and i % 5)])
puts (1...1000).select{ |n| n % 3 == 0 || n % 5 == 0 }.reduce(:+)
@dwikipedia
Copy link

sum = 0;
for(i = 1; i<1000;i++){
if((i % 3 === 0) || (i % 5 ===0)){
sum += i;
}
}

newbie rules!

@jpvajda
Copy link

jpvajda commented Sep 14, 2018

Super useful. thanks for posting this with the different code options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment