Skip to content

Instantly share code, notes, and snippets.

@bencoveney
Last active August 29, 2015 14:02
Show Gist options
  • Save bencoveney/f367fb4dd67118f4f9d8 to your computer and use it in GitHub Desktop.
Save bencoveney/f367fb4dd67118f4f9d8 to your computer and use it in GitHub Desktop.
Multiples of 3 and 5
// loop through every number and check if it is a multiple
var multiples = [];
for(var i = 0; i < 1000; i++)
{
// assume the number isnt a multiple
var multiple = false;
// is the number divisible by 3?
if(i % 3 === 0)
{
multiple = true;
console.log(i + " is a multiple of 3");
}
// is the number divisible by 5?
if (i % 5 === 0)
{
multiple = true;
console.log(i + " is a multiple of 5");
}
// if the number is a multiple then add it to the list
if(multiple)
{
multiples.push(i);
}
}
// loop through the multiples and sum them
var sum = 0;
for (var i = 0; i < multiples.length; i++)
{
sum += multiples[i];
}
console.log("Sum of the multiples is " + sum);
sum = 0
for x in range(0, 1000):
if x % 5 == 0 or x % 3 == 0:
sum += x
print sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment