Last active
August 29, 2015 14:02
-
-
Save bencoveney/f367fb4dd67118f4f9d8 to your computer and use it in GitHub Desktop.
Multiples of 3 and 5
This file contains 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
// 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); |
This file contains 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
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